tfl_api_client 0.2.1 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/LICENSE +1 -1
  4. data/README.md +1 -1
  5. data/lib/tfl_api_client.rb +2 -0
  6. data/{spec/support/coverage.rb → lib/tfl_api_client/accident_stats.rb} +27 -8
  7. data/{spec/support/vcr.rb → lib/tfl_api_client/air_quality.rb} +25 -14
  8. data/lib/tfl_api_client/bike_point.rb +0 -1
  9. data/lib/tfl_api_client/client.rb +16 -0
  10. data/lib/tfl_api_client/version.rb +1 -1
  11. metadata +4 -55
  12. data/.gitignore +0 -37
  13. data/.travis.yml +0 -31
  14. data/.yardopts +0 -4
  15. data/GETTING_STARTED.md +0 -231
  16. data/Gemfile +0 -4
  17. data/Rakefile +0 -36
  18. data/spec/cassettes/bike_point/authorised_client_location.yml +0 -83
  19. data/spec/cassettes/bike_point/authorised_client_locations.yml +0 -8179
  20. data/spec/cassettes/bike_point/authorised_client_locations_within_bounding_box.yml +0 -402
  21. data/spec/cassettes/bike_point/authorised_client_locations_within_locus.yml +0 -106
  22. data/spec/cassettes/bike_point/authorised_client_search.yml +0 -80
  23. data/spec/cassettes/bike_point/unauthorised_client_location.yml +0 -50
  24. data/spec/cassettes/bike_point/unauthorised_client_locations.yml +0 -50
  25. data/spec/cassettes/bike_point/unauthorised_client_locations_within_bounding_box.yml +0 -50
  26. data/spec/cassettes/bike_point/unauthorised_client_locations_within_locus.yml +0 -50
  27. data/spec/cassettes/bike_point/unauthorised_client_search.yml +0 -50
  28. data/spec/cassettes/cycle/authorised_client_superhighway.yml +0 -75
  29. data/spec/cassettes/cycle/authorised_client_superhighways.yml +0 -99
  30. data/spec/cassettes/cycle/unauthorised_client_superhighway.yml +0 -50
  31. data/spec/cassettes/cycle/unauthorised_client_superhighways.yml +0 -50
  32. data/spec/integration/bike_point_spec.rb +0 -158
  33. data/spec/integration/cycle_spec.rb +0 -79
  34. data/spec/spec_helper.rb +0 -114
  35. data/spec/support/helpers.rb +0 -81
  36. data/spec/unit/bike_point_spec.rb +0 -87
  37. data/spec/unit/client_spec.rb +0 -199
  38. data/spec/unit/cycle_spec.rb +0 -66
  39. data/tfl_api_client.gemspec +0 -37
@@ -1,81 +0,0 @@
1
- #
2
- # Copyright (c) 2015 - 2017 Luke Hackett
3
- #
4
- # MIT License
5
- #
6
- # Permission is hereby granted, free of charge, to any person obtaining
7
- # a copy of this software and associated documentation files (the
8
- # "Software"), to deal in the Software without restriction, including
9
- # without limitation the rights to use, copy, modify, merge, publish,
10
- # distribute, sublicense, and/or sell copies of the Software, and to
11
- # permit persons to whom the Software is furnished to do so, subject to
12
- # the following conditions:
13
- #
14
- # The above copyright notice and this permission notice shall be
15
- # included in all copies or substantial portions of the Software.
16
- #
17
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
- # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
- # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
- # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
- # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
- # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
- # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
- #
25
-
26
- require 'uri'
27
-
28
- module Helpers
29
-
30
- # Returns a new instance of the TflApi::Client class using authorised details.
31
- # All calls to the API using this object should return the expected data.
32
- #
33
- # @return [TflApi::Client] A TflApi::Client instance created with valid credentials
34
- #
35
- def authorised_client
36
- # Fail early if the Application private and public keys are not set
37
- fail 'Please set your app id via the TFL_APP_ID environment variable' unless ENV['TFL_APP_ID']
38
- fail 'Please set your app key via the TFL_APP_KEY environment variable' unless ENV['TFL_APP_KEY']
39
-
40
- TflApi::Client.new(app_id: ENV['TFL_APP_ID'], app_key: ENV['TFL_APP_KEY'], log_location: '/dev/null')
41
- end
42
-
43
- # Returns a new instance of the TflApi::Client class using unauthorised details.
44
- # All calls to the API using this object should result in unauthorised access
45
- # errors from the TFL API.
46
- #
47
- # @return [TflApi::Client] A TflApi::Client instance created with invalid credentials
48
- #
49
- def unauthorised_client
50
- TflApi::Client.new(app_id: 123, app_key: 456, log_location: '/dev/null')
51
- end
52
-
53
- # Returns a new instance of the TflApi::Client class using test details.
54
- # This client should only be used within Unit Tests, as the host has been
55
- # fixed to a dummy value.
56
- #
57
- # @return [TflApi::Client] A TflApi::Client instance created with invalid credentials
58
- #
59
- def test_client
60
- TflApi::Client.new(app_id: 12345, app_key: 6789, host: 'https://somehost', log_location: '/dev/null')
61
- end
62
-
63
- # Performs a HTTP stubbed request for the given method upon the given resource.
64
- # Optional params can be given to be appended to the URL, whilst the to_return
65
- # allows for specific return information to be set.
66
- #
67
- # @param method [Symbol] The type of stub request, e.g. :get, :post etc
68
- # @param resource [String] The resource path to stub
69
- # @param to_return [Hash] Response to return upon issuing the request
70
- #
71
- # @option params [Hash] A hash of URL params to add to the URI
72
- #
73
- # @return [WebMock::RequestStub] A WebMock::RequestStub instance
74
- #
75
- def stub_api_request(method, resource, params={}, to_return)
76
- params_string = URI.encode_www_form(params)
77
- uri = URI.parse("https://somehost/#{resource}?#{params_string}&app_id=12345&app_key=6789")
78
- stub_request(method.to_sym, uri).to_return(to_return)
79
- end
80
-
81
- end
@@ -1,87 +0,0 @@
1
- #
2
- # Copyright (c) 2015 - 2017 Luke Hackett
3
- #
4
- # MIT License
5
- #
6
- # Permission is hereby granted, free of charge, to any person obtaining
7
- # a copy of this software and associated documentation files (the
8
- # "Software"), to deal in the Software without restriction, including
9
- # without limitation the rights to use, copy, modify, merge, publish,
10
- # distribute, sublicense, and/or sell copies of the Software, and to
11
- # permit persons to whom the Software is furnished to do so, subject to
12
- # the following conditions:
13
- #
14
- # The above copyright notice and this permission notice shall be
15
- # included in all copies or substantial portions of the Software.
16
- #
17
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
- # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
- # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
- # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
- # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
- # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
- # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
- #
25
-
26
- require_relative '../spec_helper'
27
-
28
-
29
- describe TflApi::Client::BikePoint do
30
- let!(:client) { TflApi::Client.new(app_id: 123, app_key: 456) }
31
- let!(:bike_point) { TflApi::Client::BikePoint.new(client) }
32
- let(:sample_response) do
33
- [{
34
- 'id' => 1,
35
- 'url' => 'https://someurl/path/to/resource',
36
- 'name' => 'Test Response',
37
- 'additionalProperties' => [
38
- { 'foo' => 'bar', 'age' => 1234 },
39
- { 'baz' => 'foo', 'age' => 5678 }
40
- ]
41
- }]
42
- end
43
-
44
- describe '#locations' do
45
- before { allow(client).to receive(:get).with('/BikePoint').and_return(sample_response) }
46
- subject { bike_point.locations }
47
-
48
- it { is_expected.to be_an(Array) }
49
- it { is_expected.to eq(sample_response) }
50
- end
51
-
52
- describe '#location' do
53
- before { allow(client).to receive(:get).with('/BikePoint/SOME_ID').and_return(sample_response.first) }
54
- subject { bike_point.location('SOME_ID') }
55
-
56
- it { is_expected.to be_a(Hash) }
57
- it { is_expected.to eq(sample_response.first) }
58
- end
59
-
60
- describe '#locations_within_locus' do
61
- let(:query) { { lat: 1, lon: 2, radius: 3 } }
62
- before { allow(client).to receive(:get).with('/BikePoint', query).and_return(sample_response) }
63
- subject { bike_point.locations_within_locus(query[:lat], query[:lon], query[:radius]) }
64
-
65
- it { is_expected.to be_an(Array) }
66
- it { is_expected.to eq(sample_response) }
67
- end
68
-
69
- describe '#locations_within_bounding_box' do
70
- let(:query) { { swLat: 1, swLon: 2, neLat: 3, neLon: 4 } }
71
- before { allow(client).to receive(:get).with('/BikePoint', query).and_return(sample_response) }
72
- subject { bike_point.locations_within_bounding_box(query[:swLat], query[:swLon], query[:neLat], query[:neLon]) }
73
-
74
- it { is_expected.to be_an(Array) }
75
- it { is_expected.to eq(sample_response) }
76
- end
77
-
78
- describe '#search' do
79
- let(:query) { { query: 'St. James Park' } }
80
- before { allow(client).to receive(:get).with('/BikePoint/Search', query).and_return(sample_response) }
81
- subject { bike_point.search(query[:query]) }
82
-
83
- it { is_expected.to be_an(Array) }
84
- it { is_expected.to eq(sample_response) }
85
- end
86
-
87
- end
@@ -1,199 +0,0 @@
1
- #
2
- # Copyright (c) 2015 - 2017 Luke Hackett
3
- #
4
- # MIT License
5
- #
6
- # Permission is hereby granted, free of charge, to any person obtaining
7
- # a copy of this software and associated documentation files (the
8
- # "Software"), to deal in the Software without restriction, including
9
- # without limitation the rights to use, copy, modify, merge, publish,
10
- # distribute, sublicense, and/or sell copies of the Software, and to
11
- # permit persons to whom the Software is furnished to do so, subject to
12
- # the following conditions:
13
- #
14
- # The above copyright notice and this permission notice shall be
15
- # included in all copies or substantial portions of the Software.
16
- #
17
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
- # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
- # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
- # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
- # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
- # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
- # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
- #
25
-
26
- require 'logger'
27
- require_relative '../spec_helper'
28
-
29
- describe TflApi::Client do
30
- let(:nil_log_path) { '/dev/null' }
31
-
32
- describe '#initialize' do
33
- context 'mandatory attributes' do
34
- it 'should raise an exception when Application ID is not present' do
35
- expect {
36
- TflApi::Client.new(app_key: 123456789)
37
- }.to raise_error(ArgumentError)
38
- end
39
-
40
- it 'should raise an exception when Application Key is not present' do
41
- expect {
42
- TflApi::Client.new(app_id: 12345)
43
- }.to raise_error(ArgumentError)
44
- end
45
- end
46
-
47
- context 'default attributes' do
48
- subject(:client) { TflApi::Client.new(app_id: 12345, app_key: 6789) }
49
-
50
- it 'should set the host to a "https://api.tfl.gov.uk" when not present' do
51
- expect(client.host).to eq(URI.parse('https://api.tfl.gov.uk'))
52
- end
53
-
54
- it 'should set the log level to INFO' do
55
- expect(client.log_level).to eq(Logger::INFO)
56
- end
57
-
58
- it 'should set the log location to STDOUT' do
59
- expect(client.log_location).to eq(STDOUT)
60
- end
61
- end
62
-
63
- context 'when providing optional attributes' do
64
- it 'should allow a custom host' do
65
- client = TflApi::Client.new(app_id: 12345, app_key: 6789, host: 'http://myhost.com', log_location: nil_log_path)
66
- expect(client.host).to eq(URI.parse('http://myhost.com'))
67
- end
68
-
69
- it 'should allow a custom log level' do
70
- client = TflApi::Client.new(app_id: 12345, app_key: 6789, log_level: Logger::DEBUG)
71
- expect(client.log_level).to eq(Logger::DEBUG)
72
- end
73
-
74
- it 'should allow a custom log location' do
75
- client = TflApi::Client.new(app_id: 12345, app_key: 6789, log_location: '/dev/null')
76
- expect(client.log_location).to eq('/dev/null')
77
- end
78
-
79
- context 'when setting a custom logger' do
80
- let!(:logger) { Logger.new('/dev/null') }
81
-
82
- it 'should create a valid client object' do
83
- client = TflApi::Client.new(app_id: 12345, app_key: 6789, logger: logger)
84
- expect(client.logger).to eq(logger)
85
- end
86
-
87
- it 'should not allow a custom logger that is not a logger' do
88
- expect {
89
- TflApi::Client.new(app_id: 12345, app_key: 6789, logger: 'some_logger')
90
- }.to raise_error(ArgumentError)
91
- end
92
-
93
- it 'should not allow setting the log level with a custom logger' do
94
- expect {
95
- TflApi::Client.new(app_id: 12345, app_key: 6789, logger: logger, log_level: Logger::ERROR)
96
- }.to raise_error(ArgumentError)
97
- end
98
-
99
- it 'should not allow setting the log location with a custom logger' do
100
- expect {
101
- TflApi::Client.new(app_id: 12345, app_key: 6789, logger: logger, log_location: nil_log_path)
102
- }.to raise_error(ArgumentError)
103
- end
104
- end
105
- end
106
- end
107
-
108
- describe '#bike_point' do
109
- it 'should return a Client::BikePoint object' do
110
- expect(test_client.bike_point).to be_an_instance_of(TflApi::Client::BikePoint)
111
- end
112
- end
113
-
114
- describe '#get' do
115
- subject(:client) { test_client }
116
-
117
- context 'when issuing the request' do
118
- it 'should correctly format the request url' do
119
- stub = stub_api_request(:get, 'SomeResource', { status: 200, body: '{"status": "ok"}' })
120
- client.get('/SomeResource')
121
- expect(stub).to have_requested(:get, 'https://somehost/SomeResource?app_id=12345&app_key=6789').once
122
- end
123
-
124
- it 'should correctly format the request url with optional parameters' do
125
- stub = stub_api_request(:get, 'SomeResource', { foo: 'bar', version: 1 }, { body: '{"status": "ok"}' })
126
- client.get('/SomeResource', foo: 'bar', version: 1)
127
- expect(stub).to have_requested(:get, 'https://somehost/SomeResource?foo=bar&version=1&app_id=12345&app_key=6789').once
128
- end
129
- end
130
-
131
- context 'when the resource returns successfully' do
132
- it 'should return a json response as a hash on a successful response' do
133
- stub_api_request(:get, 'SomeResource', { status: 200, body: '{"status": "ok"}' })
134
- response = client.get('/SomeResource')
135
- expect(response).to be_a(Hash)
136
- end
137
-
138
- it 'should raise an ApiException if the JSON is unparsable' do
139
- stub_api_request(:get, 'SomeResource', { status: 200, body: 'invalid json' })
140
- expect {
141
- client.get('/SomeResource')
142
- }.to raise_exception(TflApi::Exceptions::ApiException)
143
- end
144
- end
145
-
146
- context 'when the resource requires authorisation' do
147
- before { stub_api_request(:get, 'SomeUnauthorisedResource', { status: 401 }) }
148
- subject { -> { client.get('/SomeUnauthorisedResource') } }
149
-
150
- it { is_expected.to raise_exception(TflApi::Exceptions::Unauthorized) }
151
- end
152
-
153
- context 'when the resource forbids access' do
154
- before { stub_api_request(:get, 'SomeForbiddenResource', { status: 403 }) }
155
- subject { -> { client.get('/SomeForbiddenResource') } }
156
-
157
- it { is_expected.to raise_exception(TflApi::Exceptions::Forbidden) }
158
- end
159
-
160
- context 'when the resource is not found' do
161
- before { stub_api_request(:get, 'SomeNotFoundResource', { status: 404 }) }
162
- subject { -> { client.get('/SomeNotFoundResource') } }
163
-
164
- it { is_expected.to raise_exception(TflApi::Exceptions::NotFound) }
165
- end
166
-
167
- context 'when the resource has malfunctioned' do
168
- before { stub_api_request(:get, 'SomeInternalErrorResource', { status: 500 }) }
169
- subject { -> { client.get('/SomeInternalErrorResource') } }
170
-
171
- it { is_expected.to raise_exception(TflApi::Exceptions::InternalServerError) }
172
- end
173
-
174
- context 'when the resource is unavailable' do
175
- before { stub_api_request(:get, 'SomeUnavailableResource', { status: 503 }) }
176
- subject { -> { client.get('/SomeUnavailableResource') } }
177
-
178
- it { is_expected.to raise_exception(TflApi::Exceptions::ServiceUnavailable) }
179
- end
180
-
181
- context 'when the resource errors unexpectedly' do
182
- before { stub_api_request(:get, 'SomeUnknownErrorResource', { status: 444 }) }
183
- subject { -> { client.get('/SomeUnknownErrorResource') } }
184
-
185
- it { is_expected.to raise_exception(TflApi::Exceptions::ApiException) }
186
- end
187
- end
188
-
189
- describe '#inspect' do
190
- let!(:client) { TflApi::Client.new(app_id: 12345, app_key: 6789) }
191
- subject { client.inspect }
192
-
193
- it { is_expected.to_not include('app_id=12345') }
194
- it { is_expected.to_not include('app_key=6789') }
195
- it { is_expected.to include('host=https://api.tfl.gov.uk') }
196
- it { is_expected.to include('log_level=1') }
197
- it { is_expected.to include('log_location=#<IO:<STDOUT>>') }
198
- end
199
- end
@@ -1,66 +0,0 @@
1
- #
2
- # Copyright (c) 2015 - 2017 Luke Hackett
3
- #
4
- # MIT License
5
- #
6
- # Permission is hereby granted, free of charge, to any person obtaining
7
- # a copy of this software and associated documentation files (the
8
- # "Software"), to deal in the Software without restriction, including
9
- # without limitation the rights to use, copy, modify, merge, publish,
10
- # distribute, sublicense, and/or sell copies of the Software, and to
11
- # permit persons to whom the Software is furnished to do so, subject to
12
- # the following conditions:
13
- #
14
- # The above copyright notice and this permission notice shall be
15
- # included in all copies or substantial portions of the Software.
16
- #
17
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
- # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
- # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
- # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
- # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
- # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
- # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
- #
25
-
26
- require_relative '../spec_helper'
27
-
28
- describe TflApi::Client::Cycle do
29
- let!(:client) { TflApi::Client.new(app_id: 123, app_key: 456) }
30
- let!(:cycle) { TflApi::Client::Cycle.new(client) }
31
- let(:sample_response) do
32
- [{
33
- 'id' => 'CS-1',
34
- 'label' => 'CS Route 1',
35
- 'labelShort' => 'CS-1',
36
- 'segmented' => true,
37
- 'geography' => {
38
- 'type' => 'LineString',
39
- 'coordinates' => [],
40
- 'crs' => {
41
- 'type' => 'name',
42
- 'properties' => {
43
- 'name' => 'ABC:1234'
44
- }
45
- }
46
- }
47
- }]
48
- end
49
-
50
- describe '#superhighways' do
51
- before { allow(client).to receive(:get).with('/CycleSuperhighway').and_return(sample_response) }
52
- subject { cycle.superhighways }
53
-
54
- it { is_expected.to be_an(Array) }
55
- it { is_expected.to eq(sample_response) }
56
- end
57
-
58
- describe '#superhighway' do
59
- before { allow(client).to receive(:get).with('/CycleSuperhighway/SOME_ID').and_return(sample_response.first) }
60
- subject { cycle.superhighway('SOME_ID') }
61
-
62
- it { is_expected.to be_an(Hash) }
63
- it { is_expected.to eq(sample_response.first) }
64
- end
65
-
66
- end
@@ -1,37 +0,0 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'tfl_api_client/version'
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = 'tfl_api_client'
8
- spec.version = TflApi::VERSION
9
- spec.author = 'Luke Hackett'
10
- spec.email = 'Luke.Hackett@live.co.uk'
11
- spec.homepage = 'https://github.com/LukeHackett/tfl_api_client'
12
- spec.license = 'MIT'
13
- spec.platform = Gem::Platform::RUBY
14
- spec.required_ruby_version = '>= 2.0'
15
- spec.summary = 'Transport for London API Client'
16
- spec.description = <<-eof
17
- This gem aims to provide a simple, programmatic ruby client that allows
18
- native ruby applications to seamlessly interact with the Transport for
19
- London's live APIs.
20
- eof
21
-
22
- spec.files = `git ls-files -z`.split("\x0")
23
- spec.require_paths = ['lib']
24
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
25
- spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
26
-
27
- spec.add_development_dependency 'bundler', '~> 1.14'
28
- spec.add_development_dependency 'rake', '~> 11.3'
29
- spec.add_development_dependency 'yard', '~> 0.8.7'
30
-
31
- spec.add_development_dependency 'rspec', '~> 3.5'
32
- spec.add_development_dependency 'fuubar', '~> 2.2'
33
- spec.add_development_dependency 'vcr', '~> 3.0'
34
- spec.add_development_dependency 'webmock', '~> 2.3'
35
- spec.add_development_dependency 'simplecov', '~> 0.12'
36
- spec.add_development_dependency 'coveralls', '~> 0.8.2'
37
- end