tfl_api_client 0.1.0

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 (34) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +37 -0
  3. data/.travis.yml +29 -0
  4. data/CHANGELOG.md +9 -0
  5. data/CONTRIBUTING.md +62 -0
  6. data/GETTING_STARTED.md +161 -0
  7. data/Gemfile +4 -0
  8. data/LICENSE +22 -0
  9. data/README.md +62 -0
  10. data/Rakefile +30 -0
  11. data/lib/tfl_api_client/bike_point.rb +105 -0
  12. data/lib/tfl_api_client/client.rb +235 -0
  13. data/lib/tfl_api_client/exceptions.rb +91 -0
  14. data/lib/tfl_api_client/version.rb +28 -0
  15. data/lib/tfl_api_client.rb +33 -0
  16. data/spec/cassettes/bike_point/authorised_client_location.yml +83 -0
  17. data/spec/cassettes/bike_point/authorised_client_locations.yml +8179 -0
  18. data/spec/cassettes/bike_point/authorised_client_locations_within_bounding_box.yml +402 -0
  19. data/spec/cassettes/bike_point/authorised_client_locations_within_locus.yml +106 -0
  20. data/spec/cassettes/bike_point/authorised_client_search.yml +80 -0
  21. data/spec/cassettes/bike_point/unauthorised_client_location.yml +50 -0
  22. data/spec/cassettes/bike_point/unauthorised_client_locations.yml +50 -0
  23. data/spec/cassettes/bike_point/unauthorised_client_locations_within_bounding_box.yml +50 -0
  24. data/spec/cassettes/bike_point/unauthorised_client_locations_within_locus.yml +50 -0
  25. data/spec/cassettes/bike_point/unauthorised_client_search.yml +50 -0
  26. data/spec/integration/bike_point_spec.rb +158 -0
  27. data/spec/spec_helper.rb +114 -0
  28. data/spec/support/coverage.rb +36 -0
  29. data/spec/support/helpers.rb +81 -0
  30. data/spec/support/vcr.rb +43 -0
  31. data/spec/unit/bike_point_spec.rb +87 -0
  32. data/spec/unit/client_spec.rb +199 -0
  33. data/tfl_api_client.gemspec +36 -0
  34. metadata +222 -0
@@ -0,0 +1,114 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
4
+ # this file to always be loaded, without a need to explicitly require it in any
5
+ # files.
6
+ #
7
+ # Given that it is always loaded, you are encouraged to keep this file as
8
+ # light-weight as possible. Requiring heavyweight dependencies from this file
9
+ # will add to the boot time of your test suite on EVERY test run, even for an
10
+ # individual file that may not need all of that loaded. Instead, consider making
11
+ # a separate helper file that requires the additional dependencies and performs
12
+ # the additional setup, and require it from the spec files that actually need
13
+ # it.
14
+
15
+ # Additional Test Imports
16
+ require 'webmock/rspec'
17
+ require_relative 'support/coverage'
18
+ require_relative 'support/vcr'
19
+ require_relative 'support/helpers'
20
+
21
+ # Load the Gem
22
+ require_relative '../lib/tfl_api_client'
23
+
24
+ # The `.rspec` file also contains a few flags that are not defaults but that
25
+ # users commonly want.
26
+ #
27
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
28
+ RSpec.configure do |config|
29
+ # rspec-expectations config goes here. You can use an alternate
30
+ # assertion/expectation library such as wrong or the stdlib/minitest
31
+ # assertions if you prefer.
32
+ config.expect_with :rspec do |expectations|
33
+ # This option will default to `true` in RSpec 4. It makes the `description`
34
+ # and `failure_message` of custom matchers include text for helper methods
35
+ # defined using `chain`, e.g.:
36
+ # be_bigger_than(2).and_smaller_than(4).description
37
+ # # => "be bigger than 2 and smaller than 4"
38
+ # ...rather than:
39
+ # # => "be bigger than 2"
40
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
41
+
42
+ # Disable should syntax
43
+ expectations.syntax = :expect
44
+ end
45
+
46
+ # rspec-mocks config goes here. You can use an alternate test double
47
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
48
+ config.mock_with :rspec do |mocks|
49
+ # Prevents you from mocking or stubbing a method that does not exist on
50
+ # a real object. This is generally recommended, and will default to
51
+ # `true` in RSpec 4.
52
+ mocks.verify_partial_doubles = true
53
+
54
+ # Disable should syntax
55
+ mocks.syntax = :expect
56
+ end
57
+
58
+ # Include test helpers
59
+ config.include Helpers
60
+
61
+ # These two settings work together to allow you to limit a spec run
62
+ # to individual examples or groups you care about by tagging them with
63
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
64
+ # get run.
65
+ # config.filter_run :focus
66
+ # config.run_all_when_everything_filtered = true
67
+
68
+ # Allows RSpec to persist some state between runs in order to support
69
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
70
+ # you configure your source control system to ignore this file.
71
+ # config.example_status_persistence_file_path = 'spec/examples.txt'
72
+
73
+ # Limits the available syntax to the non-monkey patched syntax that is
74
+ # recommended. For more details, see:
75
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
76
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
77
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
78
+ # config.disable_monkey_patching!
79
+
80
+ # This setting enables warnings. It's recommended, but in some cases may
81
+ # be too noisy due to issues in dependencies.
82
+ # config.warnings = true
83
+
84
+ # Many RSpec users commonly either run the entire suite or an individual
85
+ # file, and it's useful to allow more verbose output when running an
86
+ # individual spec file.
87
+ if config.files_to_run.one?
88
+ # Use the documentation formatter for detailed output,
89
+ # unless a formatter has already been configured
90
+ # (e.g. via a command-line flag).
91
+ # config.default_formatter = 'doc'
92
+ end
93
+
94
+ # Use Fuubar to handle pretty printing of the test suite
95
+ config.default_formatter = 'Fuubar'
96
+ config.color = true
97
+
98
+ # Print the 10 slowest examples and example groups at the
99
+ # end of the spec run, to help surface which specs are running
100
+ # particularly slow.
101
+ # config.profile_examples = 10
102
+
103
+ # Run specs in random order to surface order dependencies. If you find an
104
+ # order dependency and want to debug it, you can fix the order by providing
105
+ # the seed, which is printed after each run.
106
+ # --seed 1234
107
+ config.order = :random
108
+
109
+ # Seed global randomization in this process using the `--seed` CLI option.
110
+ # Setting this allows you to use `--seed` to deterministically reproduce
111
+ # test failures related to randomization by passing the same `--seed` value
112
+ # as the one that triggered the failure.
113
+ Kernel.srand config.seed
114
+ end
@@ -0,0 +1,36 @@
1
+ #
2
+ # Copyright (c) 2015 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 'simplecov'
27
+ require 'coveralls'
28
+
29
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
30
+ SimpleCov::Formatter::HTMLFormatter,
31
+ Coveralls::SimpleCov::Formatter
32
+ ]
33
+
34
+ SimpleCov.start do
35
+ add_filter 'spec'
36
+ end
@@ -0,0 +1,81 @@
1
+ #
2
+ # Copyright (c) 2015 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
@@ -0,0 +1,43 @@
1
+ #
2
+ # Copyright (c) 2015 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 'vcr'
27
+
28
+ VCR.configure do |config|
29
+ config.cassette_library_dir = File.expand_path('../../cassettes', __FILE__)
30
+ config.hook_into :webmock
31
+ config.configure_rspec_metadata!
32
+
33
+ # Replace the real Application ID and Application Key with some fake data
34
+ config.before_record do |interaction|
35
+ interaction.request.uri.gsub!(/app_id=([^&]+)/, 'app_id=TFL_APP_ID')
36
+ interaction.request.uri.gsub!(/app_key=([^&]+)/, 'app_key=TFL_APP_KEY')
37
+ end
38
+
39
+ # Prevent VCR from matching on Application ID and Application Key
40
+ config.default_cassette_options = {
41
+ match_requests_on: [:method, VCR.request_matchers.uri_without_param(:app_id, :app_key)]
42
+ }
43
+ end
@@ -0,0 +1,87 @@
1
+ #
2
+ # Copyright (c) 2015 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
@@ -0,0 +1,199 @@
1
+ #
2
+ # Copyright (c) 2015 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
@@ -0,0 +1,36 @@
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.summary = 'Transport for London API Client'
15
+ spec.description = <<-eof
16
+ This gem aims to provide a simple, programmatic ruby client that allows
17
+ native ruby applications to seamlessly interact with the Transport for
18
+ London's live APIs.
19
+ eof
20
+
21
+ spec.files = `git ls-files -z`.split("\x0")
22
+ spec.require_paths = ['lib']
23
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
24
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
25
+
26
+ spec.add_development_dependency 'bundler', '~> 1.10'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ spec.add_development_dependency 'yard', '~> 0.8.7'
29
+
30
+ spec.add_development_dependency 'rspec', '~> 3.3'
31
+ spec.add_development_dependency 'fuubar', '~> 2.0'
32
+ spec.add_development_dependency 'vcr', '~> 2.9'
33
+ spec.add_development_dependency 'webmock', '~> 1.21'
34
+ spec.add_development_dependency 'simplecov', '~> 0.10'
35
+ spec.add_development_dependency 'coveralls', '~> 0.8.2'
36
+ end