Class: TFGM::API

Inherits:
Object
  • Object
show all
Defined in:
lib/tfgm.rb

Overview

TFGM::API represents external API calls.

Constant Summary collapse

TFGM_BASE_URL =

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

"http://opendata.tfgm.com"
TFGM_VERSION =

The version

"0.0.3"

Instance Method Summary collapse

Constructor Details

#initialize(dev_key, app_key) ⇒ API

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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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

Instance Method Details

#_call(endpoint, params = {}) ⇒ Object

Our central API beast.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 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 = {}) ⇒ Object



138
139
140
# 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 = {}) ⇒ Object



133
134
135
136
# 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 = {}) ⇒ Object



125
126
127
# 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 = {}) ⇒ Object



142
143
144
# 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 = {}) ⇒ Object

Show a single car park



99
100
101
# File 'lib/tfgm.rb', line 99

def carpark(id, options = {})
  self._call('/api/carparks/' + id.to_s, options)
end

#carpark_states(options = {}) ⇒ Object

Show states of car parks.



106
107
108
# File 'lib/tfgm.rb', line 106

def carpark_states(options = {})
  self._call('/api/enums', options)
end

#carparks(page = 0, per_page = 10, options = {}) ⇒ Object

Show all car parks



84
85
86
87
88
89
90
91
92
93
94
# 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 = {}) ⇒ Object



121
122
123
# 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 = {}) ⇒ Object

Journey times



149
150
151
152
153
154
155
# 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 = {}) ⇒ Object



117
118
119
# File 'lib/tfgm.rb', line 117

def route(bus_code, options = {})
  self._call("/api/routes/#{bus_code.to_s}", options)
end

#routes(options = {}) ⇒ Object

Hi, buses.



113
114
115
# File 'lib/tfgm.rb', line 113

def routes(options = {})
  self._call("/api/routes", options)
end

#stops_on_route(bus_code, options = {}) ⇒ Object



129
130
131
# File 'lib/tfgm.rb', line 129

def stops_on_route(bus_code, options = {})
  self._call("/api/routes/#{bus_code.to_s}/stops", options)
end

#versionObject

Version



160
161
162
# File 'lib/tfgm.rb', line 160

def version
  TFGM_VERSION
end