tfgm 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +15 -0
  2. data/README.markdown +2 -0
  3. data/lib/tfgm.rb +164 -0
  4. metadata +94 -0
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ OGVkMzBiMDBmYTE2OWJlZjc3NWMxYTQ2YWU0YmU0YmM2NDY5M2Q1ZA==
5
+ data.tar.gz: !binary |-
6
+ OWNlZmI1YjgyNGUwODYyMmYwZGIwNzRmN2M1YTVhMjQ0MjkzNzllNQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ MjNmOGI5OWRkNjQ4ZmFlNDczNTYxNDQ5YjkzMjc4M2E2MzYxYzM1OTM5NjUz
10
+ MGNjYmYwY2UyZmUyYTgyMDlhMzBmNTg0MWEzYTA4NDljZWRmZDg0NWYzZGY0
11
+ YzYyOTBmZWYwYjc4N2FlMjEyYWQ4YzJmZGJiNzY1MjM3ZGRlZWQ=
12
+ data.tar.gz: !binary |-
13
+ OWZkMjg1ODc0MmFhZmNkMTBmYmU4NmI2MGJlNzk5MjJlM2Q0NTMzOGRkMzYx
14
+ MjlmZmJlMGFhOTk0NGNhZjVmNDgxOGFmNGY5MDk0M2FkZWMxNjUwMzE3NzM4
15
+ YWZiOTkwZDdiMDE4YjExYmY2MTAxZDdmODM0MjY5NTYzY2NjYWU=
@@ -0,0 +1,2 @@
1
+ # Transport for Greater Manchester Rubygem
2
+ It is early. Very.
@@ -0,0 +1,164 @@
1
+ ##
2
+ ## Dependencies (used for development and using this module independently)
3
+ ##
4
+ ['rubygems', 'curb', 'json', 'active_support/core_ext/object/to_query'].each { |_require| require _require }
5
+
6
+ ##
7
+ ## Some nice camel-cased error names. Gotta catch 'em all.
8
+ ##
9
+ module TFGM_Error
10
+ class Error < StandardError; end
11
+ class InvalidDeveloperKey < Error; end
12
+ class InvalidApplicationKey < Error; end
13
+ class DeveloperOrApplicationKeyNotAccepted < Error; end
14
+ class CarParkStateTypeInvalid < Error; end
15
+ end
16
+
17
+ ##
18
+ ## Let's get to work.
19
+ ##
20
+ module TFGM
21
+
22
+ ##
23
+ ## TFGM::API represents external API calls.
24
+ ##
25
+ class API
26
+
27
+ ## The endpoint we're calling, stored as a constant.
28
+ TFGM_BASE_URL = "http://opendata.tfgm.com"
29
+
30
+ ## The version
31
+ TFGM_VERSION = "0.0.1"
32
+
33
+ ##
34
+ ## When we call TFGM::API.new, we automatically call initialize. Interesting.
35
+ ##
36
+ def initialize(dev_key, app_key)
37
+
38
+ ## Developer key is needed.
39
+ throw TFGM_Error::InvalidDeveloperKey if not dev_key.to_s.length == 36
40
+
41
+ ## Oops, and an application key. They're tied.
42
+ throw TFGM_Error::InvalidApplicationKey if not app_key.to_s.length == 36
43
+
44
+ ## Move the keys to class-based variables
45
+ @developer_key = dev_key
46
+ @application_key = app_key
47
+ end
48
+
49
+ ##
50
+ ## Our central API beast.
51
+ ##
52
+ def _call(endpoint, params = {})
53
+ ## Compile RESTful API address.
54
+ _query = TFGM_BASE_URL + endpoint
55
+ _query += "?" + params.to_query if params.count > 0
56
+
57
+ ## Make the call
58
+ _response = Curl::Easy.perform(_query.to_s) do |_curl|
59
+ _curl.useragent = "Ruby/Curb/TFGM_API/#{TFGM_VERSION}"
60
+ _curl.headers['DevKey'] = @developer_key
61
+ _curl.headers['AppKey'] = @application_key
62
+ _curl.headers['Content-type'] = 'text/json'
63
+ end
64
+
65
+ ## Exception catching to make it work smoothly.
66
+ begin
67
+ _result = JSON.parse(_response.body_str) ## Parse
68
+
69
+ ## Right, throw an error if we can't authorize.
70
+ throw TFGM_Error::DeveloperOrApplicationKeyNotAccepted if _result['Message'].eql?("Authorization has been denied for this request.")
71
+ rescue TypeError
72
+ ## Empty by design.
73
+ rescue JSON::ParserError
74
+ throw TFGM_Error::JSONParserError
75
+ end
76
+
77
+ _result
78
+ end
79
+
80
+ ##
81
+ ## Show all car parks
82
+ ##
83
+ def carparks(page = 1, per_page = 10, options = {})
84
+ _options = { :pageIndex => page, :pageSize => per_page }
85
+
86
+ ## This validates whether a car park state is valid.
87
+ if options.has_key?('state')
88
+ _enums = self._call('/api/enums')
89
+ throw TFGM_Error::CarParkStateTypeInvalid unless _enums.member?(options['state'])
90
+ end
91
+
92
+ self._call('/api/carparks', _options.merge(options))
93
+ end
94
+
95
+ ##
96
+ ## Show a single car park
97
+ ##
98
+ def carpark(id, options = {})
99
+ self._call('/api/carparks/' + id.to_s, options)
100
+ end
101
+
102
+ ##
103
+ ## Show states of car parks.
104
+ ##
105
+ def carpark_states(options = {})
106
+ self._call('/api/enums', options)
107
+ end
108
+
109
+ ##
110
+ ## Hi, buses.
111
+ ##
112
+ def routes(options = {})
113
+ self._call("/api/routes", options)
114
+ end
115
+
116
+ def route(bus_code, options = {})
117
+ self._call("/api/routes/#{bus_code.to_s}", options)
118
+ end
119
+
120
+ def buses_on_route(bus_code, options = {})
121
+ self._call("/api/routes/#{bus_code.to_s}/buses", options)
122
+ end
123
+
124
+ def stops_on_route(bus_code, options = {})
125
+ self._call("/api/routes/#{bus_code.to_s}/stops", options)
126
+ end
127
+
128
+ def bus_stops_near(lat, lng, options = {})
129
+ _options = { "latitude" => lat, "longitude" => lng }
130
+ self._call("/api/stops", _options.merge(options))
131
+ end
132
+
133
+ def bus_stop(atco_code, options = {})
134
+ self._call("/api/stops/#{atco_code.to_s}", options)
135
+ end
136
+
137
+ def buses_on_stop(atco_code, options = {})
138
+ self._call("/api/stops/#{atco_code}/route", options)
139
+ end
140
+
141
+ ##
142
+ ## Journey times
143
+ ##
144
+ def journey_times(options = {})
145
+ self._call("/api/journeytimes", options)
146
+ end
147
+
148
+ def journey_time(journey_id, options = {})
149
+ self._call("/api/journeytimes/#{journey_id.to_s}", options)
150
+ end
151
+
152
+ ##
153
+ ## Version
154
+ ##
155
+ def version
156
+ TFGM_VERSION
157
+ end
158
+
159
+ end
160
+ end
161
+
162
+ ##
163
+ ## Built by Bilaw.al, but please, be awesome and improve it :)
164
+ ##
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tfgm
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Bilawal Hameed
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-03-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: curb
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: active_support
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: ! " A lightweight Ruby wrapper for the Transport for Greater Manchester
56
+ Open Data REST API. It was built on the Routes to the Future Innovation Challenge
57
+ in Manchester by @bilawalhameed.\n As of March 2013, it supports the REST API to
58
+ query data such as bus routes, car parks, metrolinks based in Greater Manchester.
59
+ The API itself is new, so I would take caution.\n"
60
+ email: b@bilawal.co.uk
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files:
64
+ - README.markdown
65
+ files:
66
+ - lib/tfgm.rb
67
+ - README.markdown
68
+ homepage: http://github.com/bih/tfgm
69
+ licenses:
70
+ - MIT
71
+ metadata: {}
72
+ post_install_message: ! '>> NOTICE: The TFGM REST API is unstable and is not recommended
73
+ for production. It is purely for testing at this stage, and we encourage contributing
74
+ to this gem via http://github.com/bih/tfgm <<'
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.0.3
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Ruby wrapper for Transport for Greater Manchester's API
94
+ test_files: []