class TFGM::API

TFGM::API represents external API calls.

Constants

TFGM_BASE_URL

The endpoint we're calling, stored as a constant.

TFGM_VERSION

The version

Public Class Methods

new(dev_key, app_key) click to toggle source

When we call TFGM::API.new, we automatically call initialize. Interesting.

# File lib/tfgm.rb, line 36
def initialize(dev_key, app_key)

  ## Developer key is needed.
  throw TFGM_Error::InvalidDeveloperKey if not dev_key.to_s.length == 36

  ## Oops, and an application key. They're tied.
  throw TFGM_Error::InvalidApplicationKey if not app_key.to_s.length == 36

  ## Move the keys to class-based variables
  @developer_key = dev_key
  @application_key = app_key

  ## This checks if the API keys is valid.
  _call('/api/enums')
end

Public Instance Methods

_call(endpoint, params = {}) click to toggle source

Our central API beast.

# File lib/tfgm.rb, line 55
def _call(endpoint, params = {})
  ## Compile RESTful API address.
  _query = TFGM_BASE_URL + endpoint
  _query += "?" + params.to_query if params.count > 0

  ## Make the call
  _response = Curl::Easy.perform(_query.to_s) do |_curl|
    _curl.useragent = "Ruby/Curb/TFGM_API/#{TFGM_VERSION}"
    _curl.headers['DevKey'] = @developer_key
    _curl.headers['AppKey'] = @application_key
    _curl.headers['Content-type'] = 'text/json'
  end

  ## Exception catching to make it work smoothly.
  begin
    _result = JSON.parse(_response.body_str) ## Parse

    ## Right, throw an error if we can't authorize.
    throw TFGM_Error::DeveloperOrApplicationKeyNotAccepted if _result['Message'].eql?("Authorization has been denied for this request.")
  rescue TypeError, JSON::ParserError
    ## Empty by design.
  end

  _result
end
bus_stop(atco_code, options = {}) click to toggle source
# File lib/tfgm.rb, line 138
def bus_stop(atco_code, options = {})
  self._call("/api/stops/#{atco_code.to_s}", options)
end
bus_stops_near(lat, lng, options = {}) click to toggle source
# File lib/tfgm.rb, line 133
def bus_stops_near(lat, lng, options = {})
  _options = { "latitude" => lat, "longitude" => lng }
  self._call("/api/stops", _options.merge(options))
end
buses_on_route(bus_code, options = {}) click to toggle source
# File lib/tfgm.rb, line 125
def buses_on_route(bus_code, options = {})
  self._call("/api/routes/#{bus_code.to_s}/buses", options)
end
buses_on_stop(atco_code, options = {}) click to toggle source
# File lib/tfgm.rb, line 142
def buses_on_stop(atco_code, options = {})
  self._call("/api/stops/#{atco_code}/route", options)
end
carpark(id, options = {}) click to toggle source

Show a single car park

# File lib/tfgm.rb, line 99
def carpark(id, options = {})
  self._call('/api/carparks/' + id.to_s, options)
end
carpark_states(options = {}) click to toggle source

Show states of car parks.

# File lib/tfgm.rb, line 106
def carpark_states(options = {})
  self._call('/api/enums', options)
end
carparks(page = 0, per_page = 10, options = {}) click to toggle source

Show all car parks

# File lib/tfgm.rb, line 84
def carparks(page = 0, per_page = 10, options = {})
  _options = { :pageIndex => page, :pageSize => per_page }

  ## This validates whether a car park state is valid.
  if options.has_key?('state') then
    _enums = self._call('/api/enums')
    throw TFGM_Error::CarParkStateTypeInvalid unless _enums.member?(options['state'])
  end

  self._call('/api/carparks', _options.merge(options))
end
is_route(bus_code, options = {}) click to toggle source
# File lib/tfgm.rb, line 121
def is_route(bus_code, options = {})
  self.route(bus_code, options).count > 0
end
journey_times(journey_id = 0, options = {}) click to toggle source

Journey times

# File lib/tfgm.rb, line 149
def journey_times(journey_id = 0, options = {})
  if journey_id > 0 then
    self._call("/api/journeytimes/#{journey_id.to_s}", options)
  else 
    self._call("/api/journeytimes", options)
  end
end
route(bus_code, options = {}) click to toggle source
# File lib/tfgm.rb, line 117
def route(bus_code, options = {})
  self._call("/api/routes/#{bus_code.to_s}", options)
end
routes(options = {}) click to toggle source

Hi, buses.

# File lib/tfgm.rb, line 113
def routes(options = {})
  self._call("/api/routes", options)
end
stops_on_route(bus_code, options = {}) click to toggle source
# File lib/tfgm.rb, line 129
def stops_on_route(bus_code, options = {})
  self._call("/api/routes/#{bus_code.to_s}/stops", options)
end
version() click to toggle source

Version

# File lib/tfgm.rb, line 160
def version
  TFGM_VERSION
end