Skip to content
Browse files

falling back better

  • Loading branch information...
1 parent d73fa3b commit 8cae63b71166930c2c9ac995dbc60fbe973ec781 @JackDanger JackDanger committed Nov 27, 2010
Showing with 5 additions and 4 deletions.
  1. +5 −4 lib/geoip_server.rb
View
9 lib/geoip_server.rb
@@ -4,7 +4,7 @@
require 'geoip'
require 'json'
-
+data_file = File.expand_path(File.join(File.dirname(__FILE__), '..', 'vendor', 'GeoLiteCity.dat'))
## Application
@@ -15,8 +15,7 @@
Lookup a location by IP address. Example:
<pre style='font-family: Iconsolata, monospace;background-color:#EEE'>curl http://#{request.host}/207.97.227.239</pre>
<br />
- <form action=/ method=GET onsubmit='if(\"\"==this.url.value)return false;else{this.action=\"/\"+this.ip.value}'>
- <label for='ip'>IP address: </label>
+ <form action=/ method=GET onsubmit='if(\"\"==this.ip.value)return false;else{this.action=\"/\"+this.ip.value}'>
<input type=text name='ip' value='#{request.env['REMOTE_ADDR']}' />
<input type=submit value='Lookup!' />
</form>
@@ -28,10 +27,12 @@
get '/:ip' do
pass unless params[:ip] =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
- data = GeoIP.new('vendor/GeoLiteCity.dat').city(params[:ip])
+ data = GeoIP.new(data_file).city(params[:ip])
content_type 'application/json'
+ return "{}" unless data
+
encode(data).to_json
end

0 comments on commit 8cae63b

Please sign in to comment.
Something went wrong with that request. Please try again.