Skip to content

Instantly share code, notes, and snippets.

@krames
Created July 15, 2011 17:22
Show Gist options
  • Save krames/1085109 to your computer and use it in GitHub Desktop.
Save krames/1085109 to your computer and use it in GitHub Desktop.
Playing around with Geocoder
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