Class: Google::Geocode

Inherits:
Object
  • Object
show all
Defined in:
lib/google/geocode.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, client) ⇒ Geocode

Returns a new instance of Geocode.



7
8
9
10
# File 'lib/google/geocode.rb', line 7

def initialize(api_key, client)
  @api_key = api_key
  @client = client
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



6
7
8
# File 'lib/google/geocode.rb', line 6

def api_key
  @api_key
end

Instance Method Details

#do_geocode(location) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/google/geocode.rb', line 12

def do_geocode(location)
  url = "http://maps.google.com/maps/geo?sensor=false&key=#{@api_key}&output=json&q=#{location}&client=#{@client}"
  resp = Net::HTTP.get_response(URI.parse(url))
  data = resp.body

  Google::Result.new(data)
end

#geocode(location) ⇒ Object



20
21
22
23
# File 'lib/google/geocode.rb', line 20

def geocode(location)
  # change whitespace to %20 for the URL
  result = do_geocode location.gsub(/\s+/, '%20')
end

#reverse_geocode(location) ⇒ Object



26
27
28
29
# File 'lib/google/geocode.rb', line 26

def reverse_geocode(location)
  # reverse geocodes should not have any spaces in them
  result = do_geocode location.gsub(/\s+/, '')
end