Class: GTFS::DataExchange::API

Inherits:
Object
  • Object
show all
Defined in:
lib/gtfs/data_exchange/api.rb

Overview

Contains all data exchange 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"

Class Method Summary collapse

Class Method Details

.agencies(options = {}) ⇒ Array, String

List all agencies.

Parameters:

  • options (Hash) (defaults to: {})

    the request options.

Options Hash (options):

  • :format (String) — default: 'json'

    the requested data format.

Returns:

  • (Array, String)

    the agencies data in the requested format.

Raises:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/gtfs/data_exchange/api.rb', line 23

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"]
    return 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 dataexchange_id.

Parameters:

  • options (Hash) (defaults to: {})

    the request options.

Options Hash (options):

  • :dataexchange_id (String) — default: 'shore-line-east'

    the requested data format.

Returns:

  • (Hash)

    the agency data.

Raises:



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/gtfs/data_exchange/api.rb', line 48

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"]
  return response["data"]["agency"]
end