Skip to content

Commit

Permalink
Directions: fix and improvements(more)
Browse files Browse the repository at this point in the history
-bugfix Endpoint::Directions validate_geo_coordinate
-improvements in Directions::Response
-added directions_spec template
  • Loading branch information
alexmik95 committed Oct 14, 2019
1 parent 27aa256 commit b905e4c
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 32 deletions.
2 changes: 1 addition & 1 deletion lib/google_maps_juice/directions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def validate_directions_params(params)
end

def validate_geo_coordinate(params)
raise ArgumentError, 'String argument expected' unless params.values.each.is_a?(String)
raise ArgumentError, 'String argument expected' unless params.values.all?(String)

geocoords = params.values.map { |x| x.split(',') }.flatten
geocoords.map! { |x| Float(x).round(7) }
Expand Down
84 changes: 53 additions & 31 deletions lib/google_maps_juice/directions/response.rb
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
116 changes: 116 additions & 0 deletions spec/unit/directions_spec.rb
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

0 comments on commit b905e4c

Please sign in to comment.