Class: TFGM::API
- Inherits:
-
Object
- Object
- TFGM::API
- 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
-
#_call(endpoint, params = {}) ⇒ Object
Our central API beast.
- #bus_stop(atco_code, options = {}) ⇒ Object
- #bus_stops_near(lat, lng, options = {}) ⇒ Object
- #buses_on_route(bus_code, options = {}) ⇒ Object
- #buses_on_stop(atco_code, options = {}) ⇒ Object
-
#carpark(id, options = {}) ⇒ Object
Show a single car park.
-
#carpark_states(options = {}) ⇒ Object
Show states of car parks.
-
#carparks(page = 0, per_page = 10, options = {}) ⇒ Object
Show all car parks.
-
#initialize(dev_key, app_key) ⇒ API
constructor
When we call TFGM::API.new, we automatically call initialize.
- #is_route(bus_code, options = {}) ⇒ Object
-
#journey_times(journey_id = 0, options = {}) ⇒ Object
Journey times.
- #route(bus_code, options = {}) ⇒ Object
-
#routes(options = {}) ⇒ Object
Hi, buses.
- #stops_on_route(bus_code, options = {}) ⇒ Object
-
#version ⇒ Object
Version.
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, = {}) self._call("/api/stops/#{atco_code.to_s}", ) end |
#bus_stops_near(lat, lng, options = {}) ⇒ Object
133 134 135 136 |
# File 'lib/tfgm.rb', line 133 def bus_stops_near(lat, lng, = {}) = { "latitude" => lat, "longitude" => lng } self._call("/api/stops", .merge()) end |
#buses_on_route(bus_code, options = {}) ⇒ Object
125 126 127 |
# File 'lib/tfgm.rb', line 125 def buses_on_route(bus_code, = {}) self._call("/api/routes/#{bus_code.to_s}/buses", ) end |
#buses_on_stop(atco_code, options = {}) ⇒ Object
142 143 144 |
# File 'lib/tfgm.rb', line 142 def buses_on_stop(atco_code, = {}) self._call("/api/stops/#{atco_code}/route", ) end |
#carpark(id, options = {}) ⇒ Object
Show a single car park
99 100 101 |
# File 'lib/tfgm.rb', line 99 def carpark(id, = {}) self._call('/api/carparks/' + id.to_s, ) end |
#carpark_states(options = {}) ⇒ Object
Show states of car parks.
106 107 108 |
# File 'lib/tfgm.rb', line 106 def carpark_states( = {}) self._call('/api/enums', ) 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, = {}) = { :pageIndex => page, :pageSize => per_page } ## This validates whether a car park state is valid. if .has_key?('state') then _enums = self._call('/api/enums') throw TFGM_Error::CarParkStateTypeInvalid unless _enums.member?(['state']) end self._call('/api/carparks', .merge()) end |
#is_route(bus_code, options = {}) ⇒ Object
121 122 123 |
# File 'lib/tfgm.rb', line 121 def is_route(bus_code, = {}) self.route(bus_code, ).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, = {}) if journey_id > 0 then self._call("/api/journeytimes/#{journey_id.to_s}", ) else self._call("/api/journeytimes", ) end end |
#route(bus_code, options = {}) ⇒ Object
117 118 119 |
# File 'lib/tfgm.rb', line 117 def route(bus_code, = {}) self._call("/api/routes/#{bus_code.to_s}", ) end |
#routes(options = {}) ⇒ Object
Hi, buses.
113 114 115 |
# File 'lib/tfgm.rb', line 113 def routes( = {}) self._call("/api/routes", ) end |
#stops_on_route(bus_code, options = {}) ⇒ Object
129 130 131 |
# File 'lib/tfgm.rb', line 129 def stops_on_route(bus_code, = {}) self._call("/api/routes/#{bus_code.to_s}/stops", ) end |