geo_ip 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +57 -0
- data/Rakefile +45 -0
- data/VERSION +1 -0
- data/init.rb +1 -0
- data/lib/geo_ip.rb +37 -0
- data/spec/geo_ip_spec.rb +19 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +9 -0
- metadata +76 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Jeroen Jacobs
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
= GeoIp
|
2
|
+
|
3
|
+
Retreive the geolocation of an IP address based on the {ipinfodb.com}[http://ipinfodb.com/] webservice.
|
4
|
+
|
5
|
+
Consider making a donation to {ipinfodb.com}[http://ipinfodb.com/] at {http://ipinfodb.com/donate.php}[http://ipinfodb.com/donate.php]
|
6
|
+
|
7
|
+
== Usage
|
8
|
+
|
9
|
+
=== Retreive geolocation
|
10
|
+
GeoIp.remote_geolocation(ip_address)
|
11
|
+
|
12
|
+
=== Example
|
13
|
+
|
14
|
+
# 209.85.227.104 = google.be (US)
|
15
|
+
GeoIp.remote_geolocation('209.85.227.104')
|
16
|
+
|
17
|
+
returns:
|
18
|
+
|
19
|
+
{
|
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
|
+
:timezone=>"-8",
|
31
|
+
:gmt_offset=>"-8",
|
32
|
+
:dst_offset=>"-7",
|
33
|
+
}
|
34
|
+
|
35
|
+
== Getting it
|
36
|
+
|
37
|
+
GeoIp can be installed as a Ruby Gem:
|
38
|
+
|
39
|
+
gem install geo_ip
|
40
|
+
|
41
|
+
Alternatively, you can also install it as a Rails plugin:
|
42
|
+
|
43
|
+
./script/plugin install git://github.com/jeroenj/geo_ip.git
|
44
|
+
|
45
|
+
== Note on Patches/Pull Requests
|
46
|
+
|
47
|
+
* Fork the project.
|
48
|
+
* Make your feature addition or bug fix.
|
49
|
+
* Add tests for it. This is important so I don't break it in a
|
50
|
+
future version unintentionally.
|
51
|
+
* Commit, do not mess with rakefile, version, or history.
|
52
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
53
|
+
* Send me a pull request. Bonus points for topic branches.
|
54
|
+
|
55
|
+
== Copyright
|
56
|
+
|
57
|
+
Copyright (c) 2010 Jeroen Jacobs, {redstorm sprl}[http://www.redstorm.be]. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "geo_ip"
|
8
|
+
gem.summary = "Retreive the geolocation of an IP address based on the ipinfodb.com webservice"
|
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 = "jj@redstorm.com"
|
11
|
+
gem.homepage = "http://github.com/jeroenj/geo_ip"
|
12
|
+
gem.authors = ["Jeroen Jacobs"]
|
13
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'spec/rake/spectask'
|
22
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
23
|
+
spec.libs << 'lib' << 'spec'
|
24
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
25
|
+
end
|
26
|
+
|
27
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
28
|
+
spec.libs << 'lib' << 'spec'
|
29
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
30
|
+
spec.rcov = true
|
31
|
+
end
|
32
|
+
|
33
|
+
task :spec => :check_dependencies
|
34
|
+
|
35
|
+
task :default => :spec
|
36
|
+
|
37
|
+
require 'rake/rdoctask'
|
38
|
+
Rake::RDocTask.new do |rdoc|
|
39
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
40
|
+
|
41
|
+
rdoc.rdoc_dir = 'rdoc'
|
42
|
+
rdoc.title = "geo_ip #{version}"
|
43
|
+
rdoc.rdoc_files.include('README*')
|
44
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
45
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'geo_ip'
|
data/lib/geo_ip.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
SERVICE_URL = "http://ipinfodb.com/ip_query.php"
|
2
|
+
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/
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'json'
|
6
|
+
require 'uri'
|
7
|
+
require 'net/http'
|
8
|
+
|
9
|
+
class GeoIp
|
10
|
+
def self.remote_geolocation(ip)
|
11
|
+
raise "Invalid IP address" unless ip.to_s =~ IPV4_REGEXP
|
12
|
+
|
13
|
+
uri = SERVICE_URL + "?ip=#{ip}&output=json"
|
14
|
+
url = URI.parse(uri)
|
15
|
+
reply = JSON.parse(Net::HTTP.get(url))
|
16
|
+
location = convert_keys reply
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
def self.convert_keys(hash)
|
21
|
+
location = {}
|
22
|
+
location[:ip] = hash["Ip"]
|
23
|
+
location[:status] = hash["Status"]
|
24
|
+
location[:country_code] = hash["CountryCode"]
|
25
|
+
location[:country_name] = hash["CountryName"]
|
26
|
+
location[:region_code] = hash["RegionCode"]
|
27
|
+
location[:region_name] = hash["RegionName"]
|
28
|
+
location[:city] = hash["City"]
|
29
|
+
location[:zip_postal_code] = hash["ZipPostalCode"]
|
30
|
+
location[:latitude] = hash["Latitude"]
|
31
|
+
location[:longitude] = hash["Longitude"]
|
32
|
+
location[:timezone] = hash["Timezone"]
|
33
|
+
location[:gmt_offset] = hash["Gmtoffset"]
|
34
|
+
location[:dst_offset] = hash["Dstoffset"]
|
35
|
+
location
|
36
|
+
end
|
37
|
+
end
|
data/spec/geo_ip_spec.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "GeoIp" do
|
4
|
+
it "should return the correct country for a public ip address" do
|
5
|
+
# 209.85.227.104 = google.be (US)
|
6
|
+
GeoIp.remote_geolocation('209.85.227.104')[:country_code].should == 'US'
|
7
|
+
GeoIp.remote_geolocation('209.85.227.104')[:country_name].should == 'United States'
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should return the correct country for a private ip address" do
|
11
|
+
GeoIp.remote_geolocation('10.0.0.1')[:country_code].should == 'RD'
|
12
|
+
GeoIp.remote_geolocation('10.0.0.1')[:country_name].should == 'Reserved'
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should return the correct country for localhost ip address" do
|
16
|
+
GeoIp.remote_geolocation('127.0.0.1')[:country_code].should == 'RD'
|
17
|
+
GeoIp.remote_geolocation('127.0.0.1')[:country_name].should == 'Reserved'
|
18
|
+
end
|
19
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: geo_ip
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jeroen Jacobs
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-01-09 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.2.9
|
24
|
+
version:
|
25
|
+
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.
|
26
|
+
email: jj@redstorm.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- .document
|
36
|
+
- .gitignore
|
37
|
+
- LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
- Rakefile
|
40
|
+
- VERSION
|
41
|
+
- init.rb
|
42
|
+
- lib/geo_ip.rb
|
43
|
+
- spec/geo_ip_spec.rb
|
44
|
+
- spec/spec.opts
|
45
|
+
- spec/spec_helper.rb
|
46
|
+
has_rdoc: true
|
47
|
+
homepage: http://github.com/jeroenj/geo_ip
|
48
|
+
licenses: []
|
49
|
+
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options:
|
52
|
+
- --charset=UTF-8
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: "0"
|
60
|
+
version:
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: "0"
|
66
|
+
version:
|
67
|
+
requirements: []
|
68
|
+
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 1.3.5
|
71
|
+
signing_key:
|
72
|
+
specification_version: 3
|
73
|
+
summary: Retreive the geolocation of an IP address based on the ipinfodb.com webservice
|
74
|
+
test_files:
|
75
|
+
- spec/geo_ip_spec.rb
|
76
|
+
- spec/spec_helper.rb
|