module GTFSDataExchangeAPI

Contains all data exchange api (www.gtfs-data-exchange.com/api) methods and exceptions.

Constants

BASE_URL

The base url for api endpoints. This page also acts as the primary source for api reference documentation.

VERSION

The current gem version. Reference the releases list (github.com/data-creative/gtfs-data-exchange-api-ruby/releases) for previous releases.

Public Class Methods

agencies(options = {}) click to toggle source

List all agencies. @param [Hash] options the request options. @option options [String] :format (‘json’) the requested data format. @raise [UnsupportedRequestFormat] if the requested data format is not supported by the service. @raise [ResponseCodeError, ResponseDataError] for unexpected responses. @return [Array, String] the agencies data in the requested format.

# File lib/gtfs_data_exchange_api.rb, line 16
def self.agencies(options = {})
  format = options[:format] || "json"
  raise UnsupportedRequestFormat, "The requested data format, '#{format}', is not supported by the service. Try 'csv' or 'json' instead." unless ["json","csv"].include?(format)

  request_url = "#{BASE_URL}/agencies?format=#{format}"
  response = HTTParty.get(request_url)

  case format
  when "json"
    raise ResponseCodeError unless response["status_code"] == 200
    raise ResponseDataError unless response["data"]
    parsed_response_data = response["data"].map{|a| Hash[a.map{|k,v| [k.to_sym, (v == "" ? nil : v)]}]}
    return parsed_response_data
  when "csv"
    raise ResponseCodeError unless response.code == 200
    raise ResponseDataError unless response.body
    return response.body
  end
end
agency(options = {}) click to toggle source

Find an agency by its data exchange identifier. @param [Hash] options the request options. @option options [String] :dataexchange_id (‘shore-line-east’) the requested agency identifier. @raise [UnrecognizedDataExchangeId] if the requested agency identifier is unrecognized by the service. @raise [ResponseCodeError, ResponseDataError, ResponseAgencyError] for unexpected responses. @return [Hash] the agency data.

# File lib/gtfs_data_exchange_api.rb, line 42
def self.agency(options = {})
  dataexchange_id = options[:dataexchange_id] || options[:data_exchange_id] || "shore-line-east"

  request_url = "#{BASE_URL}/agency?agency=#{dataexchange_id}"
  response = HTTParty.get(request_url)
  raise UnrecognizedDataExchangeId, "The requested dataexchange_id, '#{dataexchange_id}', was not recognized by the service." if response["status_code"] == 404 && response["status_txt"] == "AGENCY_NOT_FOUND"
  raise ResponseCodeError unless response["status_code"] == 200
  raise ResponseDataError unless response["data"]
  raise ResponseAgencyError unless response["data"]["agency"]

  parsed_agency_data = Hash[response["data"]["agency"].map{|k,v| [k.to_sym, (v == "" ? nil : v)]}]
  return parsed_agency_data
end