geo_ip 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/CHANGES +7 -0
- data/README.rdoc +78 -17
- data/Rakefile +3 -2
- data/VERSION +1 -1
- data/geo_ip.gemspec +12 -8
- data/lib/geo_ip.rb +12 -7
- data/spec/api.yml.example +1 -0
- data/spec/geo_ip_spec.rb +19 -9
- metadata +35 -11
data/.gitignore
CHANGED
data/CHANGES
CHANGED
@@ -1,8 +1,15 @@
|
|
1
|
+
== 0.3.0
|
2
|
+
|
3
|
+
* Added support for API key requirement (Thanks to seanconaty and luigi)
|
4
|
+
* Explicit gem dependency for json and removed rubygems requirement (idris) (http://tomayko.com/writings/require-rubygems-antipattern)
|
5
|
+
* Removed deprecated GeoIp#remote_geolocation method
|
6
|
+
|
1
7
|
== 0.2.0
|
2
8
|
|
3
9
|
* Added support for timezone information. Use the optional {:timezone => true|false} option
|
4
10
|
* Added support for country lookup. This will result in a faster reply since less queries need
|
5
11
|
to be done at ipinfodb's side. Use the optional {:precision => :city|:country} option
|
12
|
+
* API change: GeoIp.remote_geolocation(ip) is deprecated in favor of GeoIp.geolocation(ip)
|
6
13
|
|
7
14
|
== 0.1.1
|
8
15
|
|
data/README.rdoc
CHANGED
@@ -2,36 +2,81 @@
|
|
2
2
|
|
3
3
|
Retreive the geolocation of an IP address based on the {ipinfodb.com}[http://ipinfodb.com/] webservice.
|
4
4
|
|
5
|
+
As of 8th November 2010, the service is asking that all users {register}[http://ipinfodb.com/register.php] for an API key.
|
6
|
+
|
5
7
|
Consider making a donation to {ipinfodb.com}[http://ipinfodb.com/] at {http://ipinfodb.com/donate.php}[http://ipinfodb.com/donate.php]
|
6
8
|
|
7
9
|
== Usage
|
8
10
|
|
9
|
-
===
|
10
|
-
GeoIp.
|
11
|
+
=== Set API key
|
12
|
+
GeoIp.api_key = "YOUR_API_KEY"
|
13
|
+
|
14
|
+
This must be done before making the geolocation call.
|
15
|
+
|
16
|
+
=== Retrieve geolocation
|
17
|
+
GeoIp.geolocation(ip_address)
|
11
18
|
|
12
19
|
=== Example
|
13
20
|
|
14
21
|
# 209.85.227.104 = google.be (US)
|
15
|
-
GeoIp.
|
22
|
+
GeoIp.geolocation('209.85.227.104')
|
23
|
+
|
24
|
+
returns:
|
25
|
+
|
26
|
+
{
|
27
|
+
:status =>"OK",
|
28
|
+
:ip =>"209.85.227.104"
|
29
|
+
:country_code =>"US",
|
30
|
+
:country_name =>"United States",
|
31
|
+
:region_code =>"06",
|
32
|
+
:region_name =>"California",
|
33
|
+
:city =>"Mountain View",
|
34
|
+
:zip_postal_code =>"94043",
|
35
|
+
:latitude =>"37.4192",
|
36
|
+
:longitude =>"-122.057"
|
37
|
+
}
|
38
|
+
|
39
|
+
=== Country only
|
40
|
+
|
41
|
+
There is an option to only retreive the country information and thus excluding the city details. This results in a faster response from the service since less queries need to be done.
|
42
|
+
|
43
|
+
GeoIp.geolocation('209.85.227.104', {:precision => :country})
|
44
|
+
|
45
|
+
returns:
|
46
|
+
|
47
|
+
{
|
48
|
+
:status => "OK",
|
49
|
+
:ip => "209.85.227.104"
|
50
|
+
:country_code => "US",
|
51
|
+
:country_name => "United States"
|
52
|
+
}
|
53
|
+
|
54
|
+
=== Timezone information
|
55
|
+
|
56
|
+
There is an option now to retrieve optional timezone information too:
|
57
|
+
|
58
|
+
GeoIp.geolocation('209.85.227.104', {:timezone => true})
|
16
59
|
|
17
60
|
returns:
|
18
61
|
|
19
62
|
{
|
20
|
-
:status=>"OK",
|
21
|
-
:ip=>"209.85.227.104"
|
22
|
-
:country_code=>"US",
|
23
|
-
:country_name=>"United States",
|
24
|
-
:region_code=>"06",
|
25
|
-
:region_name=>"California",
|
26
|
-
:city=>"Mountain View",
|
27
|
-
:zip_postal_code=>"94043",
|
28
|
-
:latitude=>"37.4192",
|
29
|
-
:longitude=>"-122.057"
|
30
|
-
:
|
31
|
-
:
|
32
|
-
:
|
63
|
+
:status =>"OK",
|
64
|
+
:ip =>"209.85.227.104"
|
65
|
+
:country_code =>"US",
|
66
|
+
:country_name =>"United States",
|
67
|
+
:region_code =>"06",
|
68
|
+
:region_name =>"California",
|
69
|
+
:city =>"Mountain View",
|
70
|
+
:zip_postal_code =>"94043",
|
71
|
+
:latitude =>"37.4192",
|
72
|
+
:longitude =>"-122.057"
|
73
|
+
:timezone_name =>"America/Los_Angeles",
|
74
|
+
:utc_offset =>-25200,
|
75
|
+
:dst? =>true
|
33
76
|
}
|
34
77
|
|
78
|
+
Obviously it is not possible to have the country precision enabled while retrieving the timezone information.
|
79
|
+
|
35
80
|
== Getting it
|
36
81
|
|
37
82
|
GeoIp can be installed as a Ruby Gem:
|
@@ -42,8 +87,24 @@ Alternatively, you can also install it as a Rails plugin:
|
|
42
87
|
|
43
88
|
./script/plugin install git://github.com/jeroenj/geo_ip.git
|
44
89
|
|
90
|
+
== Testing
|
91
|
+
|
92
|
+
Set up your API key first for the test suite by creating a spec/api.yml file. Follow the example in spec/api.yml.example. Then run the tests with:
|
93
|
+
|
94
|
+
ruby spec/geo_ip_spec.rb
|
95
|
+
|
96
|
+
If you get a LoadError, you should run the tests with:
|
97
|
+
|
98
|
+
ruby -rubygems spec/geo_ip_spec.rb
|
99
|
+
|
100
|
+
== Colaborators
|
101
|
+
|
102
|
+
* {seanconaty}[https://github.com/seanconaty]
|
103
|
+
* {luigi}[https://github.com/luigi]
|
104
|
+
* {idris}[https://github.com/idris]
|
105
|
+
|
45
106
|
== Note on Patches/Pull Requests
|
46
|
-
|
107
|
+
|
47
108
|
* Fork the project.
|
48
109
|
* Make your feature addition or bug fix.
|
49
110
|
* Add tests for it. This is important so I don't break it in a
|
data/Rakefile
CHANGED
@@ -7,10 +7,11 @@ begin
|
|
7
7
|
gem.name = "geo_ip"
|
8
8
|
gem.summary = "Retreive the geolocation of an IP address based on the ipinfodb.com webservice"
|
9
9
|
gem.description = "A call to the ipinfodb.com will be done to retreive the geolocation based on the IP address. No need to include a database file in the application."
|
10
|
-
gem.email = "
|
10
|
+
gem.email = "jacobsjeroen@gmail.com"
|
11
11
|
gem.homepage = "http://github.com/jeroenj/geo_ip"
|
12
12
|
gem.authors = ["Jeroen Jacobs"]
|
13
|
-
gem.
|
13
|
+
gem.add_dependency "json", "~> 1.4.6"
|
14
|
+
gem.add_development_dependency "rspec", "~> 1.3.0"
|
14
15
|
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
16
|
end
|
16
17
|
Jeweler::GemcutterTasks.new
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
data/geo_ip.gemspec
CHANGED
@@ -5,13 +5,13 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{geo_ip}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.3.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Jeroen Jacobs"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-11-16}
|
13
13
|
s.description = %q{A call to the ipinfodb.com will be done to retreive the geolocation based on the IP address. No need to include a database file in the application.}
|
14
|
-
s.email = %q{
|
14
|
+
s.email = %q{jacobsjeroen@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE",
|
17
17
|
"README.rdoc"
|
@@ -27,6 +27,7 @@ Gem::Specification.new do |s|
|
|
27
27
|
"geo_ip.gemspec",
|
28
28
|
"init.rb",
|
29
29
|
"lib/geo_ip.rb",
|
30
|
+
"spec/api.yml.example",
|
30
31
|
"spec/geo_ip_spec.rb",
|
31
32
|
"spec/spec.opts",
|
32
33
|
"spec/spec_helper.rb"
|
@@ -34,7 +35,7 @@ Gem::Specification.new do |s|
|
|
34
35
|
s.homepage = %q{http://github.com/jeroenj/geo_ip}
|
35
36
|
s.rdoc_options = ["--charset=UTF-8"]
|
36
37
|
s.require_paths = ["lib"]
|
37
|
-
s.rubygems_version = %q{1.3.
|
38
|
+
s.rubygems_version = %q{1.3.7}
|
38
39
|
s.summary = %q{Retreive the geolocation of an IP address based on the ipinfodb.com webservice}
|
39
40
|
s.test_files = [
|
40
41
|
"spec/geo_ip_spec.rb",
|
@@ -45,13 +46,16 @@ Gem::Specification.new do |s|
|
|
45
46
|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
46
47
|
s.specification_version = 3
|
47
48
|
|
48
|
-
if Gem::Version.new(Gem::
|
49
|
-
s.
|
49
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
50
|
+
s.add_runtime_dependency(%q<json>, ["~> 1.4.6"])
|
51
|
+
s.add_development_dependency(%q<rspec>, ["~> 1.3.0"])
|
50
52
|
else
|
51
|
-
s.add_dependency(%q<
|
53
|
+
s.add_dependency(%q<json>, ["~> 1.4.6"])
|
54
|
+
s.add_dependency(%q<rspec>, ["~> 1.3.0"])
|
52
55
|
end
|
53
56
|
else
|
54
|
-
s.add_dependency(%q<
|
57
|
+
s.add_dependency(%q<json>, ["~> 1.4.6"])
|
58
|
+
s.add_dependency(%q<rspec>, ["~> 1.3.0"])
|
55
59
|
end
|
56
60
|
end
|
57
61
|
|
data/lib/geo_ip.rb
CHANGED
@@ -1,18 +1,22 @@
|
|
1
|
-
SERVICE_URL = "http://ipinfodb.com/"
|
1
|
+
SERVICE_URL = "http://api.ipinfodb.com/v2/"
|
2
2
|
CITY_API = "ip_query.php"
|
3
3
|
COUNTRY_API = "ip_query_country.php"
|
4
4
|
IPV4_REGEXP = /\A(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)(?:\.(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)){3}\z/
|
5
5
|
|
6
|
-
require 'rubygems'
|
7
6
|
require 'json'
|
8
7
|
require 'uri'
|
9
8
|
require 'net/http'
|
10
9
|
|
11
10
|
class GeoIp
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
11
|
+
|
12
|
+
@@api_key = nil
|
13
|
+
|
14
|
+
def self.api_key
|
15
|
+
@@api_key
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.api_key=(api_key)
|
19
|
+
@@api_key = api_key
|
16
20
|
end
|
17
21
|
|
18
22
|
# Retreive the remote location of a given ip address.
|
@@ -26,10 +30,11 @@ class GeoIp
|
|
26
30
|
def self.geolocation(ip, options={})
|
27
31
|
@precision = options[:precision] || :city
|
28
32
|
@timezone = options[:timezone] || false
|
33
|
+
raise "API key must be set first: GeoIp.api_key = 'YOURKEY'" if self.api_key.nil?
|
29
34
|
raise "Invalid IP address" unless ip.to_s =~ IPV4_REGEXP
|
30
35
|
raise "Invalid precision" unless [:country, :city].include?(@precision)
|
31
36
|
raise "Invalid timezone" unless [true, false].include?(@timezone)
|
32
|
-
uri = "#{SERVICE_URL}#{@country ? COUNTRY_API : CITY_API}?ip=#{ip}&output=json&timezone=#{@timezone}"
|
37
|
+
uri = "#{SERVICE_URL}#{@country ? COUNTRY_API : CITY_API}?key=#{self.api_key}&ip=#{ip}&output=json&timezone=#{@timezone}"
|
33
38
|
url = URI.parse(uri)
|
34
39
|
reply = JSON.parse(Net::HTTP.get(url))
|
35
40
|
location = convert_keys reply
|
@@ -0,0 +1 @@
|
|
1
|
+
key: YOUR_KEY_HERE
|
data/spec/geo_ip_spec.rb
CHANGED
@@ -4,6 +4,24 @@ IP_PRIVATE = '10.0.0.1'
|
|
4
4
|
IP_LOCAL = '127.0.0.1'
|
5
5
|
|
6
6
|
describe "GeoIp" do
|
7
|
+
|
8
|
+
before(:all) do
|
9
|
+
api_config = YAML.load_file(File.dirname(__FILE__) + '/api.yml')
|
10
|
+
GeoIp.api_key = api_config['key']
|
11
|
+
end
|
12
|
+
|
13
|
+
context "api_key" do
|
14
|
+
it "should return the API key when set" do
|
15
|
+
GeoIp.api_key = "my_api_key"
|
16
|
+
GeoIp.api_key.should == "my_api_key"
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should throw an error when API key is not set" do
|
20
|
+
GeoIp.api_key = nil
|
21
|
+
lambda {GeoIp.geolocation(IP_GOOGLE_US)}.should raise_error
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
7
25
|
context "city" do
|
8
26
|
it "should return the correct city for a public ip address" do
|
9
27
|
geolocation = GeoIp.geolocation(IP_GOOGLE_US)
|
@@ -65,7 +83,7 @@ describe "GeoIp" do
|
|
65
83
|
it "should return the correct timezone information for a public ip address" do
|
66
84
|
geolocation = GeoIp.geolocation(IP_GOOGLE_US, {:timezone => true})
|
67
85
|
geolocation[:timezone_name].should == 'America/Los_Angeles'
|
68
|
-
geolocation[:utc_offset].should == -
|
86
|
+
geolocation[:utc_offset].should == -28800
|
69
87
|
geolocation[:dst?].should_not be_nil # true if dst?, false if not dst?
|
70
88
|
end
|
71
89
|
|
@@ -90,12 +108,4 @@ describe "GeoIp" do
|
|
90
108
|
geolocation[:dst?].should be_nil
|
91
109
|
end
|
92
110
|
end
|
93
|
-
|
94
|
-
context "deprecated" do
|
95
|
-
it "should return the correct country for a public ip address" do
|
96
|
-
geolocation = GeoIp.remote_geolocation(IP_GOOGLE_US)
|
97
|
-
geolocation[:country_code].should == 'US'
|
98
|
-
geolocation[:country_name].should == 'United States'
|
99
|
-
end
|
100
|
-
end
|
101
111
|
end
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: geo_ip
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 19
|
4
5
|
prerelease: false
|
5
6
|
segments:
|
6
7
|
- 0
|
7
|
-
-
|
8
|
+
- 3
|
8
9
|
- 0
|
9
|
-
version: 0.
|
10
|
+
version: 0.3.0
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Jeroen Jacobs
|
@@ -14,25 +15,43 @@ autorequire:
|
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date: 2010-
|
18
|
+
date: 2010-11-16 00:00:00 +01:00
|
18
19
|
default_executable:
|
19
20
|
dependencies:
|
20
21
|
- !ruby/object:Gem::Dependency
|
21
|
-
name:
|
22
|
+
name: json
|
22
23
|
prerelease: false
|
23
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
24
26
|
requirements:
|
25
|
-
- -
|
27
|
+
- - ~>
|
26
28
|
- !ruby/object:Gem::Version
|
29
|
+
hash: 11
|
27
30
|
segments:
|
28
31
|
- 1
|
29
|
-
-
|
30
|
-
-
|
31
|
-
version: 1.
|
32
|
-
type: :
|
32
|
+
- 4
|
33
|
+
- 6
|
34
|
+
version: 1.4.6
|
35
|
+
type: :runtime
|
33
36
|
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: rspec
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 27
|
46
|
+
segments:
|
47
|
+
- 1
|
48
|
+
- 3
|
49
|
+
- 0
|
50
|
+
version: 1.3.0
|
51
|
+
type: :development
|
52
|
+
version_requirements: *id002
|
34
53
|
description: A call to the ipinfodb.com will be done to retreive the geolocation based on the IP address. No need to include a database file in the application.
|
35
|
-
email:
|
54
|
+
email: jacobsjeroen@gmail.com
|
36
55
|
executables: []
|
37
56
|
|
38
57
|
extensions: []
|
@@ -51,6 +70,7 @@ files:
|
|
51
70
|
- geo_ip.gemspec
|
52
71
|
- init.rb
|
53
72
|
- lib/geo_ip.rb
|
73
|
+
- spec/api.yml.example
|
54
74
|
- spec/geo_ip_spec.rb
|
55
75
|
- spec/spec.opts
|
56
76
|
- spec/spec_helper.rb
|
@@ -64,23 +84,27 @@ rdoc_options:
|
|
64
84
|
require_paths:
|
65
85
|
- lib
|
66
86
|
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
67
88
|
requirements:
|
68
89
|
- - ">="
|
69
90
|
- !ruby/object:Gem::Version
|
91
|
+
hash: 3
|
70
92
|
segments:
|
71
93
|
- 0
|
72
94
|
version: "0"
|
73
95
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
74
97
|
requirements:
|
75
98
|
- - ">="
|
76
99
|
- !ruby/object:Gem::Version
|
100
|
+
hash: 3
|
77
101
|
segments:
|
78
102
|
- 0
|
79
103
|
version: "0"
|
80
104
|
requirements: []
|
81
105
|
|
82
106
|
rubyforge_project:
|
83
|
-
rubygems_version: 1.3.
|
107
|
+
rubygems_version: 1.3.7
|
84
108
|
signing_key:
|
85
109
|
specification_version: 3
|
86
110
|
summary: Retreive the geolocation of an IP address based on the ipinfodb.com webservice
|