Created
July 15, 2011 17:22
-
-
Save krames/1085109 to your computer and use it in GitHub Desktop.
Playing around with Geocoder
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 'rubygems' | |
require 'sinatra' | |
require 'geocoder' | |
get '/' do | |
view | |
end | |
post '/locate' do | |
result = nil | |
if params[:domain] | |
ip = IPSocket::getaddress(params[:domain]) | |
@result = Geocoder.search(ip) | |
end | |
view | |
end | |
def result_div | |
return '' unless @result && [email protected]? | |
geo_data = @result.first.data | |
html = "<hr><table>" | |
html += "<tr><td style='font-weight:bold'>domain</td><td>#{params[:domain]}</td></tr>" | |
geo_data.each do |key, value| | |
html += "<tr><td style='font-weight:bold'>#{key}</td><td>#{value}</td></tr>" | |
end | |
html += "</table><hr>" | |
end | |
def view | |
<<HTML | |
<html> | |
#{result_div} | |
<h1>Domain Locator</h1> | |
<form method='post' action='locate'> | |
<input type='text' name='domain'> | |
<input type='submit'> | |
</form> | |
</html> | |
HTML | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment