[UPDATED]: I had forgotten to include the install instructions for getting the GeoIP C library. Install the C bindings, the gem (which isnt packaged as a gem for easy download) and get the data.
wget http://www.maxmind.com/download/geoip/api/c/GeoIP.tar.gz tar -zxvf GeoIP.tar.gz cd GeoIP ./configure && make && sudo make install wget http://rubyforge.org/frs/download.php/27077/geoip_city-0.1.gem sudo gem install geoip_city-0.1.gem wget http://www.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz gunzip GeoLiteCity.dat.gz sudo mkdir /usr/local/share/GeoIP sudo mv GeoLiteCity.dat /usr/local/share/GeoIP/GeoLiteCity.dat
>> require 'geoip_city' >> g = GeoIPCity::Database.new('/usr/local/share/GeoIP/GeoLiteCity.dat') >> res = g.look_up('4.2.2.2') >> puts "lat: #{res[:latitude]} lng: #{res[:longitude]}" lat: 38.0 lng: -97.0
>> Benchmark.measure { 1000.times {Hostip.geocode('4.2.2.2')}}.total => 1.02 >> Benchmark.measure { 100000.times {g.look_up('4.2.2.2')}}.total => 0.5
But all in all, for the 5 minutes it took me to write the original version, it was fast enough... but MaxMind GeoLiteCity + the geoip_city gem is Super fast-er.
