geo_location 0.1.3
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/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +41 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/geo_location.gemspec +57 -0
- data/lib/geo_location.rb +5 -0
- data/lib/geo_location/geo_location.rb +102 -0
- data/lib/geo_location/variables.rb +21 -0
- data/lib/geo_location/version.rb +3 -0
- data/test/helper.rb +10 -0
- data/test/test_geo_location.rb +21 -0
- metadata +90 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Chris Your
|
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,41 @@
|
|
1
|
+
= Geo Location
|
2
|
+
|
3
|
+
Geo Locate your users using their IP address via hostip.info or maxmind.com.
|
4
|
+
|
5
|
+
|
6
|
+
== Installation
|
7
|
+
|
8
|
+
gem install geo_location
|
9
|
+
|
10
|
+
|
11
|
+
== Configuration
|
12
|
+
|
13
|
+
config/initializers/geo_location_config.rb:
|
14
|
+
|
15
|
+
unless GeoLocation == nil
|
16
|
+
# Hostip (free)
|
17
|
+
GeoLocation::use = :hostip
|
18
|
+
|
19
|
+
# Max Mind (paid)
|
20
|
+
# For more information visit: http://www.maxmind.com/app/city
|
21
|
+
# GeoLocation::use = :maxmind
|
22
|
+
# GeoLocation::key = 'YOUR MaxMind.COM LICENSE KEY'
|
23
|
+
# GeoLocation::test = 'US,NY,Jamaica,40.676300,-73.775200' # Use this location while you test
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
== Example
|
28
|
+
|
29
|
+
ip = GeoLocation.find('24.24.24.24') # => {:city=>"Jamaica", :region=>"NY", :country=>"US", :latitude=>"40.676300", :longitude=>"-73.775200"}
|
30
|
+
|
31
|
+
puts ip[:city] # => Sugar Grove
|
32
|
+
puts ip[:region] # => IL
|
33
|
+
puts ip[:country] # => UNITED STATES
|
34
|
+
puts ip[:country_code] # => US
|
35
|
+
puts ip[:latitude] # => 40.676300
|
36
|
+
puts ip[:longitude] # => -73.775200
|
37
|
+
|
38
|
+
|
39
|
+
== Copyright
|
40
|
+
|
41
|
+
Copyright (c) 2010 Chris Your. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "geo_location"
|
8
|
+
gem.summary = %Q{Geo-locate your users using their IP address via hostip.info or maxmind.com.}
|
9
|
+
gem.description = %Q{Geo-locate your users using their IP address via hostip.info or maxmind.com.}
|
10
|
+
gem.email = "chris@ignitionindustries.com"
|
11
|
+
gem.homepage = "http://github.com/chrisyour/geo_location"
|
12
|
+
gem.authors = ["Chris Your"]
|
13
|
+
gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
|
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 'rake/testtask'
|
22
|
+
Rake::TestTask.new(:test) do |test|
|
23
|
+
test.libs << 'lib' << 'test'
|
24
|
+
test.pattern = 'test/**/test_*.rb'
|
25
|
+
test.verbose = true
|
26
|
+
end
|
27
|
+
|
28
|
+
begin
|
29
|
+
require 'rcov/rcovtask'
|
30
|
+
Rcov::RcovTask.new do |test|
|
31
|
+
test.libs << 'test'
|
32
|
+
test.pattern = 'test/**/test_*.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
rescue LoadError
|
36
|
+
task :rcov do
|
37
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
task :test => :check_dependencies
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "geo_location #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.3
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{geo_location}
|
8
|
+
s.version = "0.1.3"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Chris Your"]
|
12
|
+
s.date = %q{2010-08-24}
|
13
|
+
s.description = %q{Geo-locate your users using their IP address via hostip.info or maxmind.com.}
|
14
|
+
s.email = %q{chris@ignitionindustries.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"geo_location.gemspec",
|
27
|
+
"lib/geo_location.rb",
|
28
|
+
"lib/geo_location/geo_location.rb",
|
29
|
+
"lib/geo_location/variables.rb",
|
30
|
+
"lib/geo_location/version.rb",
|
31
|
+
"test/helper.rb",
|
32
|
+
"test/test_geo_location.rb"
|
33
|
+
]
|
34
|
+
s.homepage = %q{http://github.com/chrisyour/geo_location}
|
35
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
36
|
+
s.require_paths = ["lib"]
|
37
|
+
s.rubygems_version = %q{1.3.7}
|
38
|
+
s.summary = %q{Geo-locate your users using their IP address via hostip.info or maxmind.com.}
|
39
|
+
s.test_files = [
|
40
|
+
"test/helper.rb",
|
41
|
+
"test/test_geo_location.rb"
|
42
|
+
]
|
43
|
+
|
44
|
+
if s.respond_to? :specification_version then
|
45
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
46
|
+
s.specification_version = 3
|
47
|
+
|
48
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
49
|
+
s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
50
|
+
else
|
51
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
52
|
+
end
|
53
|
+
else
|
54
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
data/lib/geo_location.rb
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'rexml/document'
|
3
|
+
include REXML
|
4
|
+
|
5
|
+
module GeoLocation
|
6
|
+
|
7
|
+
class << self
|
8
|
+
|
9
|
+
def find(ip)
|
10
|
+
return nil unless valid_ip(ip)
|
11
|
+
return (GeoLocation::use == :maxmind) ? maxmind(ip) : hostip(ip)
|
12
|
+
end
|
13
|
+
|
14
|
+
def valid_ip(ip)
|
15
|
+
ip =~ /(([2]([0-4][0-9]|[5][0-5])|[0-1]?[0-9]?[0-9])[.]){3}(([2]([0-4][0-9]|[5][0-5])|[0-1]?[0-9]?[0-9]))/i
|
16
|
+
end
|
17
|
+
|
18
|
+
# == Sample text output from Max Mind
|
19
|
+
# US,NY,Jamaica,40.676300,-73.775200
|
20
|
+
|
21
|
+
def maxmind(ip)
|
22
|
+
unless GeoLocation::test.nil? || GeoLocation::test.empty?
|
23
|
+
puts "WARNING: GeoLocation is using the test location: #{GeoLocation::test}"
|
24
|
+
location = GeoLocation::test.split(',')
|
25
|
+
else
|
26
|
+
url = "http://geoip3.maxmind.com/b?l=#{GeoLocation::key}&i=#{ip}"
|
27
|
+
uri = URI.parse(url)
|
28
|
+
location = Net::HTTP.get_response(uri).body.split(',')
|
29
|
+
end
|
30
|
+
|
31
|
+
data = {}
|
32
|
+
data[:country] = location[0]
|
33
|
+
data[:region] = location[1]
|
34
|
+
data[:city] = location[2]
|
35
|
+
data[:latitude] = location[3]
|
36
|
+
data[:longitude] = location[4]
|
37
|
+
|
38
|
+
data
|
39
|
+
end
|
40
|
+
|
41
|
+
# == Sample XML output from hostip.info (http://api.hostip.info/?ip=12.215.42.19)
|
42
|
+
# <?xml version="1.0" encoding="ISO-8859-1" ?>
|
43
|
+
# <HostipLookupResultSet version="1.0.1" xmlns:gml="http://www.opengis.net/gml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.hostip.info/api/hostip-1.0.1.xsd">
|
44
|
+
# <gml:description>This is the Hostip Lookup Service</gml:description>
|
45
|
+
# <gml:name>hostip</gml:name>
|
46
|
+
# <gml:boundedBy>
|
47
|
+
# <gml:Null>inapplicable</gml:Null>
|
48
|
+
# </gml:boundedBy>
|
49
|
+
# <gml:featureMember>
|
50
|
+
# <Hostip>
|
51
|
+
# <ip>142.59.52.238</ip>
|
52
|
+
# <gml:name>EDMONTON, AB</gml:name>
|
53
|
+
# <countryName>CANADA</countryName>
|
54
|
+
# <countryAbbrev>CA</countryAbbrev>
|
55
|
+
# <!-- Co-ordinates are available as lng,lat -->
|
56
|
+
# <ipLocation>
|
57
|
+
# <gml:pointProperty>
|
58
|
+
# <gml:Point srsName="http://www.opengis.net/gml/srs/epsg.xml#4326">
|
59
|
+
# <gml:coordinates>-113.467,53.55</gml:coordinates>
|
60
|
+
# </gml:Point>
|
61
|
+
# </gml:pointProperty>
|
62
|
+
# </ipLocation>
|
63
|
+
# </Hostip>
|
64
|
+
# </gml:featureMember>
|
65
|
+
# </HostipLookupResultSet>
|
66
|
+
|
67
|
+
def hostip(ip)
|
68
|
+
url = "http://api.hostip.info/?ip=#{ip}"
|
69
|
+
uri = URI.parse(url)
|
70
|
+
xml = Net::HTTP.get_response(uri).body
|
71
|
+
location = REXML::Document.new(xml)
|
72
|
+
|
73
|
+
data = {}
|
74
|
+
|
75
|
+
hostip = XPath.first( location, "//Hostip" )
|
76
|
+
return nil if hostip.nil?
|
77
|
+
|
78
|
+
hostip.elements.each do |element|
|
79
|
+
case element.name
|
80
|
+
when 'name'
|
81
|
+
data[:city] = element.text.split(', ')[0].titleize
|
82
|
+
data[:region] = element.text.split(', ')[1]
|
83
|
+
when 'countryAbbrev'
|
84
|
+
data[:country] = element.text
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
coords = XPath.first( location, "//gml:Point" )
|
89
|
+
coords.elements.each do |element|
|
90
|
+
case element.name
|
91
|
+
when 'coordinates'
|
92
|
+
coordinates = element.text.split(',')
|
93
|
+
data[:latitude] = coordinates[1]
|
94
|
+
data[:longitude] = coordinates[0]
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
data
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
102
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module GeoLocation
|
2
|
+
@@use = :hostip
|
3
|
+
@@key = ''
|
4
|
+
@@test = ''
|
5
|
+
|
6
|
+
[:use, :key, :test].each do |sym|
|
7
|
+
class_eval <<-EOS, __FILE__, __LINE__
|
8
|
+
def self.#{sym}
|
9
|
+
if defined?(#{sym.to_s.upcase})
|
10
|
+
#{sym.to_s.upcase}
|
11
|
+
else
|
12
|
+
@@#{sym}
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.#{sym}=(obj)
|
17
|
+
@@#{sym} = obj
|
18
|
+
end
|
19
|
+
EOS
|
20
|
+
end
|
21
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestGeoLocation < Test::Unit::TestCase
|
4
|
+
should "find CA country using hostip" do
|
5
|
+
GeoLocation::use = :hostip
|
6
|
+
ip = GeoLocation.find('142.59.52.238')
|
7
|
+
assert_equal ip[:country], 'CA'
|
8
|
+
end
|
9
|
+
|
10
|
+
should "find NY region using hostip" do
|
11
|
+
GeoLocation::use = :hostip
|
12
|
+
ip = GeoLocation.find('24.24.24.24')
|
13
|
+
assert_equal ip[:region], 'NY'
|
14
|
+
end
|
15
|
+
|
16
|
+
should "find Edmonton city using hostip" do
|
17
|
+
GeoLocation::use = :hostip
|
18
|
+
ip = GeoLocation.find('142.59.52.238')
|
19
|
+
assert_equal ip[:city], 'Edmonton'
|
20
|
+
end
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: geo_location
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 3
|
9
|
+
version: 0.1.3
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Chris Your
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-08-24 00:00:00 -06:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: thoughtbot-shoulda
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
version: "0"
|
31
|
+
type: :development
|
32
|
+
version_requirements: *id001
|
33
|
+
description: Geo-locate your users using their IP address via hostip.info or maxmind.com.
|
34
|
+
email: chris@ignitionindustries.com
|
35
|
+
executables: []
|
36
|
+
|
37
|
+
extensions: []
|
38
|
+
|
39
|
+
extra_rdoc_files:
|
40
|
+
- LICENSE
|
41
|
+
- README.rdoc
|
42
|
+
files:
|
43
|
+
- .document
|
44
|
+
- .gitignore
|
45
|
+
- LICENSE
|
46
|
+
- README.rdoc
|
47
|
+
- Rakefile
|
48
|
+
- VERSION
|
49
|
+
- geo_location.gemspec
|
50
|
+
- lib/geo_location.rb
|
51
|
+
- lib/geo_location/geo_location.rb
|
52
|
+
- lib/geo_location/variables.rb
|
53
|
+
- lib/geo_location/version.rb
|
54
|
+
- test/helper.rb
|
55
|
+
- test/test_geo_location.rb
|
56
|
+
has_rdoc: true
|
57
|
+
homepage: http://github.com/chrisyour/geo_location
|
58
|
+
licenses: []
|
59
|
+
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options:
|
62
|
+
- --charset=UTF-8
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
segments:
|
71
|
+
- 0
|
72
|
+
version: "0"
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
version: "0"
|
81
|
+
requirements: []
|
82
|
+
|
83
|
+
rubyforge_project:
|
84
|
+
rubygems_version: 1.3.7
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: Geo-locate your users using their IP address via hostip.info or maxmind.com.
|
88
|
+
test_files:
|
89
|
+
- test/helper.rb
|
90
|
+
- test/test_geo_location.rb
|