Class: Hostip

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/hostip.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.request(ip = nil, position = false) ⇒ Object

Make a request and strip unwanted XML before returning result options: ip => xxx.xxx.xxx.xxx postion => true (Documented but does nothing!)



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/hostip.rb', line 47

def request(ip=nil, position=false)
  # construct params hash
  params = {}
  if ip != nil
    params["ip"] = ip
  end
  if position
    params["position"] = "true"
  end
  # sent request
  if params == {}
    self.get('/get_xml.php')["HostipLookupResultSet"]["featureMember"]["Hostip"]
  else
    self.get('/get_xml.php', :query => params)["HostipLookupResultSet"]["featureMember"]["Hostip"]
  end
end

Instance Method Details

#city(ip = nil) ⇒ Object

get city name for ip, if no ip is passed use own ip



19
20
21
# File 'lib/hostip.rb', line 19

def city(ip=nil)
  self.class.request(ip)["name"]
end

#country_abbrev(ip = nil) ⇒ Object

get country abbreviation for ip, if no ip is passed use own ip



14
15
16
# File 'lib/hostip.rb', line 14

def country_abbrev(ip=nil)
  self.class.request(ip)["countryAbbrev"]
end

#country_name(ip = nil) ⇒ Object

get country name for ip, if no ip is passed use own ip



9
10
11
# File 'lib/hostip.rb', line 9

def country_name(ip=nil)
    self.class.request(ip)["countryName"]
end

#geo_location(ip = nil) ⇒ Object

Returns a hash with long => “xxxx” and lat => “xxx” or raise exception if location is unknown



31
32
33
34
35
36
37
38
39
# File 'lib/hostip.rb', line 31

def geo_location(ip=nil)
  begin
    # Get Comma seperated coordinates and return as hash
    coordinates = self.class.request(ip, true)["ipLocation"]["pointProperty"]["Point"]["coordinates"].split(',')
    return { "long" => coordinates[0], "lat" => coordinates[1] }
  rescue
    raise "geo location unknown"
  end
end

#ipObject

get current ip



24
25
26
# File 'lib/hostip.rb', line 24

def ip
  self.class.request["ip"]
end