-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Directions: fix and improvements(more)
-bugfix Endpoint::Directions validate_geo_coordinate -improvements in Directions::Response -added directions_spec template
- Loading branch information
Showing
3 changed files
with
170 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,75 @@ | ||
require 'active_support/core_ext/hash/slice' | ||
|
||
# TODO: improve handling of legs and steps | ||
|
||
module GoogleMapsJuice | ||
class Directions::Response < GoogleMapsJuice::Endpoint::Response | ||
def location | ||
result.dig('geometry', 'location') | ||
def results | ||
self['routes'] | ||
end | ||
|
||
def partial_match? | ||
result['partial_match'] == true | ||
def routes | ||
results.map { |r| Route.new(r) } | ||
end | ||
|
||
def route | ||
def first | ||
routes.first | ||
end | ||
|
||
def summary | ||
route.first['summary'] | ||
end | ||
class Route | ||
attr_reader :route | ||
def initialize(route) | ||
@route = route | ||
end | ||
=begin | ||
def initialize(params) | ||
@summary = route['summary'] | ||
@legs = route['legs'] | ||
@step = route['legs']['steps'] | ||
@duration = route['duration'] | ||
@distance = route['distance'] | ||
@start_location = route['start_location'] | ||
@end_location = route['end_location'] | ||
@start_address = route['start_address'] | ||
@end_address = route['end_address'] | ||
end | ||
=end | ||
|
||
def legs | ||
route['legs'] | ||
end | ||
def summary | ||
route[:summary] | ||
end | ||
|
||
def steps | ||
legs['steps'] | ||
end | ||
def legs | ||
route[:legs] | ||
end | ||
|
||
def duration | ||
route['duration'] | ||
end | ||
def steps | ||
route.dig[:legs, :steps] | ||
end | ||
|
||
def distance | ||
route['distance'] | ||
end | ||
def duration | ||
route[:duration] | ||
end | ||
|
||
def start_location | ||
route['start_location'] | ||
end | ||
def distance | ||
route[:distance] | ||
end | ||
|
||
def end_location | ||
route['end_location'] | ||
end | ||
def start_location | ||
route[:start_location].to_s | ||
end | ||
|
||
def start_address | ||
route['start_address'] | ||
end | ||
def end_location | ||
route[:end_location].to_s | ||
end | ||
|
||
def start_address | ||
route[:start_address] | ||
end | ||
|
||
def end_address | ||
route['end_address'] | ||
def end_address | ||
route[:end_address] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
require 'spec_helper' | ||
|
||
ROME = '12.496365,41.902783' | ||
BARI = '16.871871,41.117143' | ||
SIDNEY = '151.206990,-33.867487' | ||
|
||
# TODO: specs of validate_geo_coordinate and validate_location_params | ||
|
||
RSpec.describe GoogleMapsJuice::Directions do | ||
let(:client) { GoogleMapsJuice::Client.new } | ||
let(:directions) { GoogleMapsJuice::Directions.new(client) } | ||
let(:endpoint) { '/directions/json' } | ||
|
||
describe '#directions' do | ||
subject { directions.directions(params) } | ||
|
||
context 'with bad params' do | ||
context 'when params is not a Hash' do | ||
let(:params) { 'foobar' } | ||
|
||
it 'raises ArgumentError' do | ||
expect { subject }.to raise_error( | ||
ArgumentError, | ||
'Hash argument expected' | ||
) | ||
end | ||
end | ||
|
||
context 'when some unsupported param is passed' do | ||
let(:params) { { origin: BARI, foo: 'hey', bar: 'man' } } | ||
|
||
it 'raises ArgumentError' do | ||
expect { subject }.to raise_error( | ||
ArgumentError, | ||
'The following params are not supported: foo, bar' | ||
) | ||
end | ||
end | ||
|
||
context 'when none of the required params is passed' do | ||
let(:params) { { region: 'US' } } | ||
|
||
it 'raises ArgumentError' do | ||
expect { subject }.to raise_error( | ||
ArgumentError, | ||
'Any of the following params are required: address, components' | ||
) | ||
end | ||
end | ||
end | ||
|
||
context 'with good params' do | ||
before do | ||
expect(client).to receive(:get).with(endpoint, params).and_return(response) | ||
end | ||
|
||
context 'rome to bari geo-coordinates' do | ||
let(:response) { response_fixture('directions/rome_to_bari') } | ||
|
||
context 'with right geo-coordinates' do | ||
let(:params) { { | ||
origin: ROME, | ||
destination: BARI | ||
} } | ||
|
||
it 'returns one or more routes' do | ||
expect_rome_to_bari_result(subject) | ||
end | ||
end | ||
end | ||
|
||
context 'rome to sidney' do | ||
let(:response) { response_fixture('geocoding/rome_to_sidney') } | ||
|
||
context 'with right geo-cordinates' do | ||
let(:params) { { | ||
origin: ROME, | ||
destination: SIDNEY | ||
} } | ||
|
||
it 'returns no route result' do | ||
expect_rome_to_sidney_result(subject) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
def expect_rome_to_bari_result(result) | ||
expect(result).to be_a GoogleMapsJuice::Directions::Response | ||
expect(result.routes.size).to be > 0 | ||
expect(result.routes.first.summary).to eq '...' | ||
expect(result.routes.first.distance).to eq '...' | ||
expect(result.routes.first.duration).to eq '...' | ||
expect(result.routes.first.start_location).to eq '...' | ||
expect(result.routes.first.end_location).to eq '...' | ||
expect(result.routes.first.start_address).to eq '...' | ||
expect(result.routes.first.end_address).to eq '...' | ||
expect(result.routes.first.legs.size).to be > 0 | ||
expect(result.routes.first.steps.size).to be > 0 | ||
end | ||
|
||
def expect_rome_to_sidney_result(result) | ||
expect(result).to be_a GoogleMapsJuice::Directions::Response | ||
expect(result.routes.size).to eq 0 | ||
expect(result.routes.first.summary).to be nil | ||
expect(result.routes.first.distance).to be nil | ||
expect(result.routes.first.duration).to be nil | ||
expect(result.routes.first.start_location).to be nil | ||
expect(result.routes.first.end_location).to be nil | ||
expect(result.routes.first.start_address).to be nil | ||
expect(result.routes.first.end_address).to be nil | ||
expect(result.routes.first.legs.size).to be nil | ||
expect(result.routes.first.steps.size).to be nil | ||
end | ||
end |