geo_location 0.3.3 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +6 -4
- data/VERSION +1 -1
- data/geo_location.gemspec +5 -3
- data/lib/geo_location/countries.rb +33 -0
- data/lib/geo_location/countries.txt +249 -0
- data/lib/geo_location/geo_location.rb +6 -4
- data/lib/geo_location/{variables.rb → setup.rb} +5 -1
- data/lib/geo_location/timezones.rb +6 -8
- data/lib/geo_location.rb +2 -1
- data/test/test_geo_location.rb +27 -3
- metadata +7 -5
data/README.rdoc
CHANGED
@@ -20,12 +20,13 @@ config/initializers/geo_location_config.rb:
|
|
20
20
|
|
21
21
|
== HostIP Example
|
22
22
|
|
23
|
-
location = GeoLocation.find('24.24.24.24') # => {:ip=>"24.24.24.24", :city=>"Liverpool", :region=>"NY", :country=>"US", :latitude=>"43.1059", :longitude=>"-76.2099", :timezone=>"America/New_York"}
|
23
|
+
location = GeoLocation.find('24.24.24.24') # => {:ip=>"24.24.24.24", :city=>"Liverpool", :region=>"NY", :country=>"United States", :country_code=>"US", :latitude=>"43.1059", :longitude=>"-76.2099", :timezone=>"America/New_York"}
|
24
24
|
|
25
25
|
puts location[:ip] # => 24.24.24.24
|
26
26
|
puts location[:city] # => Liverpool
|
27
27
|
puts location[:region] # => NY
|
28
|
-
puts location[:country] # =>
|
28
|
+
puts location[:country] # => United States
|
29
|
+
puts location[:country_code] # => US
|
29
30
|
puts location[:latitude] # => 43.1059
|
30
31
|
puts location[:longitude] # => -76.2099
|
31
32
|
puts location[:timezone] # => America/New_York
|
@@ -51,12 +52,13 @@ config/initializers/geo_location_config.rb:
|
|
51
52
|
|
52
53
|
GeoLocation::use = :maxmind
|
53
54
|
GeoLocation::key = 'YOUR MaxMind.COM LICENSE KEY'
|
54
|
-
location = GeoLocation.find('24.24.24.24') # => {:ip=>"24.24.24.24", :city=>"Jamaica", :region=>"NY", :country=>"US", :latitude=>"40.676300", :longitude=>"-73.775200", :timezone=>"America/New_York"}
|
55
|
+
location = GeoLocation.find('24.24.24.24') # => {:ip=>"24.24.24.24", :city=>"Jamaica", :region=>"NY", :country=>"United States", :country_code=>"US", :latitude=>"40.676300", :longitude=>"-73.775200", :timezone=>"America/New_York"}
|
55
56
|
|
56
57
|
puts location[:ip] # => 24.24.24.24
|
57
58
|
puts location[:city] # => Jamaica
|
58
59
|
puts location[:region] # => NY
|
59
|
-
puts location[:country] # =>
|
60
|
+
puts location[:country] # => United States
|
61
|
+
puts location[:country_code] # => US
|
60
62
|
puts location[:latitude] # => 40.676300
|
61
63
|
puts location[:longitude] # => -73.775200
|
62
64
|
puts location[:timezone] # => America/New_York
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.0
|
data/geo_location.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{geo_location}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.4.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Chris Your"]
|
12
|
-
s.date = %q{2010-08-
|
12
|
+
s.date = %q{2010-08-29}
|
13
13
|
s.description = %q{Geo-locate your users using their IP address via hostip.info or maxmind.com.}
|
14
14
|
s.email = %q{chris@ignitionindustries.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -25,10 +25,12 @@ Gem::Specification.new do |s|
|
|
25
25
|
"VERSION",
|
26
26
|
"geo_location.gemspec",
|
27
27
|
"lib/geo_location.rb",
|
28
|
+
"lib/geo_location/countries.rb",
|
29
|
+
"lib/geo_location/countries.txt",
|
28
30
|
"lib/geo_location/geo_location.rb",
|
31
|
+
"lib/geo_location/setup.rb",
|
29
32
|
"lib/geo_location/timezones.rb",
|
30
33
|
"lib/geo_location/timezones.txt",
|
31
|
-
"lib/geo_location/variables.rb",
|
32
34
|
"lib/geo_location/version.rb",
|
33
35
|
"test/helper.rb",
|
34
36
|
"test/test_geo_location.rb"
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module GeoLocation
|
2
|
+
|
3
|
+
class << self
|
4
|
+
|
5
|
+
def country(country_code)
|
6
|
+
return nil if GeoLocation::countries.empty?
|
7
|
+
GeoLocation::countries[country_code.to_sym]
|
8
|
+
end
|
9
|
+
|
10
|
+
def build_countries
|
11
|
+
if GeoLocation::countries.empty?
|
12
|
+
data = {}
|
13
|
+
|
14
|
+
file = File.join(File.dirname(__FILE__), 'countries.txt')
|
15
|
+
File.open(file, "r") do |infile|
|
16
|
+
while (line = infile.gets)
|
17
|
+
countries = line.split("\n")
|
18
|
+
countries.each do |c|
|
19
|
+
c = c.split(" ")
|
20
|
+
code = c[0].to_sym
|
21
|
+
country = c[1]
|
22
|
+
data[code] = country
|
23
|
+
end # end countries.each
|
24
|
+
end # end while
|
25
|
+
end # end file.open
|
26
|
+
|
27
|
+
GeoLocation::countries = data
|
28
|
+
end # end if
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,249 @@
|
|
1
|
+
A1 Anonymous Proxy
|
2
|
+
A2 Satellite Provider
|
3
|
+
O1 Other Country
|
4
|
+
AD Andorra
|
5
|
+
AE United Arab Emirates
|
6
|
+
AF Afghanistan
|
7
|
+
AG Antigua and Barbuda
|
8
|
+
AI Anguilla
|
9
|
+
AL Albania
|
10
|
+
AM Armenia
|
11
|
+
AN Netherlands Antilles
|
12
|
+
AO Angola
|
13
|
+
AP Asia/Pacific Region
|
14
|
+
AQ Antarctica
|
15
|
+
AR Argentina
|
16
|
+
AS American Samoa
|
17
|
+
AT Austria
|
18
|
+
AU Australia
|
19
|
+
AW Aruba
|
20
|
+
AX Aland Islands
|
21
|
+
AZ Azerbaijan
|
22
|
+
BA Bosnia and Herzegovina
|
23
|
+
BB Barbados
|
24
|
+
BD Bangladesh
|
25
|
+
BE Belgium
|
26
|
+
BF Burkina Faso
|
27
|
+
BG Bulgaria
|
28
|
+
BH Bahrain
|
29
|
+
BI Burundi
|
30
|
+
BJ Benin
|
31
|
+
BM Bermuda
|
32
|
+
BN Brunei Darussalam
|
33
|
+
BO Bolivia
|
34
|
+
BR Brazil
|
35
|
+
BS Bahamas
|
36
|
+
BT Bhutan
|
37
|
+
BV Bouvet Island
|
38
|
+
BW Botswana
|
39
|
+
BY Belarus
|
40
|
+
BZ Belize
|
41
|
+
CA Canada
|
42
|
+
CC Cocos (Keeling) Islands
|
43
|
+
CD Congo, The Democratic Republic of the
|
44
|
+
CF Central African Republic
|
45
|
+
CG Congo
|
46
|
+
CH Switzerland
|
47
|
+
CI Cote d'Ivoire
|
48
|
+
CK Cook Islands
|
49
|
+
CL Chile
|
50
|
+
CM Cameroon
|
51
|
+
CN China
|
52
|
+
CO Colombia
|
53
|
+
CR Costa Rica
|
54
|
+
CU Cuba
|
55
|
+
CV Cape Verde
|
56
|
+
CX Christmas Island
|
57
|
+
CY Cyprus
|
58
|
+
CZ Czech Republic
|
59
|
+
DE Germany
|
60
|
+
DJ Djibouti
|
61
|
+
DK Denmark
|
62
|
+
DM Dominica
|
63
|
+
DO Dominican Republic
|
64
|
+
DZ Algeria
|
65
|
+
EC Ecuador
|
66
|
+
EE Estonia
|
67
|
+
EG Egypt
|
68
|
+
EH Western Sahara
|
69
|
+
ER Eritrea
|
70
|
+
ES Spain
|
71
|
+
ET Ethiopia
|
72
|
+
EU Europe
|
73
|
+
FI Finland
|
74
|
+
FJ Fiji
|
75
|
+
FK Falkland Islands (Malvinas)
|
76
|
+
FM Micronesia, Federated States of
|
77
|
+
FO Faroe Islands
|
78
|
+
FR France
|
79
|
+
GA Gabon
|
80
|
+
GB United Kingdom
|
81
|
+
GD Grenada
|
82
|
+
GE Georgia
|
83
|
+
GF French Guiana
|
84
|
+
GG Guernsey
|
85
|
+
GH Ghana
|
86
|
+
GI Gibraltar
|
87
|
+
GL Greenland
|
88
|
+
GM Gambia
|
89
|
+
GN Guinea
|
90
|
+
GP Guadeloupe
|
91
|
+
GQ Equatorial Guinea
|
92
|
+
GR Greece
|
93
|
+
GS South Georgia and the South Sandwich Islands
|
94
|
+
GT Guatemala
|
95
|
+
GU Guam
|
96
|
+
GW Guinea-Bissau
|
97
|
+
GY Guyana
|
98
|
+
HK Hong Kong
|
99
|
+
HM Heard Island and McDonald Islands
|
100
|
+
HN Honduras
|
101
|
+
HR Croatia
|
102
|
+
HT Haiti
|
103
|
+
HU Hungary
|
104
|
+
ID Indonesia
|
105
|
+
IE Ireland
|
106
|
+
IL Israel
|
107
|
+
IM Isle of Man
|
108
|
+
IN India
|
109
|
+
IO British Indian Ocean Territory
|
110
|
+
IQ Iraq
|
111
|
+
IR Iran, Islamic Republic of
|
112
|
+
IS Iceland
|
113
|
+
IT Italy
|
114
|
+
JE Jersey
|
115
|
+
JM Jamaica
|
116
|
+
JO Jordan
|
117
|
+
JP Japan
|
118
|
+
KE Kenya
|
119
|
+
KG Kyrgyzstan
|
120
|
+
KH Cambodia
|
121
|
+
KI Kiribati
|
122
|
+
KM Comoros
|
123
|
+
KN Saint Kitts and Nevis
|
124
|
+
KP Korea, Democratic People's Republic of
|
125
|
+
KR Korea, Republic of
|
126
|
+
KW Kuwait
|
127
|
+
KY Cayman Islands
|
128
|
+
KZ Kazakhstan
|
129
|
+
LA Lao People's Democratic Republic
|
130
|
+
LB Lebanon
|
131
|
+
LC Saint Lucia
|
132
|
+
LI Liechtenstein
|
133
|
+
LK Sri Lanka
|
134
|
+
LR Liberia
|
135
|
+
LS Lesotho
|
136
|
+
LT Lithuania
|
137
|
+
LU Luxembourg
|
138
|
+
LV Latvia
|
139
|
+
LY Libyan Arab Jamahiriya
|
140
|
+
MA Morocco
|
141
|
+
MC Monaco
|
142
|
+
MD Moldova, Republic of
|
143
|
+
ME Montenegro
|
144
|
+
MG Madagascar
|
145
|
+
MH Marshall Islands
|
146
|
+
MK Macedonia
|
147
|
+
ML Mali
|
148
|
+
MM Myanmar
|
149
|
+
MN Mongolia
|
150
|
+
MO Macao
|
151
|
+
MP Northern Mariana Islands
|
152
|
+
MQ Martinique
|
153
|
+
MR Mauritania
|
154
|
+
MS Montserrat
|
155
|
+
MT Malta
|
156
|
+
MU Mauritius
|
157
|
+
MV Maldives
|
158
|
+
MW Malawi
|
159
|
+
MX Mexico
|
160
|
+
MY Malaysia
|
161
|
+
MZ Mozambique
|
162
|
+
NA Namibia
|
163
|
+
NC New Caledonia
|
164
|
+
NE Niger
|
165
|
+
NF Norfolk Island
|
166
|
+
NG Nigeria
|
167
|
+
NI Nicaragua
|
168
|
+
NL Netherlands
|
169
|
+
NO Norway
|
170
|
+
NP Nepal
|
171
|
+
NR Nauru
|
172
|
+
NU Niue
|
173
|
+
NZ New Zealand
|
174
|
+
OM Oman
|
175
|
+
PA Panama
|
176
|
+
PE Peru
|
177
|
+
PF French Polynesia
|
178
|
+
PG Papua New Guinea
|
179
|
+
PH Philippines
|
180
|
+
PK Pakistan
|
181
|
+
PL Poland
|
182
|
+
PM Saint Pierre and Miquelon
|
183
|
+
PN Pitcairn
|
184
|
+
PR Puerto Rico
|
185
|
+
PS Palestinian Territory
|
186
|
+
PT Portugal
|
187
|
+
PW Palau
|
188
|
+
PY Paraguay
|
189
|
+
QA Qatar
|
190
|
+
RE Reunion
|
191
|
+
RO Romania
|
192
|
+
RS Serbia
|
193
|
+
RU Russian Federation
|
194
|
+
RW Rwanda
|
195
|
+
SA Saudi Arabia
|
196
|
+
SB Solomon Islands
|
197
|
+
SC Seychelles
|
198
|
+
SD Sudan
|
199
|
+
SE Sweden
|
200
|
+
SG Singapore
|
201
|
+
SH Saint Helena
|
202
|
+
SI Slovenia
|
203
|
+
SJ Svalbard and Jan Mayen
|
204
|
+
SK Slovakia
|
205
|
+
SL Sierra Leone
|
206
|
+
SM San Marino
|
207
|
+
SN Senegal
|
208
|
+
SO Somalia
|
209
|
+
SR Suriname
|
210
|
+
ST Sao Tome and Principe
|
211
|
+
SV El Salvador
|
212
|
+
SY Syrian Arab Republic
|
213
|
+
SZ Swaziland
|
214
|
+
TC Turks and Caicos Islands
|
215
|
+
TD Chad
|
216
|
+
TF French Southern Territories
|
217
|
+
TG Togo
|
218
|
+
TH Thailand
|
219
|
+
TJ Tajikistan
|
220
|
+
TK Tokelau
|
221
|
+
TL Timor-Leste
|
222
|
+
TM Turkmenistan
|
223
|
+
TN Tunisia
|
224
|
+
TO Tonga
|
225
|
+
TR Turkey
|
226
|
+
TT Trinidad and Tobago
|
227
|
+
TV Tuvalu
|
228
|
+
TW Taiwan
|
229
|
+
TZ Tanzania, United Republic of
|
230
|
+
UA Ukraine
|
231
|
+
UG Uganda
|
232
|
+
UM United States Minor Outlying Islands
|
233
|
+
US United States
|
234
|
+
UY Uruguay
|
235
|
+
UZ Uzbekistan
|
236
|
+
VA Holy See (Vatican City State)
|
237
|
+
VC Saint Vincent and the Grenadines
|
238
|
+
VE Venezuela
|
239
|
+
VG Virgin Islands, British
|
240
|
+
VI Virgin Islands, U.S.
|
241
|
+
VN Vietnam
|
242
|
+
VU Vanuatu
|
243
|
+
WF Wallis and Futuna
|
244
|
+
WS Samoa
|
245
|
+
YE Yemen
|
246
|
+
YT Mayotte
|
247
|
+
ZA South Africa
|
248
|
+
ZM Zambia
|
249
|
+
ZW Zimbabwe
|
@@ -72,13 +72,14 @@ module GeoLocation
|
|
72
72
|
def data_from_maxmind_http_response(ip, body)
|
73
73
|
location = body.split(',')
|
74
74
|
data = {}
|
75
|
-
data[:
|
75
|
+
data[:country_code] = location[0]
|
76
|
+
data[:country] = country(location[0])
|
76
77
|
data[:region] = location[1]
|
77
78
|
data[:city] = location[2]
|
78
79
|
data[:latitude] = location[3]
|
79
80
|
data[:longitude] = location[4]
|
80
81
|
data[:ip] = ip
|
81
|
-
data[:timezone] = timezone(data[:
|
82
|
+
data[:timezone] = timezone(data[:country_code], data[:region])
|
82
83
|
data
|
83
84
|
end
|
84
85
|
|
@@ -99,7 +100,8 @@ module GeoLocation
|
|
99
100
|
data[:city] = element.text.split(', ')[0].titleize
|
100
101
|
data[:region] = element.text.split(', ')[1]
|
101
102
|
when 'countryAbbrev'
|
102
|
-
data[:
|
103
|
+
data[:country_code] = element.text
|
104
|
+
data[:country] = country(element.text)
|
103
105
|
end
|
104
106
|
end
|
105
107
|
|
@@ -114,7 +116,7 @@ module GeoLocation
|
|
114
116
|
end
|
115
117
|
|
116
118
|
data[:ip] = ip
|
117
|
-
data[:timezone] = timezone(data[:
|
119
|
+
data[:timezone] = timezone(data[:country_code], data[:region])
|
118
120
|
|
119
121
|
data
|
120
122
|
end
|
@@ -4,8 +4,9 @@ module GeoLocation
|
|
4
4
|
@@dev = nil
|
5
5
|
@@dev_ip = nil
|
6
6
|
@@timezones = {}
|
7
|
+
@@countries = {}
|
7
8
|
|
8
|
-
[:use, :key, :dev, :dev_ip, :timezones].each do |sym|
|
9
|
+
[:use, :key, :dev, :dev_ip, :timezones, :countries].each do |sym|
|
9
10
|
class_eval <<-EOS, __FILE__, __LINE__
|
10
11
|
def self.#{sym}
|
11
12
|
if defined?(#{sym.to_s.upcase})
|
@@ -21,4 +22,7 @@ module GeoLocation
|
|
21
22
|
EOS
|
22
23
|
end
|
23
24
|
|
25
|
+
GeoLocation.build_countries
|
26
|
+
GeoLocation.build_timezones
|
27
|
+
|
24
28
|
end
|
@@ -3,18 +3,16 @@ module GeoLocation
|
|
3
3
|
class << self
|
4
4
|
|
5
5
|
def timezone(country, region=nil)
|
6
|
-
build_timezones
|
7
6
|
return nil if GeoLocation::timezones.empty?
|
8
7
|
(region.nil? || region.empty?) ? GeoLocation::timezones[country.to_sym] : GeoLocation::timezones[country.to_sym][region.to_sym]
|
9
8
|
end
|
10
9
|
|
11
|
-
private
|
12
|
-
|
13
10
|
def build_timezones
|
14
11
|
if GeoLocation::timezones.empty?
|
15
12
|
data = {}
|
16
13
|
|
17
|
-
File.
|
14
|
+
file = File.join(File.dirname(__FILE__), 'timezones.txt')
|
15
|
+
File.open(file, "r") do |infile|
|
18
16
|
while (line = infile.gets)
|
19
17
|
zones = line.split("\n")
|
20
18
|
zones.each do |z|
|
@@ -22,20 +20,20 @@ module GeoLocation
|
|
22
20
|
country = zone[0].to_sym
|
23
21
|
region = zone[1].to_sym
|
24
22
|
value = zone[2]
|
25
|
-
|
23
|
+
|
26
24
|
data[country] = {} if data[country].nil?
|
27
25
|
if region.to_s.empty?
|
28
26
|
data[country] = value
|
29
27
|
else
|
30
28
|
data[country][region] = value
|
31
29
|
end
|
32
|
-
|
30
|
+
|
33
31
|
end # end zones.each
|
34
32
|
end # end while
|
35
33
|
end # end file.open
|
36
|
-
|
34
|
+
|
37
35
|
GeoLocation::timezones = data
|
38
|
-
end # end
|
36
|
+
end # end if
|
39
37
|
end
|
40
38
|
|
41
39
|
end
|
data/lib/geo_location.rb
CHANGED
data/test/test_geo_location.rb
CHANGED
@@ -20,8 +20,12 @@ class TestGeoLocation < Test::Unit::TestCase
|
|
20
20
|
assert_equal '24.24.24.24', @location[:ip]
|
21
21
|
end
|
22
22
|
|
23
|
-
should "find US
|
24
|
-
assert_equal 'US', @location[:
|
23
|
+
should "find US country_code" do
|
24
|
+
assert_equal 'US', @location[:country_code]
|
25
|
+
end
|
26
|
+
|
27
|
+
should "find United States country" do
|
28
|
+
assert_equal 'United States', @location[:country]
|
25
29
|
end
|
26
30
|
|
27
31
|
should "find NY region" do
|
@@ -64,8 +68,12 @@ class TestGeoLocation < Test::Unit::TestCase
|
|
64
68
|
assert_equal '24.24.24.24', @location[:ip]
|
65
69
|
end
|
66
70
|
|
71
|
+
should "find US country_code" do
|
72
|
+
assert_equal 'US', @location[:country_code]
|
73
|
+
end
|
74
|
+
|
67
75
|
should "find US country" do
|
68
|
-
assert_equal '
|
76
|
+
assert_equal 'United States', @location[:country]
|
69
77
|
end
|
70
78
|
|
71
79
|
should "find NY region" do
|
@@ -121,4 +129,20 @@ class TestGeoLocation < Test::Unit::TestCase
|
|
121
129
|
|
122
130
|
end
|
123
131
|
|
132
|
+
context 'on countries' do
|
133
|
+
|
134
|
+
should "find country Canada" do
|
135
|
+
assert_equal 'Canada', GeoLocation.country('CA')
|
136
|
+
end
|
137
|
+
|
138
|
+
should "find country United States" do
|
139
|
+
assert_equal 'United States', GeoLocation.country('US')
|
140
|
+
end
|
141
|
+
|
142
|
+
should "find country United Kingdom" do
|
143
|
+
assert_equal 'United Kingdom', GeoLocation.country('GB')
|
144
|
+
end
|
145
|
+
|
146
|
+
end
|
147
|
+
|
124
148
|
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 0.
|
7
|
+
- 4
|
8
|
+
- 0
|
9
|
+
version: 0.4.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Chris Your
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-08-
|
17
|
+
date: 2010-08-29 00:00:00 -06:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -48,10 +48,12 @@ files:
|
|
48
48
|
- VERSION
|
49
49
|
- geo_location.gemspec
|
50
50
|
- lib/geo_location.rb
|
51
|
+
- lib/geo_location/countries.rb
|
52
|
+
- lib/geo_location/countries.txt
|
51
53
|
- lib/geo_location/geo_location.rb
|
54
|
+
- lib/geo_location/setup.rb
|
52
55
|
- lib/geo_location/timezones.rb
|
53
56
|
- lib/geo_location/timezones.txt
|
54
|
-
- lib/geo_location/variables.rb
|
55
57
|
- lib/geo_location/version.rb
|
56
58
|
- test/helper.rb
|
57
59
|
- test/test_geo_location.rb
|