Class: IPGeolocation::NetGeoLocator

Inherits:
Object
  • Object
show all
Defined in:
lib/ipgeolocation.rb

Overview

Location interface for netgeo.caida.org. Doesn’t need to be used directly.

Class Method Summary collapse

Class Method Details

.locate(ip) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ipgeolocation.rb', line 76

def self.locate ip
  resp = Net::HTTP.new("netgeo.caida.org").get("/perl/netgeo.cgi?target=#{ip}")
  result = IPGeolocation::Location.new
  fakehash = resp.body.gsub(/<\/?[^>]*>/, "").split("\n").map {|a| a.split ":"}
  
  result.ip = fakehash.assoc("TARGET")[1].strip
  result.city = fakehash.assoc("CITY")[1].strip
  result.region_name = fakehash.assoc("STATE")[1].strip
  result.country_code = fakehash.assoc("COUNTRY")[1].strip
  result.latitude = fakehash.assoc("LAT")[1].strip
  result.longitude = fakehash.assoc("LONG")[1].strip
  result
end