Skip to content

Commit

Permalink
Add support for 'traffic_model' parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Jak Spalding committed Jun 14, 2017
1 parent 46746fd commit 7af6d4c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
13 changes: 12 additions & 1 deletion lib/google_maps_service/apis/directions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,19 @@ module Directions
# `rail` is equivalent to `["train", "tram", "subway"]`.
# @param [String] transit_routing_preference Specifies preferences for transit
# requests. Valid values are `less_walking` or `fewer_transfers`.
# @param [String] traffic_model Specifies the assumptions to use when calculating time in traffic.
# This setting affects the value returned in the duration_in_traffic field in the response,
# which contains the predicted time in traffic based on historical averages. The traffic_model
# parameter may only be specified for driving directions where the request includes a
# `departure_time`, and only if the request includes an API key or a Google Maps APIs Premium
# Plan client ID. Valid values are `best_guess` (default), `pessimistic` or `optimistic`.
#
# @return [Array] Array of routes.
def directions(origin, destination,
mode: nil, waypoints: nil, alternatives: false, avoid: nil,
language: nil, units: nil, region: nil, departure_time: nil,
arrival_time: nil, optimize_waypoints: false, transit_mode: nil,
transit_routing_preference: nil)
transit_routing_preference: nil, traffic_model: nil)

params = {
origin: GoogleMapsService::Convert.waypoint(origin),
Expand Down Expand Up @@ -94,6 +100,11 @@ def directions(origin, destination,

params[:transit_mode] = GoogleMapsService::Convert.join_list("|", transit_mode) if transit_mode
params[:transit_routing_preference] = transit_routing_preference if transit_routing_preference
params[:traffic_model] = GoogleMapsService::Validator.traffic_model(traffic_model) if traffic_model

if traffic_model and not departure_time
raise ArgumentError, 'Must specify departure_time if specifying traffic_model'
end

return get('/maps/api/directions/json', params)[:routes]
end
Expand Down
14 changes: 14 additions & 0 deletions lib/google_maps_service/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,19 @@ def avoid(avoid)
end
avoid
end

# Validate traffic model. The valid values of traffic are `best_guess`, `pessimistic` or `optimistic`.
#
# @param [String, Symbol] traffic_model Traffic model to be validated
#
# @raise ArgumentError The traffic model is invalid.
#
# @return [String] Valid traffic model
def traffic_model(traffic_model)
unless [:best_guess, :pessimistic, :optimistic].include?(traffic_model.to_sym)
raise ArgumentError, 'Invalid traffic model.'
end
traffic_model
end
end
end
14 changes: 14 additions & 0 deletions spec/google_maps_service/apis/directions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,18 @@
expect(a_request(:get, 'https://maps.googleapis.com/maps/api/directions/json?origin=Sydney+Town+Hall&destination=Parramatta+Town+Hall&alternatives=true&key=%s' % api_key)).to have_been_made
end
end

context 'traffic_model parameter' do
it 'should call Google Maps Web Service' do
now = Time.now
client.directions('Sydney Town Hall', 'Parramatta Town Hall', traffic_model: 'pessimistic', departure_time: now)
expect(a_request(:get, 'https://maps.googleapis.com/maps/api/directions/json?origin=Sydney+Town+Hall&destination=Parramatta+Town+Hall&traffic_model=pessimistic&departure_time=%d&key=%s' % [now.to_i, api_key])).to have_been_made
end

it 'should error if departure_time not provided' do
expect {
client.directions('Sydney Town Hall', 'Parramatta Town Hall', traffic_model: 'pessimistic')
}.to raise_error ArgumentError
end
end
end
21 changes: 20 additions & 1 deletion spec/google_maps_service/validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,29 @@
end
end

context 'with invalid travel mode' do
context 'with invalid route restriction' do
it 'should raise ArgumentError' do
expect { GoogleMapsService::Validator.avoid('people') }.to raise_error ArgumentError
end
end
end

describe '.traffic_model' do
context 'with valid traffic model' do
it 'should return the traffic model' do
[:best_guess, :pessimistic, :optimistic].each do |mode|
expect(GoogleMapsService::Validator.traffic_model(mode)).to eq(mode)
end
['best_guess', 'pessimistic', 'optimistic'].each do |mode|
expect(GoogleMapsService::Validator.traffic_model(mode)).to eq(mode)
end
end
end

context 'with invalid traffic model' do
it 'should raise ArgumentError' do
expect { GoogleMapsService::Validator.traffic_model('nonexistent') }.to raise_error ArgumentError
end
end
end
end

0 comments on commit 7af6d4c

Please sign in to comment.