Class: Geokit::Geocoders::GeoIpCityGeocoder

Inherits:
Geocoder
  • Object
show all
Defined in:
lib/geokit-geoip.rb

Overview

Provide geocoding based upon an IP address. The underlying web service is maxmind.com. MaxMind City is a paid-for service, provides country, region, and city. Updated every month.

Class Method Summary collapse

Class Method Details

.do_geocode(ip, options = {}) ⇒ Object

Given an IP address, returns a GeoLoc instance which contains latitude, longitude, city, and country code. Sets the success attribute to false if the ip parameter does not match an ip address.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/geokit-geoip.rb', line 18

def self.do_geocode(ip, options = {})
  return GeoLoc.new unless /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})?$/.match(ip)
  if (res = ::GeoIP.new(Geocoders::geoip_data_path).city(ip))
    loc = GeoLoc.new({
      :provider => 'maxmind_city',
      :lat => res.latitude,
      :lng => res.longitude,
      :city => res.city_name,
      :state => res.region_name,
      :zip => res.postal_code,
      :country_code => res.country_code2
    })
    # Work around Geokit's jankiness
    loc.success = res.city_name && res.city_name != ''
    loc
  else
    GeoLoc.new
  end
end