-
Notifications
You must be signed in to change notification settings - Fork 0
/
geoiplookup.rb
executable file
·69 lines (51 loc) · 1.29 KB
/
geoiplookup.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
require 'rubygems'
require 'sinatra'
configure do
require 'geoip'
require 'json'
require 'haml'
set :haml, {:format => :html5 }
GEOIP = GeoIP.new('GeoLiteCity.dat')
end
helpers do
def lookup(host)
results = perform_lookup(host)
fields_and_values = { :hostname => results[0],
:ip => results[1],
:cc_alpha2 => results[2],
:cc_alpha3 => results[3],
:country_name => results[4],
:continent => results[5],
:state => results[6],
:city => results[7],
:zipcode => results[8],
:latitude => results[9],
:longitude => results[10]
}
fields_and_values[:dma_code] = results[11] if results[11]
fields_and_values[:area_code] = results[12] if results[12]
return fields_and_values
end
private
def perform_lookup(host)
begin
results = GEOIP.city(host)
rescue
halt 404, "host #{host} not found"
end
halt 404, "No info found for #{host}" unless results
return results
end
end
get '/:lookup.json' do
content_type :json
resolved = lookup(params[:lookup])
resolved.to_json
end
get '/:lookup' do
@resolved = lookup(params[:lookup])
haml :lookup
end
get '/' do
File.open('public/index.html').read
end