Module: SimpleGeocoder

Extended by:
SimpleGeocoder
Included in:
SimpleGeocoder
Defined in:
lib/simple_geocode.rb

Overview

A basic, caching, geocoder

Constant Summary collapse

TIME_TO_LIVE =

30 days

30 * 24 * 3600

Instance Method Summary collapse

Instance Method Details

#cacheObject



13
14
15
# File 'lib/simple_geocode.rb', line 13

def cache
  @cache ||= SimpleCache.new("simple_geocoder")
end

#cache=(cache) ⇒ Object



9
10
11
# File 'lib/simple_geocode.rb', line 9

def cache=(cache)
  @cache = cache
end

#latlng(address) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/simple_geocode.rb', line 20

def latlng(address)
  cache.cached(address, TIME_TO_LIVE) { 
    url = "http://maps.googleapis.com/maps/api/geocode/json?address=#{CGI.escape(address)}&sensor=false&region=de"
    data = JSON.parse(get(url))

    if data["status"] != "OK"
      STDERR.puts "Geocoding failed for '#{address}'"
      return
    end

    results = data["results"]
    location = results[0]["geometry"]["location"]
    location.values_at("lat", "lng")
  }
end