Module: GTFSDataExchangeAPI
- Defined in:
- lib/gtfs_data_exchange_api.rb,
lib/gtfs_data_exchange_api/version.rb
Overview
Contains all data exchange api (www.gtfs-data-exchange.com/api) methods and exceptions.
Defined Under Namespace
Classes: ResponseAgencyError, ResponseCodeError, ResponseDataError, UnrecognizedDataExchangeId, UnsupportedRequestFormat
Constant Summary collapse
- BASE_URL =
The base url for api endpoints. This page also acts as the primary source for api reference documentation.
"http://www.gtfs-data-exchange.com/api"
- VERSION =
The current gem version. Reference the releases list (github.com/data-creative/gtfs-data-exchange-api-ruby/releases) for previous releases.
"0.1.0"
Class Method Summary collapse
-
.agencies(options = {}) ⇒ Array, String
List all agencies.
-
.agency(options = {}) ⇒ Hash
Find an agency by its data exchange identifier.
Class Method Details
.agencies(options = {}) ⇒ Array, String
List all agencies.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/gtfs_data_exchange_api.rb', line 16 def self.agencies( = {}) format = [: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 = {}) ⇒ Hash
Find an agency by its data exchange identifier.
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/gtfs_data_exchange_api.rb', line 42 def self.agency( = {}) dataexchange_id = [:dataexchange_id] || [: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 |