Module: GoogleMaps::Geocoder
- Defined in:
- lib/google_maps/geocoder.rb,
lib/google_maps/geocoder/result.rb,
lib/google_maps/geocoder/location.rb
Defined Under Namespace
Classes: Location, Result
Constant Summary
collapse
- URI_DOMAIN =
"maps.googleapis.com"
- URI_BASE =
"/maps/api/geocode/json"
Class Method Summary
collapse
Class Method Details
.locate!(address, options = { }) ⇒ Object
18
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/google_maps/geocoder.rb', line 18
def self.locate!(address, options = { })
options = {
:ssl => false,
:address => address,
:sensor => false
}.merge(options)
json = ActiveSupport::JSON.decode RestClient.get(url(options))
Geocoder::Result.new(json)
rescue => e
raise GeocodeFailed.new(address, e)
end
|
.uri_base_path(options = { }) ⇒ Object
55
56
57
58
|
# File 'lib/google_maps/geocoder.rb', line 55
def self.uri_base_path(options = { })
protocol = options[:ssl] ? "https" : "http"
"#{protocol}://#{URI_DOMAIN}#{URI_BASE}"
end
|
.url(options) ⇒ Object
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/google_maps/geocoder.rb', line 31
def self.url(options)
ssl = options.delete(:ssl)
client = options.delete(:client) || ::GoogleMaps.client
key = options.delete(:key) || ::GoogleMaps.key
parameters = []
options.each do |k, v|
parameters << "#{k}=#{CGI.escape(v.to_s)}"
end
if ::GoogleMaps.enterprise_account? && client && key
parameters << "client=#{CGI.escape(client)}"
sign_str = "#{URI_BASE}?#{parameters.join('&')}"
sha1 = OpenSSL::Digest::Digest.new('sha1')
binary_key = Base64.decode64(key.tr('-_','+/'))
binary_signature = OpenSSL::HMAC.digest(sha1, binary_key, sign_str)
signature = Base64.encode64(binary_signature).tr('+/','-_').strip
parameters << "signature=#{signature}"
end
"#{uri_base_path(:ssl => ssl)}?#{parameters.join('&')}"
end
|