google_distance_matrix 0.4.0 → 0.6.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.editorconfig +16 -0
- data/.rubocop.yml +6 -0
- data/.ruby-version +1 -1
- data/.travis.yml +4 -6
- data/CHANGELOG.md +40 -0
- data/Gemfile +2 -0
- data/README.md +0 -3
- data/Rakefile +9 -4
- data/google_distance_matrix.gemspec +21 -19
- data/lib/google_distance_matrix.rb +25 -23
- data/lib/google_distance_matrix/client.rb +32 -18
- data/lib/google_distance_matrix/client_cache.rb +9 -3
- data/lib/google_distance_matrix/configuration.rb +39 -24
- data/lib/google_distance_matrix/errors.rb +6 -3
- data/lib/google_distance_matrix/log_subscriber.rb +14 -14
- data/lib/google_distance_matrix/logger.rb +7 -5
- data/lib/google_distance_matrix/matrix.rb +45 -22
- data/lib/google_distance_matrix/place.rb +37 -28
- data/lib/google_distance_matrix/places.rb +5 -4
- data/lib/google_distance_matrix/polyline_encoder.rb +2 -2
- data/lib/google_distance_matrix/polyline_encoder/delta.rb +4 -2
- data/lib/google_distance_matrix/polyline_encoder/value_encoder.rb +13 -5
- data/lib/google_distance_matrix/railtie.rb +4 -1
- data/lib/google_distance_matrix/route.rb +22 -15
- data/lib/google_distance_matrix/routes_finder.rb +27 -29
- data/lib/google_distance_matrix/url_builder.rb +44 -16
- data/lib/google_distance_matrix/url_builder/polyline_encoder_buffer.rb +3 -0
- data/lib/google_distance_matrix/version.rb +3 -1
- data/spec/lib/google_distance_matrix/client_cache_spec.rb +27 -11
- data/spec/lib/google_distance_matrix/client_spec.rb +40 -30
- data/spec/lib/google_distance_matrix/configuration_spec.rb +36 -24
- data/spec/lib/google_distance_matrix/log_subscriber_spec.rb +13 -44
- data/spec/lib/google_distance_matrix/logger_spec.rb +16 -13
- data/spec/lib/google_distance_matrix/matrix_spec.rb +90 -57
- data/spec/lib/google_distance_matrix/place_spec.rb +38 -25
- data/spec/lib/google_distance_matrix/places_spec.rb +29 -28
- data/spec/lib/google_distance_matrix/polyline_encoder/delta_spec.rb +5 -3
- data/spec/lib/google_distance_matrix/polyline_encoder_spec.rb +7 -2
- data/spec/lib/google_distance_matrix/route_spec.rb +11 -9
- data/spec/lib/google_distance_matrix/routes_finder_spec.rb +124 -81
- data/spec/lib/google_distance_matrix/url_builder_spec.rb +97 -48
- data/spec/spec_helper.rb +3 -1
- metadata +46 -18
@@ -1,11 +1,13 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
2
4
|
|
3
5
|
describe GoogleDistanceMatrix::Configuration do
|
4
6
|
include Shoulda::Matchers::ActiveModel
|
5
7
|
|
6
8
|
subject { described_class.new }
|
7
9
|
|
8
|
-
describe
|
10
|
+
describe 'Validations' do
|
9
11
|
describe 'departure_time' do
|
10
12
|
it 'is valid with a timestamp' do
|
11
13
|
subject.departure_time = Time.now.to_i
|
@@ -46,28 +48,32 @@ describe GoogleDistanceMatrix::Configuration do
|
|
46
48
|
end
|
47
49
|
end
|
48
50
|
|
49
|
-
it { should validate_inclusion_of(:mode).in_array([
|
51
|
+
it { should validate_inclusion_of(:mode).in_array(%w[driving walking bicycling transit]) }
|
50
52
|
it { should allow_value(nil).for(:mode) }
|
51
53
|
|
52
|
-
it { should validate_inclusion_of(:avoid).in_array([
|
54
|
+
it { should validate_inclusion_of(:avoid).in_array(%w[tolls highways ferries indoor]) }
|
53
55
|
it { should allow_value(nil).for(:avoid) }
|
54
56
|
|
55
|
-
it { should validate_inclusion_of(:units).in_array([
|
57
|
+
it { should validate_inclusion_of(:units).in_array(%w[metric imperial]) }
|
56
58
|
it { should allow_value(nil).for(:units) }
|
57
59
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
it {
|
62
|
-
|
63
|
-
|
60
|
+
it { should validate_inclusion_of(:protocol).in_array(%w[http https]) }
|
61
|
+
|
62
|
+
it { should validate_inclusion_of(:transit_mode).in_array(%w[bus subway train tram rail]) }
|
63
|
+
it {
|
64
|
+
should validate_inclusion_of(
|
65
|
+
:transit_routing_preference
|
66
|
+
).in_array(%w[less_walking fewer_transfers])
|
67
|
+
}
|
68
|
+
it {
|
69
|
+
should validate_inclusion_of(:traffic_model).in_array(%w[best_guess pessimistic optimistic])
|
70
|
+
}
|
64
71
|
end
|
65
72
|
|
66
|
-
|
67
|
-
|
68
|
-
it { expect(subject.mode).to eq "driving" }
|
73
|
+
describe 'defaults' do
|
74
|
+
it { expect(subject.mode).to eq 'driving' }
|
69
75
|
it { expect(subject.avoid).to be_nil }
|
70
|
-
it { expect(subject.units).to eq
|
76
|
+
it { expect(subject.units).to eq 'metric' }
|
71
77
|
it { expect(subject.lat_lng_scale).to eq 5 }
|
72
78
|
it { expect(subject.use_encoded_polylines).to eq false }
|
73
79
|
it { expect(subject.protocol).to eq 'https' }
|
@@ -84,13 +90,19 @@ describe GoogleDistanceMatrix::Configuration do
|
|
84
90
|
|
85
91
|
it { expect(subject.logger).to be_nil }
|
86
92
|
it { expect(subject.cache).to be_nil }
|
87
|
-
end
|
88
93
|
|
94
|
+
# rubocop:disable Metrics/LineLength
|
95
|
+
it 'has a default expected cache_key_transform' do
|
96
|
+
key = subject.cache_key_transform.call('foo')
|
97
|
+
expect(key).to eq 'f7fbba6e0636f890e56fbbf3283e524c6fa3204ae298382d624741d0dc6638326e282c41be5e4254d8820772c5518a2c5a8c0c7f7eda19594a7eb539453e1ed7'
|
98
|
+
end
|
99
|
+
# rubocop:enable Metrics/LineLength
|
100
|
+
end
|
89
101
|
|
90
|
-
describe
|
102
|
+
describe '#to_param' do
|
91
103
|
described_class::ATTRIBUTES.each do |attr|
|
92
104
|
it "includes #{attr}" do
|
93
|
-
subject[attr] =
|
105
|
+
subject[attr] = 'foo'
|
94
106
|
expect(subject.to_param[attr]).to eq subject.public_send(attr)
|
95
107
|
end
|
96
108
|
|
@@ -108,14 +120,14 @@ describe GoogleDistanceMatrix::Configuration do
|
|
108
120
|
end
|
109
121
|
end
|
110
122
|
|
111
|
-
it
|
112
|
-
subject.google_business_api_client_id =
|
113
|
-
expect(subject.to_param['client']).to eq
|
123
|
+
it 'includes client if google_business_api_client_id has been set' do
|
124
|
+
subject.google_business_api_client_id = '123'
|
125
|
+
expect(subject.to_param['client']).to eq '123'
|
114
126
|
end
|
115
127
|
|
116
|
-
it
|
117
|
-
subject.google_api_key =
|
118
|
-
expect(subject.to_param['key']).to eq(
|
128
|
+
it 'includes key if google_api_key has been set' do
|
129
|
+
subject.google_api_key = '12345'
|
130
|
+
expect(subject.to_param['key']).to eq('12345')
|
119
131
|
end
|
120
132
|
end
|
121
133
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
module GoogleDistanceMatrix
|
@@ -9,22 +11,23 @@ module GoogleDistanceMatrix
|
|
9
11
|
@logged = []
|
10
12
|
end
|
11
13
|
|
12
|
-
def info(msg,
|
14
|
+
def info(msg, _tag)
|
13
15
|
@logged << msg
|
14
16
|
end
|
15
17
|
|
16
18
|
def error(msg)
|
17
|
-
|
19
|
+
raise msg
|
18
20
|
end
|
19
21
|
end
|
20
22
|
|
21
23
|
# Little helper to clean up examples
|
22
24
|
def notify(instrumentation)
|
23
|
-
ActiveSupport::Notifications.instrument
|
25
|
+
ActiveSupport::Notifications.instrument(
|
26
|
+
'client_request_matrix_data.google_distance_matrix', instrumentation
|
27
|
+
) do
|
24
28
|
end
|
25
29
|
end
|
26
30
|
|
27
|
-
|
28
31
|
let(:mock_logger) { MockLogger.new }
|
29
32
|
let(:config) { Configuration.new }
|
30
33
|
|
@@ -32,57 +35,23 @@ module GoogleDistanceMatrix
|
|
32
35
|
before do
|
33
36
|
@old_subscribers = LogSubscriber.subscribers.dup
|
34
37
|
LogSubscriber.subscribers.clear
|
35
|
-
LogSubscriber.attach_to
|
38
|
+
LogSubscriber.attach_to 'google_distance_matrix',
|
39
|
+
LogSubscriber.new(logger: mock_logger, config: config)
|
36
40
|
end
|
37
41
|
|
38
42
|
after do
|
39
43
|
@old_subscribers.each do |subscriber|
|
40
|
-
LogSubscriber.attach_to
|
44
|
+
LogSubscriber.attach_to 'google_distance_matrix', subscriber
|
41
45
|
end
|
42
46
|
end
|
43
47
|
|
44
|
-
|
45
|
-
it "logs the url and elements" do
|
48
|
+
it 'logs the url and elements' do
|
46
49
|
url = 'https://example.com'
|
47
|
-
instrumentation = {
|
50
|
+
instrumentation = { filtered_url: url, elements: 0 }
|
48
51
|
|
49
52
|
expect { notify instrumentation }.to change(mock_logger.logged, :length).from(0).to 1
|
50
53
|
|
51
|
-
expect(mock_logger.logged.first).to include
|
52
|
-
end
|
53
|
-
|
54
|
-
describe "filtering of logged url" do
|
55
|
-
it "filters nothing if config has no keys to be filtered" do
|
56
|
-
config.filter_parameters_in_logged_url.clear
|
57
|
-
|
58
|
-
instrumentation = {url: 'https://example.com/?foo=bar&sensitive=secret'}
|
59
|
-
notify instrumentation
|
60
|
-
|
61
|
-
expect(mock_logger.logged.first).to include "https://example.com/?foo=bar&sensitive=secret"
|
62
|
-
end
|
63
|
-
|
64
|
-
it "filters sensitive GET param if config has it in list of params to filter" do
|
65
|
-
config.filter_parameters_in_logged_url << 'sensitive'
|
66
|
-
|
67
|
-
instrumentation = {url: 'https://example.com/?foo=bar&sensitive=secret'}
|
68
|
-
notify instrumentation
|
69
|
-
|
70
|
-
expect(mock_logger.logged.first).to include "https://example.com/?foo=bar&sensitive=[FILTERED]"
|
71
|
-
end
|
72
|
-
|
73
|
-
it "filters key and signature as defaul from configuration" do
|
74
|
-
instrumentation = {url: 'https://example.com/?key=bar&signature=secret&other=foo'}
|
75
|
-
notify instrumentation
|
76
|
-
|
77
|
-
expect(mock_logger.logged.first).to include "https://example.com/?key=[FILTERED]&signature=[FILTERED]&other=foo"
|
78
|
-
end
|
79
|
-
|
80
|
-
it "filters all appearances of a param" do
|
81
|
-
instrumentation = {url: 'https://example.com/?key=bar&key=secret&other=foo'}
|
82
|
-
notify instrumentation
|
83
|
-
|
84
|
-
expect(mock_logger.logged.first).to include "https://example.com/?key=[FILTERED]&key=[FILTERED]&other=foo"
|
85
|
-
end
|
54
|
+
expect(mock_logger.logged.first).to include '(elements: 0) GET https://example.com'
|
86
55
|
end
|
87
56
|
end
|
88
57
|
end
|
@@ -1,36 +1,39 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
2
4
|
|
3
5
|
describe GoogleDistanceMatrix::Logger do
|
4
|
-
context
|
6
|
+
context 'without a logger backend' do
|
5
7
|
subject { described_class.new }
|
6
8
|
|
7
9
|
described_class::LEVELS.each do |level|
|
8
10
|
it "logging #{level} does not fail" do
|
9
|
-
subject.public_send level,
|
11
|
+
subject.public_send level, 'log msg'
|
10
12
|
end
|
11
13
|
end
|
12
14
|
end
|
13
15
|
|
14
|
-
context
|
16
|
+
context 'with a logger backend' do
|
15
17
|
let(:backend) { double }
|
16
18
|
|
17
19
|
subject { described_class.new backend }
|
18
20
|
|
19
21
|
described_class::LEVELS.each do |level|
|
20
22
|
describe level do
|
21
|
-
it
|
22
|
-
expect(backend).to receive(level).with(
|
23
|
-
subject.public_send level,
|
23
|
+
it 'sends log message to the backend' do
|
24
|
+
expect(backend).to receive(level).with('[google_distance_matrix] log msg')
|
25
|
+
subject.public_send level, 'log msg'
|
24
26
|
end
|
25
27
|
|
26
|
-
it
|
27
|
-
expect(backend).to receive(level).with(
|
28
|
-
subject.public_send level,
|
28
|
+
it 'supports sending in a tag' do
|
29
|
+
expect(backend).to receive(level).with('[google_distance_matrix] [client] log msg')
|
30
|
+
subject.public_send level, 'log msg', tag: :client
|
29
31
|
end
|
30
32
|
|
31
|
-
it
|
32
|
-
expect(backend).to receive(level)
|
33
|
-
|
33
|
+
it 'supports sending in multiple tags' do
|
34
|
+
expect(backend).to receive(level)
|
35
|
+
.with('[google_distance_matrix] [client] [request] log msg')
|
36
|
+
subject.public_send level, 'log msg', tag: %w[client request]
|
34
37
|
end
|
35
38
|
end
|
36
39
|
end
|
@@ -1,14 +1,16 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
2
4
|
|
3
5
|
describe GoogleDistanceMatrix::Matrix do
|
4
|
-
let(:origin_1) { GoogleDistanceMatrix::Place.new address:
|
5
|
-
let(:origin_2) { GoogleDistanceMatrix::Place.new address:
|
6
|
+
let(:origin_1) { GoogleDistanceMatrix::Place.new address: 'Karl Johans gate, Oslo' }
|
7
|
+
let(:origin_2) { GoogleDistanceMatrix::Place.new address: 'Askerveien 1, Asker' }
|
6
8
|
|
7
|
-
let(:destination_1) { GoogleDistanceMatrix::Place.new address:
|
8
|
-
let(:destination_2) { GoogleDistanceMatrix::Place.new address:
|
9
|
+
let(:destination_1) { GoogleDistanceMatrix::Place.new address: 'Drammensveien 1, Oslo' }
|
10
|
+
let(:destination_2) { GoogleDistanceMatrix::Place.new address: 'Skjellestadhagen, Heggedal' }
|
9
11
|
|
10
12
|
let(:url_builder) { GoogleDistanceMatrix::UrlBuilder.new subject }
|
11
|
-
let(:url) { url_builder.
|
13
|
+
let(:url) { url_builder.sensitive_url }
|
12
14
|
|
13
15
|
subject do
|
14
16
|
described_class.new(
|
@@ -17,24 +19,24 @@ describe GoogleDistanceMatrix::Matrix do
|
|
17
19
|
)
|
18
20
|
end
|
19
21
|
|
20
|
-
describe
|
21
|
-
it
|
22
|
+
describe '#initialize' do
|
23
|
+
it 'takes a list of origins' do
|
22
24
|
matrix = described_class.new origins: [origin_1, origin_2]
|
23
25
|
expect(matrix.origins).to include origin_1, origin_2
|
24
26
|
end
|
25
27
|
|
26
|
-
it
|
28
|
+
it 'takes a list of destinations' do
|
27
29
|
matrix = described_class.new destinations: [destination_1, destination_2]
|
28
30
|
expect(matrix.destinations).to include destination_1, destination_2
|
29
31
|
end
|
30
32
|
|
31
|
-
it
|
33
|
+
it 'has a default configuration' do
|
32
34
|
expect(subject.configuration).to be_present
|
33
35
|
end
|
34
36
|
end
|
35
37
|
|
36
|
-
describe
|
37
|
-
it
|
38
|
+
describe '#configuration' do
|
39
|
+
it 'is by default set from default_configuration' do
|
38
40
|
config = double
|
39
41
|
allow(config).to receive(:dup).and_return config
|
40
42
|
expect(GoogleDistanceMatrix).to receive(:default_configuration).and_return config
|
@@ -43,47 +45,58 @@ describe GoogleDistanceMatrix::Matrix do
|
|
43
45
|
end
|
44
46
|
|
45
47
|
it "has it's own configuration" do
|
46
|
-
expect
|
48
|
+
expect do
|
47
49
|
subject.configure { |c| c.mode = !GoogleDistanceMatrix.default_configuration.mode }
|
48
|
-
|
50
|
+
end.to_not change(GoogleDistanceMatrix.default_configuration, :mode)
|
49
51
|
end
|
50
52
|
|
51
|
-
it
|
52
|
-
expect
|
53
|
+
it 'has a configurable configuration :-)' do
|
54
|
+
expect do
|
53
55
|
subject.configure { |c| c.mode = !GoogleDistanceMatrix.default_configuration.mode }
|
54
|
-
|
56
|
+
end.to change(
|
57
|
+
subject.configuration, :mode
|
58
|
+
).to !GoogleDistanceMatrix.default_configuration.mode
|
55
59
|
end
|
56
60
|
end
|
57
61
|
|
58
62
|
%w[origins destinations].each do |attr|
|
59
|
-
let(:place) { GoogleDistanceMatrix::Place.new address:
|
63
|
+
let(:place) { GoogleDistanceMatrix::Place.new address: 'My street' }
|
64
|
+
let(:place_2) { GoogleDistanceMatrix::Place.new address: 'Some other street' }
|
60
65
|
|
61
66
|
describe "##{attr}" do
|
62
|
-
it
|
67
|
+
it 'can receive places' do
|
63
68
|
subject.public_send(attr) << place
|
64
69
|
expect(subject.public_send(attr)).to include place
|
65
70
|
end
|
66
71
|
|
67
|
-
it
|
68
|
-
expect
|
72
|
+
it 'does not same place twice' do
|
73
|
+
expect do
|
69
74
|
2.times { subject.public_send(attr) << place }
|
70
|
-
|
75
|
+
end.to change(subject.public_send(attr), :length).by 1
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'updates the url when adding places' do
|
79
|
+
subject.public_send(attr) << place
|
80
|
+
expect(subject.sensitive_url).to include CGI.escape('My street')
|
81
|
+
|
82
|
+
subject.public_send(attr) << place_2
|
83
|
+
expect(subject.sensitive_url).to include CGI.escape('Some other street')
|
71
84
|
end
|
72
85
|
end
|
73
86
|
end
|
74
87
|
|
75
88
|
%w[
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
89
|
+
route_for
|
90
|
+
route_for!
|
91
|
+
routes_for
|
92
|
+
routes_for!
|
93
|
+
shortest_route_by_duration_to
|
94
|
+
shortest_route_by_duration_to!
|
95
|
+
shortest_route_by_distance_to
|
96
|
+
shortest_route_by_distance_to!
|
97
|
+
shortest_route_by_duration_in_traffic_to
|
98
|
+
shortest_route_by_duration_in_traffic_to!
|
99
|
+
].each do |method|
|
87
100
|
it "delegates #{method} to routes_finder" do
|
88
101
|
finder = double
|
89
102
|
result = double
|
@@ -95,8 +108,8 @@ describe GoogleDistanceMatrix::Matrix do
|
|
95
108
|
end
|
96
109
|
end
|
97
110
|
|
98
|
-
describe
|
99
|
-
it
|
111
|
+
describe 'making API requests', :request_recordings do
|
112
|
+
it 'loads correctly API response data in to route objects' do
|
100
113
|
stub_request(:get, url).to_return body: recorded_request_for(:success)
|
101
114
|
expect(subject.data[0][0].distance_text).to eq '2.0 km'
|
102
115
|
expect(subject.data[0][0].distance_in_meters).to eq 2032
|
@@ -104,7 +117,7 @@ describe GoogleDistanceMatrix::Matrix do
|
|
104
117
|
expect(subject.data[0][0].duration_in_seconds).to eq 367
|
105
118
|
end
|
106
119
|
|
107
|
-
it
|
120
|
+
it 'loads correctly API response data in to route objects when it includes in traffic data' do
|
108
121
|
stub_request(:get, url).to_return body: recorded_request_for(:success_with_in_traffic)
|
109
122
|
expect(subject.data[0][0].distance_text).to eq '1.8 km'
|
110
123
|
expect(subject.data[0][0].distance_in_meters).to eq 1752
|
@@ -114,8 +127,8 @@ describe GoogleDistanceMatrix::Matrix do
|
|
114
127
|
expect(subject.data[0][0].duration_in_traffic_in_seconds).to eq 405
|
115
128
|
end
|
116
129
|
|
117
|
-
context
|
118
|
-
it
|
130
|
+
context 'no cache' do
|
131
|
+
it 'makes multiple requests to same url' do
|
119
132
|
stub = stub_request(:get, url).to_return body: recorded_request_for(:success)
|
120
133
|
subject.data
|
121
134
|
subject.reset!
|
@@ -125,15 +138,16 @@ describe GoogleDistanceMatrix::Matrix do
|
|
125
138
|
end
|
126
139
|
end
|
127
140
|
|
128
|
-
context
|
141
|
+
context 'with cache' do
|
129
142
|
before do
|
130
143
|
subject.configure do |config|
|
131
144
|
config.cache = ActiveSupport::Cache.lookup_store :memory_store
|
132
145
|
end
|
133
146
|
end
|
134
147
|
|
135
|
-
it
|
148
|
+
it 'makes one requests to same url' do
|
136
149
|
stub = stub_request(:get, url).to_return body: recorded_request_for(:success)
|
150
|
+
|
137
151
|
subject.data
|
138
152
|
subject.reset!
|
139
153
|
subject.data
|
@@ -141,7 +155,22 @@ describe GoogleDistanceMatrix::Matrix do
|
|
141
155
|
expect(stub).to have_been_requested.once
|
142
156
|
end
|
143
157
|
|
144
|
-
it
|
158
|
+
it 'makes one request when filtered params to same url' do
|
159
|
+
was = GoogleDistanceMatrix.default_configuration.filter_parameters_in_logged_url
|
160
|
+
GoogleDistanceMatrix.default_configuration.filter_parameters_in_logged_url = ['origins']
|
161
|
+
|
162
|
+
stub = stub_request(:get, url).to_return body: recorded_request_for(:success)
|
163
|
+
|
164
|
+
subject.data
|
165
|
+
subject.reset!
|
166
|
+
subject.data
|
167
|
+
|
168
|
+
expect(stub).to have_been_requested.once
|
169
|
+
|
170
|
+
GoogleDistanceMatrix.default_configuration.filter_parameters_in_logged_url = was
|
171
|
+
end
|
172
|
+
|
173
|
+
it 'clears the cache key on reload' do
|
145
174
|
stub = stub_request(:get, url).to_return body: recorded_request_for(:success)
|
146
175
|
subject.data
|
147
176
|
subject.reload
|
@@ -152,30 +181,32 @@ describe GoogleDistanceMatrix::Matrix do
|
|
152
181
|
end
|
153
182
|
end
|
154
183
|
|
155
|
-
describe
|
156
|
-
context
|
157
|
-
let!(:api_request_stub)
|
184
|
+
describe '#data', :request_recordings do
|
185
|
+
context 'success' do
|
186
|
+
let!(:api_request_stub) do
|
187
|
+
stub_request(:get, url).to_return body: recorded_request_for(:success)
|
188
|
+
end
|
158
189
|
|
159
190
|
it "loads from Google's API" do
|
160
191
|
subject.data
|
161
192
|
expect(api_request_stub).to have_been_requested
|
162
193
|
end
|
163
194
|
|
164
|
-
it
|
195
|
+
it 'does not load twice' do
|
165
196
|
2.times { subject.data }
|
166
197
|
expect(api_request_stub).to have_been_requested
|
167
198
|
end
|
168
199
|
|
169
|
-
it
|
200
|
+
it 'contains one row' do
|
170
201
|
expect(subject.data.length).to eq 2
|
171
202
|
end
|
172
203
|
|
173
|
-
it
|
204
|
+
it 'contains two columns each row' do
|
174
205
|
expect(subject.data[0].length).to eq 2
|
175
206
|
expect(subject.data[1].length).to eq 2
|
176
207
|
end
|
177
208
|
|
178
|
-
it
|
209
|
+
it 'assigns correct origin on routes in the data' do
|
179
210
|
expect(subject.data[0][0].origin).to eq origin_1
|
180
211
|
expect(subject.data[0][1].origin).to eq origin_1
|
181
212
|
|
@@ -183,7 +214,7 @@ describe GoogleDistanceMatrix::Matrix do
|
|
183
214
|
expect(subject.data[1][1].origin).to eq origin_2
|
184
215
|
end
|
185
216
|
|
186
|
-
it
|
217
|
+
it 'assigns correct destination on routes in the data' do
|
187
218
|
expect(subject.data[0][0].destination).to eq destination_1
|
188
219
|
expect(subject.data[0][1].destination).to eq destination_2
|
189
220
|
|
@@ -192,36 +223,38 @@ describe GoogleDistanceMatrix::Matrix do
|
|
192
223
|
end
|
193
224
|
end
|
194
225
|
|
195
|
-
context
|
196
|
-
let!(:api_request_stub)
|
226
|
+
context 'some elements is not OK' do
|
227
|
+
let!(:api_request_stub) do
|
228
|
+
stub_request(:get, url).to_return body: recorded_request_for(:zero_results)
|
229
|
+
end
|
197
230
|
|
198
231
|
it "loads from Google's API" do
|
199
232
|
subject.data
|
200
233
|
expect(api_request_stub).to have_been_requested
|
201
234
|
end
|
202
235
|
|
203
|
-
it
|
236
|
+
it 'adds loaded route with errors correctly' do
|
204
237
|
route = subject.data[0][1]
|
205
238
|
|
206
|
-
expect(route.status).to eq
|
239
|
+
expect(route.status).to eq 'zero_results'
|
207
240
|
expect(route.duration_in_seconds).to be_nil
|
208
241
|
end
|
209
242
|
end
|
210
243
|
end
|
211
244
|
|
212
|
-
describe
|
245
|
+
describe '#reload' do
|
213
246
|
before do
|
214
247
|
allow(subject).to receive(:load_matrix) { ['loaded'] }
|
215
248
|
subject.data.clear
|
216
249
|
end
|
217
250
|
|
218
251
|
it "reloads matrix' data from the API" do
|
219
|
-
expect
|
252
|
+
expect do
|
220
253
|
subject.reload
|
221
|
-
|
254
|
+
end.to change(subject, :data).from([]).to ['loaded']
|
222
255
|
end
|
223
256
|
|
224
|
-
it
|
257
|
+
it 'is chainable' do
|
225
258
|
expect(subject.reload.data).to eq ['loaded']
|
226
259
|
end
|
227
260
|
end
|