maxmind-geoip2 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +17 -0
  3. data/Gemfile +11 -0
  4. data/Gemfile.lock +70 -0
  5. data/LICENSE-APACHE +202 -0
  6. data/LICENSE-MIT +17 -0
  7. data/README.dev.md +4 -0
  8. data/README.md +326 -0
  9. data/Rakefile +14 -0
  10. data/lib/maxmind/geoip2.rb +4 -0
  11. data/lib/maxmind/geoip2/client.rb +313 -0
  12. data/lib/maxmind/geoip2/errors.rb +48 -0
  13. data/lib/maxmind/geoip2/model/abstract.rb +27 -0
  14. data/lib/maxmind/geoip2/model/anonymous_ip.rb +63 -0
  15. data/lib/maxmind/geoip2/model/asn.rb +39 -0
  16. data/lib/maxmind/geoip2/model/city.rb +75 -0
  17. data/lib/maxmind/geoip2/model/connection_type.rb +32 -0
  18. data/lib/maxmind/geoip2/model/country.rb +70 -0
  19. data/lib/maxmind/geoip2/model/domain.rb +32 -0
  20. data/lib/maxmind/geoip2/model/enterprise.rb +15 -0
  21. data/lib/maxmind/geoip2/model/insights.rb +14 -0
  22. data/lib/maxmind/geoip2/model/isp.rb +53 -0
  23. data/lib/maxmind/geoip2/reader.rb +277 -0
  24. data/lib/maxmind/geoip2/record/abstract.rb +22 -0
  25. data/lib/maxmind/geoip2/record/city.rb +38 -0
  26. data/lib/maxmind/geoip2/record/continent.rb +37 -0
  27. data/lib/maxmind/geoip2/record/country.rb +54 -0
  28. data/lib/maxmind/geoip2/record/location.rb +73 -0
  29. data/lib/maxmind/geoip2/record/maxmind.rb +17 -0
  30. data/lib/maxmind/geoip2/record/place.rb +28 -0
  31. data/lib/maxmind/geoip2/record/postal.rb +30 -0
  32. data/lib/maxmind/geoip2/record/represented_country.rb +23 -0
  33. data/lib/maxmind/geoip2/record/subdivision.rb +48 -0
  34. data/lib/maxmind/geoip2/record/traits.rb +200 -0
  35. data/maxmind-geoip2.gemspec +25 -0
  36. data/test/test_client.rb +424 -0
  37. data/test/test_model_country.rb +80 -0
  38. data/test/test_model_names.rb +47 -0
  39. data/test/test_reader.rb +459 -0
  40. metadata +116 -0
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'maxmind/geoip2'
4
+ require 'minitest/autorun'
5
+
6
+ class ModelNameTest < Minitest::Test
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,459 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ipaddr'
4
+ require 'maxmind/geoip2'
5
+ require 'minitest/autorun'
6
+
7
+ class ReaderTest < Minitest::Test
8
+ def test_anonymous_ip
9
+ reader = MaxMind::GeoIP2::Reader.new(
10
+ 'test/data/test-data/GeoIP2-Anonymous-IP-Test.mmdb',
11
+ )
12
+ ip = '1.2.0.1'
13
+ record = reader.anonymous_ip(ip)
14
+
15
+ assert_equal(true, record.anonymous?)
16
+ assert_equal(true, record.anonymous_vpn?)
17
+ assert_equal(false, record.hosting_provider?)
18
+ assert_equal(false, record.public_proxy?)
19
+ assert_equal(false, record.tor_exit_node?)
20
+ assert_equal(ip, record.ip_address)
21
+ assert_equal('1.2.0.0/16', record.network)
22
+
23
+ reader.close
24
+ end
25
+
26
+ def test_asn
27
+ reader = MaxMind::GeoIP2::Reader.new(
28
+ 'test/data/test-data/GeoLite2-ASN-Test.mmdb',
29
+ )
30
+ ip = '1.128.0.1'
31
+ record = reader.asn(ip)
32
+
33
+ assert_equal(1221, record.autonomous_system_number)
34
+ assert_equal('Telstra Pty Ltd', record.autonomous_system_organization)
35
+ assert_equal(ip, record.ip_address)
36
+ assert_equal('1.128.0.0/11', record.network)
37
+
38
+ reader.close
39
+ end
40
+
41
+ def test_city
42
+ reader = MaxMind::GeoIP2::Reader.new(
43
+ 'test/data/test-data/GeoIP2-City-Test.mmdb',
44
+ )
45
+ record = reader.city('2.125.160.216')
46
+
47
+ assert_equal('EU', record.continent.code)
48
+
49
+ assert_equal(2_655_045, record.city.geoname_id)
50
+ assert_equal({ 'en' => 'Boxford' }, record.city.names)
51
+ assert_equal('Boxford', record.city.name)
52
+ assert_nil(record.city.confidence)
53
+
54
+ assert_equal(100, record.location.accuracy_radius)
55
+ assert_equal(51.75, record.location.latitude)
56
+ assert_equal(-1.25, record.location.longitude)
57
+ assert_equal('Europe/London', record.location.time_zone)
58
+
59
+ assert_equal(2, record.subdivisions.size)
60
+ assert_equal('England', record.subdivisions[0].name)
61
+ assert_equal('West Berkshire', record.subdivisions[1].name)
62
+ assert_equal('West Berkshire', record.most_specific_subdivision.name)
63
+
64
+ record = reader.city('216.160.83.56')
65
+
66
+ assert_equal(819, record.location.metro_code)
67
+
68
+ assert_equal('98354', record.postal.code)
69
+
70
+ assert_equal(1, record.subdivisions.size)
71
+ assert_equal(5_815_135, record.subdivisions[0].geoname_id)
72
+ assert_equal('WA', record.subdivisions[0].iso_code)
73
+ assert_equal(
74
+ {
75
+ 'en' => 'Washington',
76
+ 'es' => 'Washington',
77
+ 'fr' => 'État de Washington',
78
+ 'ja' => 'ワシントン州',
79
+ 'ru' => 'Вашингтон',
80
+ 'zh-CN' => '华盛顿州',
81
+ },
82
+ record.subdivisions[0].names,
83
+ assert_equal('WA', record.most_specific_subdivision.iso_code)
84
+ )
85
+
86
+ reader.close
87
+ end
88
+
89
+ def test_city_no_subdivisions
90
+ reader = MaxMind::GeoIP2::Reader.new(
91
+ 'test/data/test-data/GeoIP2-City-Test.mmdb',
92
+ )
93
+ record = reader.city('2001:218::')
94
+
95
+ assert_equal([], record.subdivisions)
96
+ assert_nil(record.most_specific_subdivision)
97
+
98
+ reader.close
99
+ end
100
+
101
+ def test_connection_type
102
+ reader = MaxMind::GeoIP2::Reader.new(
103
+ 'test/data/test-data/GeoIP2-Connection-Type-Test.mmdb',
104
+ )
105
+ ip = '1.0.1.1'
106
+ record = reader.connection_type(ip)
107
+
108
+ assert_equal('Cable/DSL', record.connection_type)
109
+ assert_equal(ip, record.ip_address)
110
+ assert_equal('1.0.1.0/24', record.network)
111
+
112
+ reader.close
113
+ end
114
+
115
+ def test_country
116
+ reader = MaxMind::GeoIP2::Reader.new(
117
+ 'test/data/test-data/GeoIP2-Country-Test.mmdb',
118
+ )
119
+ record = reader.country('2.125.160.216')
120
+
121
+ assert_equal('EU', record.continent.code)
122
+ assert_equal(6_255_148, record.continent.geoname_id)
123
+ assert_equal(
124
+ {
125
+ 'de' => 'Europa',
126
+ 'en' => 'Europe',
127
+ 'es' => 'Europa',
128
+ 'fr' => 'Europe',
129
+ 'ja' => 'ヨーロッパ',
130
+ 'pt-BR' => 'Europa',
131
+ 'ru' => 'Европа',
132
+ 'zh-CN' => '欧洲',
133
+ },
134
+ record.continent.names,
135
+ )
136
+ assert_equal('Europe', record.continent.name)
137
+
138
+ assert_equal(2_635_167, record.country.geoname_id)
139
+ assert_equal(true, record.country.in_european_union?)
140
+ assert_equal('GB', record.country.iso_code)
141
+ assert_equal(
142
+ {
143
+ 'de' => 'Vereinigtes Königreich',
144
+ 'en' => 'United Kingdom',
145
+ 'es' => 'Reino Unido',
146
+ 'fr' => 'Royaume-Uni',
147
+ 'ja' => 'イギリス',
148
+ 'pt-BR' => 'Reino Unido',
149
+ 'ru' => 'Великобритания',
150
+ 'zh-CN' => '英国',
151
+ },
152
+ record.country.names,
153
+ )
154
+ assert_equal('United Kingdom', record.country.name)
155
+
156
+ assert_equal(3_017_382, record.registered_country.geoname_id)
157
+ assert_equal(true, record.registered_country.in_european_union?)
158
+ assert_equal('FR', record.registered_country.iso_code)
159
+ assert_equal(
160
+ {
161
+ 'de' => 'Frankreich',
162
+ 'en' => 'France',
163
+ 'es' => 'Francia',
164
+ 'fr' => 'France',
165
+ 'ja' => 'フランス共和国',
166
+ 'pt-BR' => 'França',
167
+ 'ru' => 'Франция',
168
+ 'zh-CN' => '法国',
169
+ },
170
+ record.registered_country.names,
171
+ )
172
+ assert_equal('France', record.registered_country.name)
173
+
174
+ record = reader.country('202.196.224.0')
175
+
176
+ assert_equal(6_252_001, record.represented_country.geoname_id)
177
+ assert_equal('US', record.represented_country.iso_code)
178
+ assert_equal(
179
+ {
180
+ 'de' => 'USA',
181
+ 'en' => 'United States',
182
+ 'es' => 'Estados Unidos',
183
+ 'fr' => 'États-Unis',
184
+ 'ja' => 'アメリカ合衆国',
185
+ 'pt-BR' => 'Estados Unidos',
186
+ 'ru' => 'США',
187
+ 'zh-CN' => '美国',
188
+ },
189
+ record.represented_country.names,
190
+ )
191
+ assert_equal('United States', record.represented_country.name)
192
+ assert_equal('military', record.represented_country.type)
193
+
194
+ record = reader.country('81.2.69.163')
195
+ assert_equal('81.2.69.163', record.traits.ip_address)
196
+ assert_equal('81.2.69.160/27', record.traits.network)
197
+
198
+ assert_raises(NoMethodError) { record.foo }
199
+ reader.close
200
+ end
201
+
202
+ def test_is_method_returns_false
203
+ reader = MaxMind::GeoIP2::Reader.new(
204
+ 'test/data/test-data/GeoIP2-Country-Test.mmdb',
205
+ )
206
+ record = reader.country('74.209.24.0')
207
+ assert_equal(false, record.country.in_european_union?)
208
+
209
+ reader.close
210
+ end
211
+
212
+ def test_domain
213
+ reader = MaxMind::GeoIP2::Reader.new(
214
+ 'test/data/test-data/GeoIP2-Domain-Test.mmdb',
215
+ )
216
+ ip = '1.2.0.1'
217
+ record = reader.domain(ip)
218
+
219
+ assert_equal('maxmind.com', record.domain)
220
+ assert_equal(ip, record.ip_address)
221
+ assert_equal('1.2.0.0/16', record.network)
222
+
223
+ reader.close
224
+ end
225
+
226
+ def test_enterprise
227
+ reader = MaxMind::GeoIP2::Reader.new(
228
+ 'test/data/test-data/GeoIP2-Enterprise-Test.mmdb',
229
+ )
230
+ record = reader.enterprise('2.125.160.216')
231
+
232
+ assert_equal(50, record.city.confidence)
233
+ assert_equal(2_655_045, record.city.geoname_id)
234
+ assert_equal({ 'en' => 'Boxford' }, record.city.names)
235
+ assert_equal('Boxford', record.city.name)
236
+
237
+ ip_address = '74.209.24.0'
238
+ record = reader.enterprise(ip_address)
239
+
240
+ assert_equal(11, record.city.confidence)
241
+ assert_equal(99, record.country.confidence)
242
+ assert_equal(6_252_001, record.country.geoname_id)
243
+ assert_equal(false, record.country.in_european_union?)
244
+
245
+ assert_equal(27, record.location.accuracy_radius)
246
+
247
+ assert_equal(false, record.registered_country.in_european_union?)
248
+
249
+ assert_equal('Cable/DSL', record.traits.connection_type)
250
+ assert_equal(true, record.traits.legitimate_proxy?)
251
+
252
+ assert_equal(ip_address, record.traits.ip_address)
253
+ assert_equal('74.209.16.0/20', record.traits.network)
254
+
255
+ reader.close
256
+ end
257
+
258
+ def test_isp
259
+ reader = MaxMind::GeoIP2::Reader.new(
260
+ 'test/data/test-data/GeoIP2-ISP-Test.mmdb',
261
+ )
262
+ ip = '1.128.1.1'
263
+ record = reader.isp(ip)
264
+
265
+ assert_equal(1221, record.autonomous_system_number)
266
+ assert_equal('Telstra Pty Ltd', record.autonomous_system_organization)
267
+ assert_equal('Telstra Internet', record.isp)
268
+ assert_equal('Telstra Internet', record.organization)
269
+ assert_equal(ip, record.ip_address)
270
+ assert_equal('1.128.0.0/11', record.network)
271
+
272
+ reader.close
273
+ end
274
+
275
+ def test_no_traits
276
+ reader = MaxMind::GeoIP2::Reader.new(
277
+ 'test/data/test-data/GeoIP2-Enterprise-Test.mmdb',
278
+ )
279
+ record = reader.enterprise('2.125.160.216')
280
+
281
+ assert_equal('2.125.160.216', record.traits.ip_address)
282
+ assert_equal('2.125.160.216/29', record.traits.network)
283
+ assert_nil(record.traits.autonomous_system_number)
284
+ assert_equal(false, record.traits.anonymous?)
285
+
286
+ reader.close
287
+ end
288
+
289
+ def test_no_location
290
+ reader = MaxMind::GeoIP2::Reader.new(
291
+ 'test/data/test-data/GeoIP2-Enterprise-Test.mmdb',
292
+ )
293
+ record = reader.enterprise('212.47.235.81')
294
+
295
+ assert_nil(record.location.accuracy_radius)
296
+
297
+ reader.close
298
+ end
299
+
300
+ def test_no_postal
301
+ reader = MaxMind::GeoIP2::Reader.new(
302
+ 'test/data/test-data/GeoIP2-Enterprise-Test.mmdb',
303
+ )
304
+ record = reader.enterprise('212.47.235.81')
305
+
306
+ assert_nil(record.postal.code)
307
+
308
+ reader.close
309
+ end
310
+
311
+ def test_no_city
312
+ reader = MaxMind::GeoIP2::Reader.new(
313
+ 'test/data/test-data/GeoIP2-Enterprise-Test.mmdb',
314
+ )
315
+ record = reader.enterprise('212.47.235.81')
316
+
317
+ assert_nil(record.city.confidence)
318
+ assert_nil(record.city.name)
319
+ assert_nil(record.city.names)
320
+
321
+ reader.close
322
+ end
323
+
324
+ def test_no_continent
325
+ reader = MaxMind::GeoIP2::Reader.new(
326
+ 'test/data/test-data/GeoIP2-Enterprise-Test.mmdb',
327
+ )
328
+ record = reader.enterprise('212.47.235.81')
329
+
330
+ assert_nil(record.continent.code)
331
+
332
+ reader.close
333
+ end
334
+
335
+ def test_no_country
336
+ reader = MaxMind::GeoIP2::Reader.new(
337
+ 'test/data/test-data/GeoIP2-Enterprise-Test.mmdb',
338
+ )
339
+ record = reader.enterprise('212.47.235.81')
340
+
341
+ assert_nil(record.country.confidence)
342
+
343
+ reader.close
344
+ end
345
+
346
+ def test_no_represented_country
347
+ reader = MaxMind::GeoIP2::Reader.new(
348
+ 'test/data/test-data/GeoIP2-Enterprise-Test.mmdb',
349
+ )
350
+ record = reader.enterprise('212.47.235.81')
351
+
352
+ assert_nil(record.represented_country.type)
353
+
354
+ reader.close
355
+ end
356
+
357
+ def database_types
358
+ [
359
+ { 'file' => 'City', 'method' => 'city' },
360
+ { 'file' => 'Country', 'method' => 'country' },
361
+ ]
362
+ end
363
+
364
+ def test_default_locale
365
+ database_types.each do |t|
366
+ reader = MaxMind::GeoIP2::Reader.new(
367
+ "test/data/test-data/GeoIP2-#{t['file']}-Test.mmdb",
368
+ )
369
+ record = reader.send(t['method'], '81.2.69.160')
370
+ assert_equal('United Kingdom', record.country.name)
371
+ reader.close
372
+ end
373
+ end
374
+
375
+ def test_locale_list
376
+ database_types.each do |t|
377
+ reader = MaxMind::GeoIP2::Reader.new(
378
+ "test/data/test-data/GeoIP2-#{t['file']}-Test.mmdb",
379
+ %w[xx ru pt-BR es en],
380
+ )
381
+ record = reader.send(t['method'], '81.2.69.160')
382
+ assert_equal('Великобритания', record.country.name)
383
+ reader.close
384
+ end
385
+ end
386
+
387
+ def test_has_ip_address_and_network
388
+ database_types.each do |t|
389
+ reader = MaxMind::GeoIP2::Reader.new(
390
+ "test/data/test-data/GeoIP2-#{t['file']}-Test.mmdb",
391
+ )
392
+ record = reader.send(t['method'], '81.2.69.163')
393
+ assert_equal('81.2.69.163', record.traits.ip_address)
394
+ assert_equal('81.2.69.160/27', record.traits.network)
395
+ reader.close
396
+ end
397
+ end
398
+
399
+ def test_is_in_european_union
400
+ database_types.each do |t|
401
+ reader = MaxMind::GeoIP2::Reader.new(
402
+ "test/data/test-data/GeoIP2-#{t['file']}-Test.mmdb",
403
+ )
404
+ record = reader.send(t['method'], '81.2.69.160')
405
+ assert_equal(true, record.country.in_european_union?)
406
+ assert_equal(false, record.registered_country.in_european_union?)
407
+ reader.close
408
+ end
409
+ end
410
+
411
+ def test_unknown_address
412
+ database_types.each do |t|
413
+ reader = MaxMind::GeoIP2::Reader.new(
414
+ "test/data/test-data/GeoIP2-#{t['file']}-Test.mmdb",
415
+ )
416
+ error = assert_raises(
417
+ MaxMind::GeoIP2::AddressNotFoundError,
418
+ ) { reader.send(t['method'], '10.10.10.0') }
419
+ assert_equal(
420
+ 'The address 10.10.10.0 is not in the database.',
421
+ error.message,
422
+ )
423
+ reader.close
424
+ end
425
+ end
426
+
427
+ def test_incorrect_database
428
+ reader = MaxMind::GeoIP2::Reader.new(
429
+ 'test/data/test-data/GeoIP2-City-Test.mmdb',
430
+ )
431
+ error = assert_raises(ArgumentError) { reader.country('10.10.10.10') }
432
+ assert_equal(
433
+ 'The country method cannot be used with the GeoIP2-City database.',
434
+ error.message,
435
+ )
436
+ reader.close
437
+ end
438
+
439
+ def test_invalid_address
440
+ reader = MaxMind::GeoIP2::Reader.new(
441
+ 'test/data/test-data/GeoIP2-City-Test.mmdb',
442
+ )
443
+ error = assert_raises(
444
+ IPAddr::InvalidAddressError,
445
+ ) { reader.city('invalid') }
446
+ # Ruby 2.5 says just 'invalid address'. Ruby 2.6+ says 'invalid address:
447
+ # invalid'.
448
+ assert_match('invalid address', error.message)
449
+ reader.close
450
+ end
451
+
452
+ def test_metadata
453
+ reader = MaxMind::GeoIP2::Reader.new(
454
+ 'test/data/test-data/GeoIP2-City-Test.mmdb',
455
+ )
456
+ assert_equal('GeoIP2-City', reader.metadata.database_type)
457
+ reader.close
458
+ end
459
+ end