Skip to content

Commit

Permalink
Merge 6a8ce4d into 6e37676
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmik95 committed Oct 21, 2019
2 parents 6e37676 + 6a8ce4d commit 2d1db50
Show file tree
Hide file tree
Showing 14 changed files with 1,228 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .ruby-version
@@ -1 +1 @@
2.5.0
2.6.3
4 changes: 2 additions & 2 deletions .travis.yml
@@ -1,9 +1,9 @@
sudo: false
language: ruby
rvm:
- 2.5.0
- 2.6.3

before_install: gem install bundler -v 1.16.4
before_install: gem install bundler -v 1.17.2

install: bundle install

Expand Down
2 changes: 1 addition & 1 deletion google_maps_juice.gemspec
Expand Up @@ -25,7 +25,7 @@ Gem::Specification.new do |spec|
spec.add_dependency 'activesupport', '~> 5.2'
spec.add_dependency 'excon'

spec.add_development_dependency 'bundler', '~> 1.16'
spec.add_development_dependency 'bundler', '~> 1.17.2'
spec.add_development_dependency 'rake', '~> 12.3'
spec.add_development_dependency 'rspec', '~> 3.5'
spec.add_development_dependency 'webmock', '~> 3.4'
Expand Down
1 change: 1 addition & 0 deletions lib/google_maps_juice.rb
Expand Up @@ -14,6 +14,7 @@ module GoogleMapsJuice
autoload :Endpoint, 'google_maps_juice/endpoint'
autoload :Geocoding, 'google_maps_juice/geocoding'
autoload :Timezone, 'google_maps_juice/timezone'
autoload :Directions, 'google_maps_juice/directions'

class Error < Exception; end
class ApiLimitError < Exception; end
Expand Down
55 changes: 55 additions & 0 deletions lib/google_maps_juice/directions.rb
@@ -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
63 changes: 63 additions & 0 deletions lib/google_maps_juice/directions/response.rb
@@ -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 spec/fixtures/response/directions/colosseum-colosseum.json
@@ -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"
}

0 comments on commit 2d1db50

Please sign in to comment.