What?
This is a library which provides a single function. The function takes as input an IP address and it outputs a hash containing best-guess geographical information (like city, country, latitude, and longitude).
Actually this is only a Ruby binding to a C library which provides this function. Also, you must download a large binary database with all this mapping information. It is kindly provided free of charge by MaxMind.com.
How To Use
Some variation of the following should work.
-
Install the GeoCity C library. You can get it from here
www.maxmind.com/app/c For example, I like to install mine in /opt/GeoIP so I do this:
tar -zxvf GeoIP-1.4.3.tar.gz cd GeoIP-1.4.3 ./configure --prefix=/opt/GeoIP make && sudo make install
-
Now install the geoip Ruby gem (this package!)
Hint: You need to make sure that gcc can find the GeoIP header and library files. One way to do this is to ensure that your CPATH and LIBRARY_PATH enviromental variables include the GeoIP directory. So, if you want, do this:
export CPATH="/opt/GeoIP/include:$CPATH" export LIBRARY_PATH="/opt/GeoIP/lib:$LIBRARY_PATH" sudo gem install geoip_city
-
Download the GeoLite City database file in binary format from: www.maxmind.com/app/geolitecity Maybe this direct link will work: www.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz I put this file in /opt/GeoIP/share/GeoIP/GeoLiteCity.dat
-
Use it!
require 'geoip' db = GeoIP::Database.new('/opt/GeoIP/share/GeoIP/GeoLiteCity.dat') result = db.look_up('24.24.24.24') puts result.inspect
License
Copyright © 2007 Ryan Dahl ([email protected])
I give permission for you to do whatever you’d like to do with this software.