Class: Geogov::Hostip

Inherits:
Object
  • Object
show all
Defined in:
lib/geogov/providers/hostip.rb

Instance Method Summary collapse

Constructor Details

#initializeHostip

Returns a new instance of Hostip.



6
7
8
# File 'lib/geogov/providers/hostip.rb', line 6

def initialize
  @url = 'http://api.hostip.info/get_html.php'
end

Instance Method Details

#remote_location(ip_address) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/geogov/providers/hostip.rb', line 10

def remote_location(ip_address)
  params = {:ip => ip_address, :position => true}
  results = Geogov.get(@url + "?" + Geogov.hash_to_params(params))
  return nil if results.nil?
  response = YAML.load(results + "\n")
  location = {}.tap do |h|
    h["lat"] = response['Latitude']
    h["lon"] = response['Longitude']
    h["city"], h["county"] = response['City'].split(', ')
    country = response['Country'].match(/\((\w+)\)$/)
    h["country"] = country[1] if country
  end
  return nil if location['city'] =~ /Unknown City/
  return nil if location['city'] =~ /Private Address/

  # I found these very unreliable, so better they're
  # not there to tempt anyone
  location.delete("city")
  location.delete("county")
  return location
end