one_bus_away 0.0.2 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,210 @@
1
+ require 'one_bus_away/client'
2
+
3
+ RSpec.describe OneBusAway::Client do
4
+ let(:valid_client) do
5
+ OneBusAway::Client.new(
6
+ api_method: ['current-time']
7
+ )
8
+ end
9
+
10
+ let(:invalid_client) { OneBusAway::Client.new }
11
+
12
+ describe '#api_key' do
13
+ it 'to respond' do
14
+ expect(valid_client).to respond_to(:api_key)
15
+ end
16
+ it 'returns api_key' do
17
+ client = valid_client
18
+ client.api_key = 'aer-dkjfwif0d-slkjsdflkjs'
19
+ expect(client.api_key).to eq('aer-dkjfwif0d-slkjsdflkjs')
20
+ end
21
+
22
+ it 'calls apply_local_api_key' do
23
+ file_like_object = 'somekey'
24
+ allow(File).to receive(:exist?).and_return(true)
25
+ allow(File).to receive(:read).and_return(file_like_object)
26
+
27
+ expect(invalid_client.api_key).to eq('somekey')
28
+ end
29
+ end
30
+
31
+ describe '#apply_local_api_key' do
32
+ it 'responds' do
33
+ expect(invalid_client).to respond_to(:apply_local_api_key)
34
+ end
35
+
36
+ it 'fails if file does not exists' do
37
+ allow(File).to receive(:exist?).and_return(false)
38
+
39
+ expect { invalid_client.apply_local_api_key }
40
+ .to raise_error(RuntimeError, 'no API key provided. '\
41
+ 'Please ensure you have your api key'\
42
+ 'installed in here: ~/.one_bus_away'
43
+ )
44
+ end
45
+ it 'succeeds if file exists' do
46
+ file_like_object = '123-12321321-1234jsdkfjsd'
47
+ allow(File).to receive(:exist?).and_return(true)
48
+ allow(File).to receive(:read).and_return(file_like_object)
49
+
50
+ expect(invalid_client.apply_local_api_key).to eq(file_like_object)
51
+ end
52
+ end
53
+
54
+ describe '#api_method' do
55
+ it 'to respond' do
56
+ expect(valid_client).to respond_to(:api_method)
57
+ end
58
+ it 'returns api_method' do
59
+ client = valid_client
60
+ client.api_method = ['route-ids-for-agency']
61
+ expect(client.api_method).to eq(['route-ids-for-agency'])
62
+ end
63
+ end
64
+
65
+ describe '#build_path' do
66
+ it 'responds' do
67
+ expect(invalid_client).to respond_to(:build_path)
68
+ end
69
+ it 'formats correctly' do
70
+ client = valid_client
71
+ client.api_method = ['route-ids-for-agency', '1']
72
+
73
+ expect(client.build_path)
74
+ .to eq('/api/where/route-ids-for-agency/1.json')
75
+ end
76
+ end
77
+
78
+ describe '#parameters' do
79
+ it 'to respond' do
80
+ expect(valid_client).to respond_to(:parameters)
81
+ end
82
+ it 'returns parameters' do
83
+ client = valid_client
84
+ client.parameters = { somekey: 'someparameters' }
85
+
86
+ expect(valid_client.parameters)
87
+ .to eq(somekey: 'someparameters')
88
+ end
89
+ end
90
+
91
+ describe '#build_query' do
92
+ it 'responds' do
93
+ expect(invalid_client).to respond_to(:build_query)
94
+ end
95
+
96
+ it 'contains api_key if no parameters' do
97
+ client = valid_client
98
+ client.parameters = nil
99
+ client.api_key = 'thisissomekeyyyyy'
100
+
101
+ expect(client.build_query).to eq('key=thisissomekeyyyyy')
102
+ end
103
+ it 'contains all parameters if included' do
104
+ client = valid_client
105
+ client.api_key = 'thisissomekeyyyyy'
106
+ client.parameters = {
107
+ route: 'someroute',
108
+ stop: 'somestop'
109
+ }
110
+
111
+ expect(client.build_query)
112
+ .to eq('key=thisissomekeyyyyy&route=someroute&stop=somestop')
113
+ end
114
+ end
115
+
116
+ describe '.valid?' do
117
+ context 'with valid paremeters' do
118
+ it 'is truthy' do
119
+ expect(valid_client.valid?).to be_truthy
120
+ end
121
+ end
122
+
123
+ context 'without valid parameters' do
124
+ it 'is falsey' do
125
+ expect(invalid_client.valid?).to be_falsey
126
+ end
127
+ end
128
+ end
129
+
130
+ describe '#get' do
131
+ it 'responds' do
132
+ expect(invalid_client).to respond_to(:get)
133
+ end
134
+ it 'fails if url not built' do
135
+ client = invalid_client
136
+
137
+ expect { client.get }
138
+ .to raise_error(RuntimeError, 'url is not properly built')
139
+ end
140
+ end
141
+
142
+ describe '#base_url' do
143
+ it 'equals the correct string' do
144
+ expect(invalid_client.base_url)
145
+ .to eq('api.pugetsound.onebusaway.org')
146
+ end
147
+ end
148
+
149
+ describe '#build_url' do
150
+ it { expect(invalid_client).to respond_to(:build_url) }
151
+
152
+ it 'returns correct url without parameters' do
153
+ client = valid_client
154
+ client.api_key = 'somekey'
155
+ client.parameters = nil
156
+ client.api_method = ['current_time']
157
+
158
+ expect(valid_client.build_url)
159
+ .to eq('http://api.pugetsound.onebusaway.org/api/where/current_time.json?key=somekey')
160
+ end
161
+
162
+ it 'returns correct url without parameters' do
163
+ client = valid_client
164
+ client.api_key = 'somekey'
165
+ client.parameters = { route: '123532' }
166
+ client.api_method = ['arrivals-and-departures-for-stop', '1_75403']
167
+
168
+ expect(valid_client.build_url)
169
+ .to eq('http://api.pugetsound.onebusaway.org'\
170
+ '/api/where/arrivals-and-departures-for-stop/'\
171
+ '1_75403.json?key=somekey&route=123532'
172
+ )
173
+ end
174
+
175
+ it 'calls valid?' do
176
+ expect(valid_client).to receive(:valid?)
177
+ valid_client.build_url
178
+ end
179
+
180
+ it 'returns false with valid? false' do
181
+ client = invalid_client
182
+ client.build_url
183
+
184
+ expect(client.url).to be_nil
185
+ end
186
+ end
187
+
188
+ describe '#url' do
189
+ it { expect(invalid_client).to respond_to(:url) }
190
+ it 'is set when calling build_url' do
191
+ client = valid_client
192
+ client.build_url
193
+
194
+ expect(client.url).not_to be_nil
195
+ end
196
+ end
197
+
198
+ describe '#http_response' do
199
+ it { expect(invalid_client).to respond_to(:http_response) }
200
+ it 'is set when calling #build_url' do
201
+ VCR.use_cassette('one_bus_away/current-time') do
202
+ client = valid_client
203
+ client.build_url
204
+ client.get
205
+
206
+ expect(client.http_response).not_to be_nil
207
+ end
208
+ end
209
+ end
210
+ end
@@ -1,34 +1,131 @@
1
1
  require 'one_bus_away'
2
2
 
3
3
  RSpec.describe OneBusAway do
4
+ let(:one_bus_away) { OneBusAway.new }
5
+
4
6
  describe '.new' do
5
- it 'should raise error without arguements' do
6
- expect { OneBusAway.new() }.to raise_error
7
+ it 'responds' do
8
+ expect(OneBusAway).to respond_to(:new)
9
+ end
10
+
11
+ it 'has an accessor for parameters' do
12
+ one_bus_away.parameters = 'parameters'
13
+
14
+ expect(one_bus_away.parameters).to eq('parameters')
15
+ end
16
+
17
+ it 'has an accessor for api_method' do
18
+ one_bus_away.api_method = 'method'
19
+
20
+ expect(one_bus_away.api_method).to eq('method')
21
+ end
22
+
23
+ it 'has a reader client' do
24
+ expect(one_bus_away).to respond_to(:client)
25
+ end
26
+
27
+ it 'does not have a writer for client' do
28
+ expect { one_bus_away.client = '' }.to raise_error(NoMethodError)
29
+ end
30
+ end
31
+
32
+ describe '#assign_data' do
33
+ let(:valid_client) { OneBusAway.new }
34
+
35
+ it 'responds' do
36
+ expect(one_bus_away).to respond_to(:assign_data)
37
+ end
38
+
39
+ it 'assigns @data', :vcr do
40
+ one_bus_away.current_time
41
+
42
+ expect(one_bus_away.data.entry.time).to eq(1_445_727_779_101)
43
+ end
44
+ end
45
+
46
+ describe '#data' do
47
+ it { is_expected.to respond_to(:data) }
48
+ end
49
+
50
+ describe '#call_api', :vcr do
51
+ it { is_expected.to respond_to(:call_api) }
52
+
53
+ it 'sets @data' do
54
+ one_bus_away.current_time
55
+
56
+ expect(one_bus_away.data).to be_kind_of(RecursiveOpenStruct)
7
57
  end
8
-
9
- it 'should accept API_KEY as arguement' do
10
- expect { OneBusAway.new("somestring")}.not_to raise_error
58
+ end
59
+
60
+ describe '#current_time', :vcr do
61
+ let(:one_bus_away_client) { allow_any_instance_of(OneBusAway::Client) }
62
+
63
+ it 'responds' do
64
+ expect(one_bus_away).to respond_to(:current_time)
65
+ end
66
+
67
+ it 'creates @client' do
68
+ one_bus_away.current_time
69
+
70
+ expect(one_bus_away.client).to be_a(OneBusAway::Client)
71
+ end
72
+
73
+ it 'fails if .build_url doesnt validate' do
74
+ one_bus_away_client.to receive(:valid?).and_return(false)
75
+ allow_any_instance_of(OneBusAway)
76
+ .to receive(:assign_data)
77
+ .and_return(true)
78
+
79
+ expect { one_bus_away.current_time }
80
+ .to raise_error(RuntimeError, 'url is not properly built')
81
+ end
82
+
83
+ it 'sets http_response' do
84
+ one_bus_away.current_time
85
+
86
+ expect(one_bus_away.client.http_response.code).to eq(200)
11
87
  end
12
88
  end
13
-
14
- describe '#current_time' do
15
- let(:one_bus_away) { OneBusAway.new("6a1c72f7-6ec4-4522-bf33-3698b3ad86c2") }
16
- it 'returns true' do
17
- expect { one_bus_away.current_time }.not_to raise_error
89
+
90
+ describe '#arrivals-and-departures-for-stop', :vcr do
91
+ let(:one_bus_away_client) { allow_any_instance_of(OneBusAway::Client) }
92
+
93
+ it 'responds with 1 arguments' do
94
+ expect(one_bus_away)
95
+ .to respond_to(:arrivals_and_departures_for_stop)
96
+ .with(1).argument
97
+ end
98
+
99
+ it 'creates @client' do
100
+ one_bus_away.arrivals_and_departures_for_stop(1_234_567)
101
+
102
+ expect(one_bus_away.client).to be_a(OneBusAway::Client)
103
+ end
104
+
105
+ it 'sets http_response' do
106
+ one_bus_away.arrivals_and_departures_for_stop(19_360)
107
+
108
+ expect(one_bus_away.client.http_response.code).to eq(200)
18
109
  end
19
110
  end
20
-
21
- describe '#arrivals_and_departures_for_stop' do
22
- let(:one_bus_away) { OneBusAway.new("6a1c72f7-6ec4-4522-bf33-3698b3ad86c2") }
23
- it 'returns true' do
24
- expect { one_bus_away.arrivals_and_departures_for_stop(18145, "40", 10) }.not_to raise_error
111
+
112
+ describe '#filter_by_route', :vcr do
113
+ it { is_expected.to respond_to(:filter_by_route) }
114
+
115
+ it 'outputs array' do
116
+ one_bus_away.arrivals_and_departures_for_stop(19_360)
117
+
118
+ expect(one_bus_away.filter_by_route).to be_kind_of(Array)
25
119
  end
26
120
  end
27
-
28
- describe '#valid_stop?' do
29
- let(:one_bus_away) { OneBusAway.new("6a1c72f7-6ec4-4522-bf33-3698b3ad86c2") }
30
- it 'returns true' do
31
- expect { one_bus_away.valid_stop?(18145) }.not_to raise_error
121
+
122
+ describe '#get_location', :vcr do
123
+ it { expect(OneBusAway.new).to respond_to(:get_location) }
124
+ it 'returns the latitude and longitude of a given address in Seattle' do
125
+ address = '400 Broad St'
126
+ location = one_bus_away.get_location(address)
127
+
128
+ expect(location).to eq([-122.3491348, 47.6205379])
32
129
  end
33
130
  end
34
- end
131
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,91 +1,26 @@
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
- # The `.rspec` file also contains a few flags that are not defaults but that
16
- # users commonly want.
17
- #
18
- # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
1
+ require 'simplecov'
2
+ require 'webmock/rspec'
3
+ require 'vcr'
4
+
5
+ VCR.configure do |c|
6
+ c.cassette_library_dir = 'spec/vcr'
7
+ c.hook_into :webmock
8
+ c.configure_rspec_metadata!
9
+ end
10
+ SimpleCov.start
11
+ WebMock.disable_net_connect!(allow_localhost: true)
19
12
  RSpec.configure do |config|
20
- # rspec-expectations config goes here. You can use an alternate
21
- # assertion/expectation library such as wrong or the stdlib/minitest
22
- # assertions if you prefer.
23
13
  config.expect_with :rspec do |expectations|
24
- # This option will default to `true` in RSpec 4. It makes the `description`
25
- # and `failure_message` of custom matchers include text for helper methods
26
- # defined using `chain`, e.g.:
27
- # be_bigger_than(2).and_smaller_than(4).description
28
- # # => "be bigger than 2 and smaller than 4"
29
- # ...rather than:
30
- # # => "be bigger than 2"
31
14
  expectations.include_chain_clauses_in_custom_matcher_descriptions = true
32
15
  end
33
16
 
34
- # rspec-mocks config goes here. You can use an alternate test double
35
- # library (such as bogus or mocha) by changing the `mock_with` option here.
36
17
  config.mock_with :rspec do |mocks|
37
- # Prevents you from mocking or stubbing a method that does not exist on
38
- # a real object. This is generally recommended, and will default to
39
- # `true` in RSpec 4.
40
18
  mocks.verify_partial_doubles = true
41
19
  end
42
20
 
43
- # The settings below are suggested to provide a good initial experience
44
- # with RSpec, but feel free to customize to your heart's content.
45
- =begin
46
- # These two settings work together to allow you to limit a spec run
47
- # to individual examples or groups you care about by tagging them with
48
- # `:focus` metadata. When nothing is tagged with `:focus`, all examples
49
- # get run.
50
- config.filter_run :focus
51
- config.run_all_when_everything_filtered = true
52
-
53
- # Limits the available syntax to the non-monkey patched syntax that is
54
- # recommended. For more details, see:
55
- # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
56
- # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
57
- # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
58
21
  config.disable_monkey_patching!
59
22
 
60
- # This setting enables warnings. It's recommended, but in some cases may
61
- # be too noisy due to issues in dependencies.
62
- config.warnings = true
63
-
64
- # Many RSpec users commonly either run the entire suite or an individual
65
- # file, and it's useful to allow more verbose output when running an
66
- # individual spec file.
67
- if config.files_to_run.one?
68
- # Use the documentation formatter for detailed output,
69
- # unless a formatter has already been configured
70
- # (e.g. via a command-line flag).
71
- config.default_formatter = 'doc'
72
- end
73
-
74
- # Print the 10 slowest examples and example groups at the
75
- # end of the spec run, to help surface which specs are running
76
- # particularly slow.
77
- config.profile_examples = 10
23
+ config.warnings = false
78
24
 
79
- # Run specs in random order to surface order dependencies. If you find an
80
- # order dependency and want to debug it, you can fix the order by providing
81
- # the seed, which is printed after each run.
82
- # --seed 1234
83
25
  config.order = :random
84
-
85
- # Seed global randomization in this process using the `--seed` CLI option.
86
- # Setting this allows you to use `--seed` to deterministically reproduce
87
- # test failures related to randomization by passing the same `--seed` value
88
- # as the one that triggered the failure.
89
- Kernel.srand config.seed
90
- =end
91
26
  end