geokit-geoip 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ geokit-geoip
2
+ ============
3
+
4
+ GeoKit module for using Maxmind GeoIP City database
@@ -0,0 +1,11 @@
1
+ require "rubygems"
2
+ require "bundler/setup"
3
+
4
+ require 'rake/testtask'
5
+ Rake::TestTask.new do |t|
6
+ t.libs << "test"
7
+ t.test_files = FileList['test/test*.rb']
8
+ t.verbose = true
9
+ end
10
+
11
+ task :default => :test
Binary file
@@ -0,0 +1,41 @@
1
+ # Normally we would want this in lib/ext, but it needs to be loaded before the initializers.
2
+
3
+ require 'geokit'
4
+ require 'geoip'
5
+
6
+ module Geokit
7
+ module Geocoders
8
+
9
+ @@geoip_data_path = File.expand_path(File.join(File.dirname(__FILE__),'..','data','GeoLiteCity.dat'))
10
+ __define_accessors
11
+
12
+ # Provide geocoding based upon an IP address. The underlying web service is maxmind.com.
13
+ # MaxMind City is a paid-for service, provides country, region, and city. Updated every month.
14
+ class GeoIpCityGeocoder < Geocoder
15
+ # Given an IP address, returns a GeoLoc instance which contains latitude,
16
+ # longitude, city, and country code. Sets the success attribute to false if the ip
17
+ # parameter does not match an ip address.
18
+ def self.do_geocode(ip, options = {})
19
+ return GeoLoc.new unless /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})?$/.match(ip)
20
+ if (res = ::GeoIP.new(Geocoders::geoip_data_path).city(ip))
21
+ loc = GeoLoc.new({
22
+ :provider => 'maxmind_city',
23
+ :lat => res.latitude,
24
+ :lng => res.longitude,
25
+ :city => res.city_name,
26
+ :state => res.region_name,
27
+ :zip => res.postal_code,
28
+ :country_code => res.country_code2
29
+ })
30
+ # Work around Geokit's jankiness
31
+ loc.success = res.city_name && res.city_name != ''
32
+ loc
33
+ else
34
+ GeoLoc.new
35
+ end
36
+ end
37
+
38
+ end
39
+ end
40
+ end
41
+
@@ -0,0 +1,48 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestGeokitGeoip < Test::Unit::TestCase
4
+
5
+ context "GeoIpCityGeocoder" do
6
+ setup do
7
+ @geocoder = Geokit::Geocoders::GeoIpCityGeocoder
8
+ end
9
+
10
+ should "have a default geoip_data_path" do
11
+ path = File.expand_path(File.join(File.dirname(__FILE__), "..", "data", "GeoLiteCity.dat"))
12
+ assert_equal path, Geokit::Geocoders.geoip_data_path
13
+ end
14
+
15
+ should "be able to set geoip_data_path" do
16
+ orig_path = Geokit::Geocoders.geoip_data_path
17
+ Geokit::Geocoders.geoip_data_path = "foo"
18
+ assert_equal "foo", Geokit::Geocoders.geoip_data_path
19
+ # Reset
20
+ Geokit::Geocoders.geoip_data_path = orig_path
21
+ end
22
+
23
+
24
+ bad_ips = ["nonesuch", "0.0.0.0", "127.0.0.1"]
25
+ bad_ips.each do |bad_ip|
26
+ should "return a blank GeoLoc for #{bad_ip}" do
27
+ assert_equal Geokit::GeoLoc.new, @geocoder.geocode(bad_ip)
28
+ end
29
+ end
30
+
31
+ context "with a good ip" do
32
+ setup { @ip = '67.244.97.190' }
33
+ should "be successful" do
34
+ result = @geocoder.geocode(@ip)
35
+ assert result.success?, result.city
36
+ end
37
+ should "set the right attributes" do
38
+ loc = @geocoder.geocode(@ip)
39
+ assert_equal "New York", loc.city
40
+ assert_equal "NY", loc.state
41
+ assert_equal "US", loc.country_code
42
+ assert (40..41).include?(loc.lat)
43
+ assert (-74..073).include?(loc.lng)
44
+ assert 5, loc.zip.size
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,4 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/geokit-geoip'
3
+ require 'shoulda'
4
+ require 'ruby-debug'
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: geokit-geoip
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Aaron Suggs
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-05-01 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: geoip
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: geokit
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :runtime
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: rake
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ type: :development
61
+ version_requirements: *id003
62
+ - !ruby/object:Gem::Dependency
63
+ name: shoulda
64
+ prerelease: false
65
+ requirement: &id004 !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ hash: 3
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ type: :development
75
+ version_requirements: *id004
76
+ - !ruby/object:Gem::Dependency
77
+ name: ruby-debug
78
+ prerelease: false
79
+ requirement: &id005 !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ hash: 3
85
+ segments:
86
+ - 0
87
+ version: "0"
88
+ type: :development
89
+ version_requirements: *id005
90
+ description: GeoKit module for using Maxmind GeoIP City database
91
+ email: aaron@ktheory.com
92
+ executables: []
93
+
94
+ extensions: []
95
+
96
+ extra_rdoc_files: []
97
+
98
+ files:
99
+ - data/GeoLiteCity.dat
100
+ - lib/geokit-geoip.rb
101
+ - README.md
102
+ - Rakefile
103
+ - test/test_geokit_geoip.rb
104
+ - test/test_helper.rb
105
+ homepage: http://github.com/kickstarter/geokit-geoip
106
+ licenses: []
107
+
108
+ post_install_message:
109
+ rdoc_options:
110
+ - --charset=UTF-8
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ hash: 3
119
+ segments:
120
+ - 0
121
+ version: "0"
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ none: false
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ hash: 3
128
+ segments:
129
+ - 1
130
+ - 5
131
+ - 0
132
+ version: 1.5.0
133
+ requirements: []
134
+
135
+ rubyforge_project:
136
+ rubygems_version: 1.8.22
137
+ signing_key:
138
+ specification_version: 3
139
+ summary: GeoKit module for GeoIP
140
+ test_files:
141
+ - test/test_geokit_geoip.rb
142
+ - test/test_helper.rb