Class: IPGeolocation::HostIPLocator

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

Overview

Location interface for hostip.info. Doesn’t need to be used directly.

Constant Summary collapse

HOST_IP_QUERY_URL =
"http://api.hostip.info/?"

Class Method Summary collapse

Class Method Details

.locate(ip) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ipgeolocation.rb', line 54

def self.locate ip
  doc = REXML::Document.new(open(IPGeolocation::HostIPLocator::HOST_IP_QUERY_URL+"ip=#{ip}"))

  result = IPGeolocation::Location.new
  result.ip = ip
  result.country_code = REXML::XPath.first( doc, "//countryAbbrev" ).text.strip
  result.country_name = REXML::XPath.first( doc, "//countryName" ).text.strip
  result.region_code = REXML::XPath.first( doc, "//gml:name" ).text.split(",").last.strip
  result.city = REXML::XPath.first( doc, "//gml:name" ).text.split(",").first.strip
  
  coords = REXML::XPath.first( doc, "//gml:coordinates" )
  if coords
    result.latitude = coords.text.split(",").last.strip
    result.longitude = coords.text.split(",").first.strip
  end
  result
end