-
Notifications
You must be signed in to change notification settings - Fork 0
/
geolocation.rb
36 lines (30 loc) · 869 Bytes
/
geolocation.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
require_relative 'json_api'
class Coordinate
attr_reader :longitude, :latitude
def initialize(lat, long)
@latitude = lat
@longitude = long
end
end
class Geolocation < JsonAPI
URL = "https://maps.google.com/maps/api/geocode/json"
attr_reader :language
def initialize(options)
@language = options.language
end
def coordinates(location)
response = connect(URL, {language: @language, address: location})
response = Hashie::Mash.new(JSON.parse(response))
case response.status
when "OK"
return response.results.map { |result|
coord = result.geometry.location
Coordinate.new(coord.lat, coord.lng)
}
when "ZERO_RESULTS"
raise(IOError, "Could not find the location `#{options.location}'!")
else
raise(IOError, "Unknown response in Geocode API call:\n#{result}")
end
end
end