Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from algonauti/4-directions-endpoint
4-directions-endpoint
- Loading branch information
Showing
15 changed files
with
1,294 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
2.5.0 | ||
2.6.3 |
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
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
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,55 @@ | ||
module GoogleMapsJuice | ||
class Directions < Endpoint | ||
|
||
ENDPOINT = '/directions' | ||
|
||
autoload :Response, 'google_maps_juice/directions/response' | ||
|
||
class << self | ||
def find(params, api_key: GoogleMapsJuice.config.api_key) | ||
client = GoogleMapsJuice::Client.new(api_key: api_key) | ||
new(client).find(params) | ||
end | ||
end | ||
|
||
def find(params) | ||
validate_find_params(params) | ||
response_text = @client.get("#{ENDPOINT}/json", params) | ||
response = JSON.parse(response_text, object_class: Response) | ||
detect_errors(response) | ||
end | ||
|
||
def validate_find_params(params) | ||
raise ArgumentError, 'Hash argument expected' unless params.is_a?(Hash) | ||
|
||
supported_keys = %w[origin destination] | ||
validate_supported_params(params, supported_keys) | ||
|
||
required_keys = %w[origin destination] | ||
validate_any_required_params(params, required_keys) | ||
|
||
validate_geo_coordinate(params) | ||
end | ||
|
||
def validate_geo_coordinate(params) | ||
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) } | ||
raise ArgumentError, 'Wrong geo-coordinates' if geocoords.size != 4 | ||
|
||
validate_location_params(geocoords) | ||
end | ||
|
||
def validate_location_params(params) | ||
latitudes = params[0], params[2] | ||
if latitudes.any? { |l| l.abs > 90 } | ||
raise ArgumentError, 'Wrong latitude value' | ||
end | ||
longitudes = params[1], params[3] | ||
if longitudes.any? { |l| l.abs > 180 } | ||
raise ArgumentError, 'Wrong longitude value' | ||
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,63 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'active_support/core_ext/hash/slice' | ||
|
||
module GoogleMapsJuice | ||
class Directions::Response < GoogleMapsJuice::Endpoint::Response | ||
def results | ||
self['routes'] | ||
end | ||
|
||
def routes | ||
results.map { |r| Route.new(r) } | ||
end | ||
|
||
def first | ||
routes.first | ||
end | ||
|
||
class Route | ||
attr_reader :route, :first_leg | ||
def initialize(route) | ||
@route = route | ||
@first_leg = route['legs'].first | ||
end | ||
|
||
def legs | ||
route['legs'] | ||
end | ||
|
||
def summary | ||
route['summary'] | ||
end | ||
|
||
def steps | ||
first_leg['steps'] | ||
end | ||
|
||
def duration | ||
first_leg['duration'] | ||
end | ||
|
||
def distance | ||
first_leg['distance'] | ||
end | ||
|
||
def start_location | ||
first_leg['start_location'] | ||
end | ||
|
||
def end_location | ||
first_leg['end_location'] | ||
end | ||
|
||
def start_address | ||
first_leg['start_address'] | ||
end | ||
|
||
def end_address | ||
first_leg['end_address'] | ||
end | ||
end | ||
end | ||
end |
93 changes: 93 additions & 0 deletions
93
spec/fixtures/response/directions/colosseum-colosseum.json
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,93 @@ | ||
{ | ||
"geocoded_waypoints": [ | ||
{ | ||
"geocoder_status": "OK", | ||
"place_id": "ChIJrRMgU7ZhLxMRxAOFkC7I8Sg", | ||
"types": [ | ||
"establishment", | ||
"point_of_interest", | ||
"tourist_attraction" | ||
] | ||
}, | ||
{ | ||
"geocoder_status": "OK", | ||
"place_id": "ChIJrRMgU7ZhLxMRxAOFkC7I8Sg", | ||
"types": [ | ||
"establishment", | ||
"point_of_interest", | ||
"tourist_attraction" | ||
] | ||
} | ||
], | ||
"routes": [ | ||
{ | ||
"bounds": { | ||
"northeast": { | ||
"lat": 41.8892732, | ||
"lng": 12.4921921 | ||
}, | ||
"southwest": { | ||
"lat": 41.8892732, | ||
"lng": 12.4921921 | ||
} | ||
}, | ||
"copyrights": "Map data ©2019 Google", | ||
"legs": [ | ||
{ | ||
"distance": { | ||
"text": "1 m", | ||
"value": 0 | ||
}, | ||
"duration": { | ||
"text": "1 min", | ||
"value": 0 | ||
}, | ||
"end_address": "Piazza del Colosseo, 1, 00184 Roma RM, Italy", | ||
"end_location": { | ||
"lat": 41.8892732, | ||
"lng": 12.4921921 | ||
}, | ||
"start_address": "Piazza del Colosseo, 1, 00184 Roma RM, Italy", | ||
"start_location": { | ||
"lat": 41.8892732, | ||
"lng": 12.4921921 | ||
}, | ||
"steps": [ | ||
{ | ||
"distance": { | ||
"text": "1 m", | ||
"value": 0 | ||
}, | ||
"duration": { | ||
"text": "1 min", | ||
"value": 0 | ||
}, | ||
"end_location": { | ||
"lat": 41.8892732, | ||
"lng": 12.4921921 | ||
}, | ||
"html_instructions": "Head on <b>Via Celio Vibenna</b>", | ||
"polyline": { | ||
"points": "}nt~Fe{fkA" | ||
}, | ||
"start_location": { | ||
"lat": 41.8892732, | ||
"lng": 12.4921921 | ||
}, | ||
"travel_mode": "DRIVING" | ||
} | ||
], | ||
"traffic_speed_entry": [], | ||
"via_waypoint": [] | ||
} | ||
], | ||
"overview_polyline": { | ||
"points": "}nt~Fe{fkA" | ||
}, | ||
"summary": "Via Celio Vibenna", | ||
"warnings": [], | ||
"waypoint_order": [] | ||
} | ||
], | ||
"status": "OK" | ||
} |
Oops, something went wrong.