Module: Sgeoip

Defined in:
lib/sgeoip.rb,
lib/sgeoip/version.rb

Constant Summary collapse

VERSION =
"0.2.2"

Class Method Summary collapse

Class Method Details

.geoiptool(ip) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sgeoip.rb', line 19

def self.geoiptool(ip)
  url = "http://www.geoiptool.com/en/?IP=#{ip}"
  begin
    doc = Nokogiri::HTML(open(url))
    geoip = {  "host"         => doc.at_css(".tbl_style tr:nth-child(1) td.arial_bold").text.chars.select{|i| i.valid_encoding?}.join,
               "country"      => doc.at_css(".tbl_style tr:nth-child(3) td.arial_bold").text.to_s[1..-2].chars.select{|i| i.valid_encoding?}.join, #strip extra spaces
               "country_code" => doc.at_css(".tbl_style tr:nth-child(4) td.arial_bold").text.chars.select{|i| i.valid_encoding?}.join,
               "region"       => doc.at_css(".tbl_style tr:nth-child(5) td.arial_bold").text.chars.select{|i| i.valid_encoding?}.join,
               "city"         => doc.at_css(".tbl_style tr:nth-child(6) td.arial_bold").text.chars.select{|i| i.valid_encoding?}.join,
               "zip"          => doc.at_css(".tbl_style tr:nth-child(7) td.arial_bold").text.chars.select{|i| i.valid_encoding?}.join
             }

  rescue Exception => e
    geoip = { "host" => ""}
  end
  if geoip["host"].empty?
    geoip["error"] = "Results Not Found"
  end
  geoip
end

.hostip(ip) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/sgeoip.rb', line 40

def self.hostip(ip)
  begin
    GeoLocation::use = :hostip
    location = GeoLocation.find(ip)
    geoip = {  "host"         => "N/A",
               "country"      => location[:country].chars.select{|i| i.valid_encoding?}.join,
               "country_code" => location[:country_code].chars.select{|i| i.valid_encoding?}.join,
               "region"       => "N/A",
               "city"         => location[:city].chars.select{|i| i.valid_encoding?}.join,
               "zip"          => "N/A"
             }
  rescue Exception => e
    geoip = { "host" => ""}
  end
  if geoip["host"].empty?
    geoip["error"] = "Results Not Found"
  end
  geoip
end

.scrape(ip) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/sgeoip.rb', line 10

def self.scrape(ip)
  # geoip = geoiptool(ip)
  # if geoip["error"]
  #   geoip = hostip(ip)
  # end
  geoip = hostip(ip)
  geoip
end