Class: GoogleMapsJuice::Directions
Defined Under Namespace
Classes: Response
Constant Summary
collapse
- ENDPOINT =
'/directions'
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Endpoint
#initialize
Class Method Details
.find(params, api_key: GoogleMapsJuice.config.api_key) ⇒ Object
9
10
11
12
|
# File 'lib/google_maps_juice/directions.rb', line 9
def find(params, api_key: GoogleMapsJuice.config.api_key)
client = GoogleMapsJuice::Client.new(api_key: api_key)
new(client).find(params)
end
|
Instance Method Details
#find(params) ⇒ Object
15
16
17
18
19
20
|
# File 'lib/google_maps_juice/directions.rb', line 15
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
|
#validate_find_params(params) ⇒ Object
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/google_maps_juice/directions.rb', line 22
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
|
#validate_geo_coordinate(params) ⇒ Object
34
35
36
37
38
39
40
41
42
|
# File 'lib/google_maps_juice/directions.rb', line 34
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
|
#validate_location_params(params) ⇒ Object
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/google_maps_juice/directions.rb', line 44
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
|