Skip to content

Instantly share code, notes, and snippets.

@ip2k
Created September 29, 2011 20:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ip2k/1251796 to your computer and use it in GitHub Desktop.
Save ip2k/1251796 to your computer and use it in GitHub Desktop.
GeoIP in Sinatra
require 'sinatra'
require 'geoip'
geo = GeoIP.new('GeoLiteCity.dat')
get '/' do
begin
x = geo.country request.ip
puts x.inspect
unless x[:postal_code].empty?
"ZIP for #{request.ip}: #{x[:postal_code]}"
else
"GeoIP doesn't have any info on your IP, #{request.ip} ...sorry."
end
rescue
"GeoIP doesn't know where #{request.ip} is."
end
end
get '/:location' do
begin
x = geo.country params[:location]
puts x.inspect
unless x[:postal_code].empty?
"Here ya go: #{x[:postal_code]}"
else
"GeoIP doesn't have any info on #{params[:location]}, sorry."
end
rescue
"Your request wasn't something that GeoIP could look up. Try an IP!"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment