Created
September 29, 2011 20:12
-
-
Save ip2k/1251796 to your computer and use it in GitHub Desktop.
GeoIP in Sinatra
This file contains 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 '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