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!)



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/hostip.rb', line 50

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"]["gml:featureMember"]["Hostip"]
  else
    self.get('/get_xml.php', :query => params)["HostipLookupResultSet"]["gml:featureMember"]["Hostip"]
  end
end

Instance Method Details

#city(ip = nil) ⇒ Object

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



22
23
24
# File 'lib/hostip.rb', line 22

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

#country_abbrev(ip = nil) ⇒ Object

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



17
18
19
# File 'lib/hostip.rb', line 17

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



12
13
14
# File 'lib/hostip.rb', line 12

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



34
35
36
37
38
39
40
41
42
# File 'lib/hostip.rb', line 34

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

#ipObject

get current ip



27
28
29
# File 'lib/hostip.rb', line 27

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