June 11th, 2008 tkadom
I was reviewing a friends application this weekend and saw a nifty free IPGeocoding gem based off the maxmind geocoding library. If you have ever wondered how websites can guess where you are, then you probably ran into a site that uses IPGeocoding. Essentially your location is guessed based off your IP address. the results are not always accurate, but for a good portion of the userbase they are close enough. You can check out the library using the following steps:
wget http://www.maxmind.com/download/geoip/api/c/GeoIP.tar.gz
tar -zxvf GeoIP.tar.gz
cd GeoIP
./configure
make
sudo make install
sudo gem install geoip_city
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
use irb to confirm install
require 'rubygems'
require 'geoip_city'
g = GeoIPCity::Database.new('/usr/local/share/GeoIP/GeoLiteCity.dat')
res = g.look_up('69.94.197.133')
puts "lat: #{res[:latitude]} lng: #{res[:longitude]}"
Its that simple! I did notice that upon installing the gem on a debian box, i was receiving a shared object library error. This was corrected by exporting my LD_LIBRARY_PATH to include /usr/local/lib. Alternately you could simply take the libraries that were installed in usr/local/lib and create symbolic links in /usr/lib. A commercial version of their database is available for a fee. (check out maxmind for more info)
once that was working, i decided to take a look at events that might be happening in the area. so i created a basic rails application, installed the eventful gem (sudo gem install eventfulapi), and created a geo controller. i.e.
class GeoController < ApplicationController
require 'rubygems'
require 'geoip_city'
require 'eventful/api'
GEOCODER = GeoIPCity::Database.new('/usr/local/share/GeoIP/GeoLiteCity.dat')
def index
@ip = request.remote_ip
@location = GEOCODER.look_up(@ip.to_s)
eventful = Eventful::API.new('REPLACEME')
@citystate = "#{@location[:city]}, #{@location[:region]}"
@results = eventful.call('events/search',
:app_key => 'REPLACEME',
:location => @citystate,
:date => 'Today',
:page_size => 25)
end
end
this controller pulls events for the location returned. you can access the results in a view
via:
<%= event['start_time'] %> - </td><td><%= link_to event['title'], event['url']%>
Posted in Random | 3 Comments »