Created
August 11, 2010 22:05
-
-
Save damien/519865 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'ostruct' | |
class GeoIpCity | |
DATA = "#{Rails.root}/db/GeoLiteCity.dat" | |
def locate(ip_to_locate) | |
data = api.city(ip_to_locate) | |
return if data.nil? | |
location = OpenStruct.new | |
location.input = data[0] | |
location.ip = data[1] | |
location.country_code = data[2] | |
location.country_code3 = data[3] | |
location.country_name = data[4] | |
location.parent_name = data[5] | |
location.region_code = data[6] | |
location.locality = data[7] | |
location.postal_code = data[8] | |
location.latitude = data[9] | |
location.longitude = data[10] | |
location.metro_code = data[11] | |
location.area_code = data[12] | |
location.timezone = TimezoneRegions.timezone(data[2], data[6]) | |
return location | |
end | |
private | |
def api | |
if File.exists? DATA | |
@api ||= GeoIP.new(DATA) | |
else | |
logger.warn "GeoLiteCity data file not found! Run `rake geo_lite:download_data_file`." | |
@api.city = nil | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment