maxmind-geoip2 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'maxmind/geoip2'
4
+ require 'minitest/autorun'
5
+
6
+ class ModelNameTest < Minitest::Test # :nodoc:
7
+ RAW = {
8
+ 'continent' => {
9
+ 'code' => 'NA',
10
+ 'geoname_id' => 42,
11
+ 'names' => {
12
+ 'en' => 'North America',
13
+ 'zh-CN' => '北美洲',
14
+ },
15
+ },
16
+ 'country' => {
17
+ 'geoname_id' => 1,
18
+ 'iso_code' => 'US',
19
+ 'names' => {
20
+ 'en' => 'United States of America',
21
+ 'ru' => 'объединяет государства',
22
+ 'zh-CN' => '美国',
23
+ },
24
+ },
25
+ 'traits' => {
26
+ 'ip_address' => '1.2.3.4',
27
+ },
28
+ }.freeze
29
+
30
+ def test_fallback
31
+ model = MaxMind::GeoIP2::Model::Country.new(RAW, %w[ru zh-CN en])
32
+ assert_equal('北美洲', model.continent.name)
33
+ assert_equal('объединяет государства', model.country.name)
34
+ end
35
+
36
+ def test_two_fallbacks
37
+ model = MaxMind::GeoIP2::Model::Country.new(RAW, %w[ru jp])
38
+ assert_nil(model.continent.name)
39
+ assert_equal('объединяет государства', model.country.name)
40
+ end
41
+
42
+ def test_no_fallbacks
43
+ model = MaxMind::GeoIP2::Model::Country.new(RAW, %w[jp])
44
+ assert_nil(model.continent.name)
45
+ assert_nil(model.country.name)
46
+ end
47
+ end
@@ -0,0 +1,381 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ipaddr'
4
+ require 'maxmind/geoip2'
5
+ require 'minitest/autorun'
6
+
7
+ class ReaderTest < Minitest::Test # :nodoc:
8
+ def test_city
9
+ reader = MaxMind::GeoIP2::Reader.new(
10
+ 'test/data/test-data/GeoIP2-City-Test.mmdb',
11
+ )
12
+ record = reader.city('2.125.160.216')
13
+
14
+ assert_equal('EU', record.continent.code)
15
+
16
+ assert_equal(2_655_045, record.city.geoname_id)
17
+ assert_equal({ 'en' => 'Boxford' }, record.city.names)
18
+ assert_equal('Boxford', record.city.name)
19
+ assert_nil(record.city.confidence)
20
+
21
+ assert_equal(100, record.location.accuracy_radius)
22
+ assert_equal(51.75, record.location.latitude)
23
+ assert_equal(-1.25, record.location.longitude)
24
+ assert_equal('Europe/London', record.location.time_zone)
25
+
26
+ assert_equal(2, record.subdivisions.size)
27
+ assert_equal('England', record.subdivisions[0].name)
28
+ assert_equal('West Berkshire', record.subdivisions[1].name)
29
+ assert_equal('West Berkshire', record.most_specific_subdivision.name)
30
+
31
+ record = reader.city('216.160.83.56')
32
+
33
+ assert_equal(819, record.location.metro_code)
34
+
35
+ assert_equal('98354', record.postal.code)
36
+
37
+ assert_equal(1, record.subdivisions.size)
38
+ assert_equal(5_815_135, record.subdivisions[0].geoname_id)
39
+ assert_equal('WA', record.subdivisions[0].iso_code)
40
+ assert_equal(
41
+ {
42
+ 'en' => 'Washington',
43
+ 'es' => 'Washington',
44
+ 'fr' => 'État de Washington',
45
+ 'ja' => 'ワシントン州',
46
+ 'ru' => 'Вашингтон',
47
+ 'zh-CN' => '华盛顿州',
48
+ },
49
+ record.subdivisions[0].names,
50
+ assert_equal('WA', record.most_specific_subdivision.iso_code)
51
+ )
52
+
53
+ reader.close
54
+ end
55
+
56
+ def test_city_no_subdivisions
57
+ reader = MaxMind::GeoIP2::Reader.new(
58
+ 'test/data/test-data/GeoIP2-City-Test.mmdb',
59
+ )
60
+ record = reader.city('2001:218::')
61
+
62
+ assert_equal([], record.subdivisions)
63
+ assert_nil(record.most_specific_subdivision)
64
+
65
+ reader.close
66
+ end
67
+
68
+ def test_country
69
+ reader = MaxMind::GeoIP2::Reader.new(
70
+ 'test/data/test-data/GeoIP2-Country-Test.mmdb',
71
+ )
72
+ record = reader.country('2.125.160.216')
73
+
74
+ assert_equal('EU', record.continent.code)
75
+ assert_equal(6_255_148, record.continent.geoname_id)
76
+ assert_equal(
77
+ {
78
+ 'de' => 'Europa',
79
+ 'en' => 'Europe',
80
+ 'es' => 'Europa',
81
+ 'fr' => 'Europe',
82
+ 'ja' => 'ヨーロッパ',
83
+ 'pt-BR' => 'Europa',
84
+ 'ru' => 'Европа',
85
+ 'zh-CN' => '欧洲',
86
+ },
87
+ record.continent.names,
88
+ )
89
+ assert_equal('Europe', record.continent.name)
90
+
91
+ assert_equal(2_635_167, record.country.geoname_id)
92
+ assert_equal(true, record.country.in_european_union?)
93
+ assert_equal('GB', record.country.iso_code)
94
+ assert_equal(
95
+ {
96
+ 'de' => 'Vereinigtes Königreich',
97
+ 'en' => 'United Kingdom',
98
+ 'es' => 'Reino Unido',
99
+ 'fr' => 'Royaume-Uni',
100
+ 'ja' => 'イギリス',
101
+ 'pt-BR' => 'Reino Unido',
102
+ 'ru' => 'Великобритания',
103
+ 'zh-CN' => '英国',
104
+ },
105
+ record.country.names,
106
+ )
107
+ assert_equal('United Kingdom', record.country.name)
108
+
109
+ assert_equal(3_017_382, record.registered_country.geoname_id)
110
+ assert_equal(true, record.registered_country.in_european_union?)
111
+ assert_equal('FR', record.registered_country.iso_code)
112
+ assert_equal(
113
+ {
114
+ 'de' => 'Frankreich',
115
+ 'en' => 'France',
116
+ 'es' => 'Francia',
117
+ 'fr' => 'France',
118
+ 'ja' => 'フランス共和国',
119
+ 'pt-BR' => 'França',
120
+ 'ru' => 'Франция',
121
+ 'zh-CN' => '法国',
122
+ },
123
+ record.registered_country.names,
124
+ )
125
+ assert_equal('France', record.registered_country.name)
126
+
127
+ record = reader.country('202.196.224.0')
128
+
129
+ assert_equal(6_252_001, record.represented_country.geoname_id)
130
+ assert_equal('US', record.represented_country.iso_code)
131
+ assert_equal(
132
+ {
133
+ 'de' => 'USA',
134
+ 'en' => 'United States',
135
+ 'es' => 'Estados Unidos',
136
+ 'fr' => 'États-Unis',
137
+ 'ja' => 'アメリカ合衆国',
138
+ 'pt-BR' => 'Estados Unidos',
139
+ 'ru' => 'США',
140
+ 'zh-CN' => '美国',
141
+ },
142
+ record.represented_country.names,
143
+ )
144
+ assert_equal('United States', record.represented_country.name)
145
+ assert_equal('military', record.represented_country.type)
146
+
147
+ record = reader.country('81.2.69.163')
148
+ assert_equal('81.2.69.163', record.traits.ip_address)
149
+ assert_equal('81.2.69.160/27', record.traits.network)
150
+
151
+ assert_raises(NoMethodError) { record.foo }
152
+ reader.close
153
+ end
154
+
155
+ def test_is_method_returns_false
156
+ reader = MaxMind::GeoIP2::Reader.new(
157
+ 'test/data/test-data/GeoIP2-Country-Test.mmdb',
158
+ )
159
+ record = reader.country('74.209.24.0')
160
+ assert_equal(false, record.country.in_european_union?)
161
+
162
+ reader.close
163
+ end
164
+
165
+ def test_enterprise
166
+ reader = MaxMind::GeoIP2::Reader.new(
167
+ 'test/data/test-data/GeoIP2-Enterprise-Test.mmdb',
168
+ )
169
+ record = reader.enterprise('2.125.160.216')
170
+
171
+ assert_equal(50, record.city.confidence)
172
+ assert_equal(2_655_045, record.city.geoname_id)
173
+ assert_equal({ 'en' => 'Boxford' }, record.city.names)
174
+ assert_equal('Boxford', record.city.name)
175
+
176
+ ip_address = '74.209.24.0'
177
+ record = reader.enterprise(ip_address)
178
+
179
+ assert_equal(11, record.city.confidence)
180
+ assert_equal(99, record.country.confidence)
181
+ assert_equal(6_252_001, record.country.geoname_id)
182
+ assert_equal(false, record.country.in_european_union?)
183
+
184
+ assert_equal(27, record.location.accuracy_radius)
185
+
186
+ assert_equal(false, record.registered_country.in_european_union?)
187
+
188
+ assert_equal('Cable/DSL', record.traits.connection_type)
189
+ assert_equal(true, record.traits.legitimate_proxy?)
190
+
191
+ assert_equal(ip_address, record.traits.ip_address)
192
+ assert_equal('74.209.16.0/20', record.traits.network)
193
+
194
+ reader.close
195
+ end
196
+
197
+ def test_no_traits
198
+ reader = MaxMind::GeoIP2::Reader.new(
199
+ 'test/data/test-data/GeoIP2-Enterprise-Test.mmdb',
200
+ )
201
+ record = reader.enterprise('2.125.160.216')
202
+
203
+ assert_equal('2.125.160.216', record.traits.ip_address)
204
+ assert_equal('2.125.160.216/29', record.traits.network)
205
+ assert_nil(record.traits.autonomous_system_number)
206
+ assert_equal(false, record.traits.anonymous?)
207
+
208
+ reader.close
209
+ end
210
+
211
+ def test_no_location
212
+ reader = MaxMind::GeoIP2::Reader.new(
213
+ 'test/data/test-data/GeoIP2-Enterprise-Test.mmdb',
214
+ )
215
+ record = reader.enterprise('212.47.235.81')
216
+
217
+ assert_nil(record.location.accuracy_radius)
218
+
219
+ reader.close
220
+ end
221
+
222
+ def test_no_postal
223
+ reader = MaxMind::GeoIP2::Reader.new(
224
+ 'test/data/test-data/GeoIP2-Enterprise-Test.mmdb',
225
+ )
226
+ record = reader.enterprise('212.47.235.81')
227
+
228
+ assert_nil(record.postal.code)
229
+
230
+ reader.close
231
+ end
232
+
233
+ def test_no_city
234
+ reader = MaxMind::GeoIP2::Reader.new(
235
+ 'test/data/test-data/GeoIP2-Enterprise-Test.mmdb',
236
+ )
237
+ record = reader.enterprise('212.47.235.81')
238
+
239
+ assert_nil(record.city.confidence)
240
+ assert_nil(record.city.name)
241
+ assert_nil(record.city.names)
242
+
243
+ reader.close
244
+ end
245
+
246
+ def test_no_continent
247
+ reader = MaxMind::GeoIP2::Reader.new(
248
+ 'test/data/test-data/GeoIP2-Enterprise-Test.mmdb',
249
+ )
250
+ record = reader.enterprise('212.47.235.81')
251
+
252
+ assert_nil(record.continent.code)
253
+
254
+ reader.close
255
+ end
256
+
257
+ def test_no_country
258
+ reader = MaxMind::GeoIP2::Reader.new(
259
+ 'test/data/test-data/GeoIP2-Enterprise-Test.mmdb',
260
+ )
261
+ record = reader.enterprise('212.47.235.81')
262
+
263
+ assert_nil(record.country.confidence)
264
+
265
+ reader.close
266
+ end
267
+
268
+ def test_no_represented_country
269
+ reader = MaxMind::GeoIP2::Reader.new(
270
+ 'test/data/test-data/GeoIP2-Enterprise-Test.mmdb',
271
+ )
272
+ record = reader.enterprise('212.47.235.81')
273
+
274
+ assert_nil(record.represented_country.type)
275
+
276
+ reader.close
277
+ end
278
+
279
+ def database_types
280
+ [
281
+ { 'file' => 'City', 'method' => 'city' },
282
+ { 'file' => 'Country', 'method' => 'country' },
283
+ ]
284
+ end
285
+
286
+ def test_default_locale
287
+ database_types.each do |t|
288
+ reader = MaxMind::GeoIP2::Reader.new(
289
+ "test/data/test-data/GeoIP2-#{t['file']}-Test.mmdb",
290
+ )
291
+ record = reader.send(t['method'], '81.2.69.160')
292
+ assert_equal('United Kingdom', record.country.name)
293
+ reader.close
294
+ end
295
+ end
296
+
297
+ def test_locale_list
298
+ database_types.each do |t|
299
+ reader = MaxMind::GeoIP2::Reader.new(
300
+ "test/data/test-data/GeoIP2-#{t['file']}-Test.mmdb",
301
+ %w[xx ru pt-BR es en],
302
+ )
303
+ record = reader.send(t['method'], '81.2.69.160')
304
+ assert_equal('Великобритания', record.country.name)
305
+ reader.close
306
+ end
307
+ end
308
+
309
+ def test_has_ip_address_and_network
310
+ database_types.each do |t|
311
+ reader = MaxMind::GeoIP2::Reader.new(
312
+ "test/data/test-data/GeoIP2-#{t['file']}-Test.mmdb",
313
+ )
314
+ record = reader.send(t['method'], '81.2.69.163')
315
+ assert_equal('81.2.69.163', record.traits.ip_address)
316
+ assert_equal('81.2.69.160/27', record.traits.network)
317
+ reader.close
318
+ end
319
+ end
320
+
321
+ def test_is_in_european_union
322
+ database_types.each do |t|
323
+ reader = MaxMind::GeoIP2::Reader.new(
324
+ "test/data/test-data/GeoIP2-#{t['file']}-Test.mmdb",
325
+ )
326
+ record = reader.send(t['method'], '81.2.69.160')
327
+ assert_equal(true, record.country.in_european_union?)
328
+ assert_equal(false, record.registered_country.in_european_union?)
329
+ reader.close
330
+ end
331
+ end
332
+
333
+ def test_unknown_address
334
+ database_types.each do |t|
335
+ reader = MaxMind::GeoIP2::Reader.new(
336
+ "test/data/test-data/GeoIP2-#{t['file']}-Test.mmdb",
337
+ )
338
+ error = assert_raises(
339
+ MaxMind::GeoIP2::AddressNotFoundError,
340
+ ) { reader.send(t['method'], '10.10.10.0') }
341
+ assert_equal(
342
+ 'The address 10.10.10.0 is not in the database.',
343
+ error.message,
344
+ )
345
+ reader.close
346
+ end
347
+ end
348
+
349
+ def test_incorrect_database
350
+ reader = MaxMind::GeoIP2::Reader.new(
351
+ 'test/data/test-data/GeoIP2-City-Test.mmdb',
352
+ )
353
+ error = assert_raises(ArgumentError) { reader.country('10.10.10.10') }
354
+ assert_equal(
355
+ 'The country method cannot be used with the GeoIP2-City database.',
356
+ error.message,
357
+ )
358
+ reader.close
359
+ end
360
+
361
+ def test_invalid_address
362
+ reader = MaxMind::GeoIP2::Reader.new(
363
+ 'test/data/test-data/GeoIP2-City-Test.mmdb',
364
+ )
365
+ error = assert_raises(
366
+ IPAddr::InvalidAddressError,
367
+ ) { reader.city('invalid') }
368
+ # Ruby 2.5 says just 'invalid address'. Ruby 2.6+ says 'invalid address:
369
+ # invalid'.
370
+ assert_match('invalid address', error.message)
371
+ reader.close
372
+ end
373
+
374
+ def test_metadata
375
+ reader = MaxMind::GeoIP2::Reader.new(
376
+ 'test/data/test-data/GeoIP2-City-Test.mmdb',
377
+ )
378
+ assert_equal('GeoIP2-City', reader.metadata.database_type)
379
+ reader.close
380
+ end
381
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: maxmind-geoip2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - William Storey
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-01-09 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A gem for interacting with the GeoIP2 webservices and databases. MaxMind
14
+ provides geolocation data as downloadable databases as well as through a webservice.
15
+ email: support@maxmind.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - CHANGELOG.md
21
+ - Gemfile
22
+ - Gemfile.lock
23
+ - LICENSE-APACHE
24
+ - LICENSE-MIT
25
+ - README.dev.md
26
+ - README.md
27
+ - Rakefile
28
+ - lib/maxmind/geoip2.rb
29
+ - lib/maxmind/geoip2/errors.rb
30
+ - lib/maxmind/geoip2/model/city.rb
31
+ - lib/maxmind/geoip2/model/country.rb
32
+ - lib/maxmind/geoip2/model/enterprise.rb
33
+ - lib/maxmind/geoip2/reader.rb
34
+ - lib/maxmind/geoip2/record/abstract.rb
35
+ - lib/maxmind/geoip2/record/city.rb
36
+ - lib/maxmind/geoip2/record/continent.rb
37
+ - lib/maxmind/geoip2/record/country.rb
38
+ - lib/maxmind/geoip2/record/location.rb
39
+ - lib/maxmind/geoip2/record/place.rb
40
+ - lib/maxmind/geoip2/record/postal.rb
41
+ - lib/maxmind/geoip2/record/represented_country.rb
42
+ - lib/maxmind/geoip2/record/subdivision.rb
43
+ - lib/maxmind/geoip2/record/traits.rb
44
+ - maxmind-geoip2.gemspec
45
+ - test/test_model_country.rb
46
+ - test/test_model_names.rb
47
+ - test/test_reader.rb
48
+ homepage: https://github.com/maxmind/GeoIP2-ruby
49
+ licenses:
50
+ - Apache-2.0
51
+ - MIT
52
+ metadata:
53
+ bug_tracker_uri: https://github.com/maxmind/GeoIP2-ruby/issues
54
+ changelog_uri: https://github.com/maxmind/GeoIP2-ruby/blob/master/CHANGELOG.md
55
+ documentation_uri: https://github.com/maxmind/GeoIP2-ruby
56
+ homepage_uri: https://github.com/maxmind/GeoIP2-ruby
57
+ source_code_uri: https://github.com/maxmind/GeoIP2-ruby
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 2.4.0
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 2.5.2.1
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: A gem for interacting with the GeoIP2 webservices and databases.
78
+ test_files: []