sgeoip 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/sgeoip.rb CHANGED
@@ -1,14 +1,24 @@
1
1
  require "sgeoip/version"
2
2
  require 'nokogiri'
3
3
  require 'open-uri'
4
+ require 'active_support'
5
+ require 'geo_location'
4
6
 
5
7
  module Sgeoip
6
8
  def self.scrape(ip)
9
+ geoip = geoiptool(ip)
10
+ if geoip["error"]
11
+ geoip = hostip(ip)
12
+ end
13
+ geoip
14
+ end
15
+
16
+ def self.geoiptool(ip)
7
17
  url = "http://www.geoiptool.com/en/?IP=#{ip}"
8
18
  begin
9
19
  doc = Nokogiri::HTML(open(url))
10
20
  geoip = { "host" => doc.at_css(".tbl_style tr:nth-child(1) td.arial_bold").text,
11
- "country" => doc.at_css(".tbl_style tr:nth-child(3) td.arial_bold").text,
21
+ "country" => doc.at_css(".tbl_style tr:nth-child(3) td.arial_bold").text.to_s[1..-2], #strip extra spaces
12
22
  "country_code" => doc.at_css(".tbl_style tr:nth-child(4) td.arial_bold").text,
13
23
  "region" => doc.at_css(".tbl_style tr:nth-child(5) td.arial_bold").text,
14
24
  "city" => doc.at_css(".tbl_style tr:nth-child(6) td.arial_bold").text,
@@ -22,4 +32,25 @@ module Sgeoip
22
32
  end
23
33
  geoip
24
34
  end
35
+
36
+ def self.hostip(ip)
37
+ begin
38
+ GeoLocation::use = :hostip
39
+ location = GeoLocation.find(ip)
40
+ geoip = { "host" => "N/A",
41
+ "country" => location[:country],
42
+ "country_code" => location[:country_code],
43
+ "region" => "N/A",
44
+ "city" => location[:city],
45
+ "zip" => "N/A"
46
+ }
47
+ rescue Exception => e
48
+ geoip = { "host" => ""}
49
+ end
50
+ if geoip["host"].empty?
51
+ geoip["error"] = "Results Not Found"
52
+ end
53
+ geoip
54
+ end
55
+
25
56
  end
@@ -1,3 +1,3 @@
1
1
  module Sgeoip
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/sgeoip-0.1.0.gem ADDED
Binary file
data/sgeoip.gemspec CHANGED
@@ -18,5 +18,7 @@ Gem::Specification.new do |gem|
18
18
  gem.require_paths = ["lib"]
19
19
 
20
20
  gem.add_dependency 'nokogiri'
21
+ gem.add_dependency 'geo_location'
22
+ gem.add_dependency 'active_support'
21
23
  gem.add_development_dependency 'rspec'
22
24
  end
data/spec/sgeoip_spec.rb CHANGED
@@ -5,7 +5,7 @@ describe Sgeoip do
5
5
  ip_info = Sgeoip::scrape('8.8.8.8')
6
6
 
7
7
  ip_info["host"].should == "google-public-dns-a.google.com"
8
- ip_info["country"].should == " United States "
8
+ ip_info["country"].should == "United States"
9
9
  ip_info["country_code"].should == "US (USA)"
10
10
  ip_info["region"].should == "California"
11
11
  ip_info["city"].should == "Mountain View"
@@ -13,9 +13,34 @@ describe Sgeoip do
13
13
  ip_info["error"].should be_nil
14
14
  end
15
15
 
16
+ it 'should return correct info for ip using geoip tool' do
17
+ ip_info = Sgeoip::geoiptool('8.8.8.8')
18
+
19
+ ip_info["host"].should == "google-public-dns-a.google.com"
20
+ ip_info["country"].should == "United States"
21
+ ip_info["country_code"].should == "US (USA)"
22
+ ip_info["region"].should == "California"
23
+ ip_info["city"].should == "Mountain View"
24
+ ip_info["zip"].should == "94043"
25
+ ip_info["error"].should be_nil
26
+ end
27
+
28
+ it 'should return correct info for ip using hostip' do
29
+ ip_info = Sgeoip::hostip('8.8.8.8')
30
+
31
+ ip_info["host"].should == "N/A"
32
+ ip_info["country"].should == "United States"
33
+ ip_info["country_code"].should == "US"
34
+ ip_info["region"].should == "N/A"
35
+ ip_info["city"].should == "Mountain View"
36
+ ip_info["zip"].should == "N/A"
37
+ ip_info["error"].should be_nil
38
+ end
39
+
16
40
  it 'should return an error for a bad ip' do
17
41
  ip_info = Sgeoip::scrape('1.c.aa.p/')
18
42
 
19
43
  ip_info["error"].should == "Results Not Found"
20
44
  end
45
+
21
46
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sgeoip
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-26 00:00:00.000000000 Z
12
+ date: 2012-10-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
@@ -27,6 +27,38 @@ dependencies:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: geo_location
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: active_support
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
30
62
  - !ruby/object:Gem::Dependency
31
63
  name: rspec
32
64
  requirement: !ruby/object:Gem::Requirement
@@ -58,7 +90,7 @@ files:
58
90
  - Rakefile
59
91
  - lib/sgeoip.rb
60
92
  - lib/sgeoip/version.rb
61
- - sgeoip-0.0.9.gem
93
+ - sgeoip-0.1.0.gem
62
94
  - sgeoip.gemspec
63
95
  - spec/sgeoip_spec.rb
64
96
  - spec/spec_helper.rb
data/sgeoip-0.0.9.gem DELETED
Binary file