maxmind-geoip2 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +15 -0
  3. data/README.md +4 -6
  4. data/lib/maxmind/geoip2/client.rb +1 -1
  5. data/lib/maxmind/geoip2/errors.rb +17 -8
  6. data/lib/maxmind/geoip2/model/city.rb +1 -1
  7. data/lib/maxmind/geoip2/model/enterprise.rb +1 -1
  8. data/lib/maxmind/geoip2/model/insights.rb +1 -1
  9. data/lib/maxmind/geoip2/model/isp.rb +16 -0
  10. data/lib/maxmind/geoip2/reader.rb +1 -2
  11. data/lib/maxmind/geoip2/record/traits.rb +23 -3
  12. data/maxmind-geoip2.gemspec +2 -1
  13. data/test/data/MaxMind-DB-spec.md +15 -11
  14. data/test/data/source-data/GeoIP2-City-Test.json +81 -48
  15. data/test/data/source-data/GeoIP2-Connection-Type-Test.json +20 -0
  16. data/test/data/source-data/GeoIP2-Country-Test.json +46 -58
  17. data/test/data/source-data/GeoIP2-Enterprise-Test.json +61 -3
  18. data/test/data/source-data/GeoIP2-ISP-Test.json +10 -0
  19. data/test/data/source-data/GeoIP2-Precision-Enterprise-Test.json +61 -4
  20. data/test/data/source-data/GeoLite2-ASN-Test.json +4091 -8
  21. data/test/data/source-data/GeoLite2-City-Test.json +12804 -0
  22. data/test/data/source-data/GeoLite2-Country-Test.json +11280 -0
  23. data/test/data/test-data/GeoIP2-Anonymous-IP-Test.mmdb +0 -0
  24. data/test/data/test-data/GeoIP2-City-Test-Broken-Double-Format.mmdb +0 -0
  25. data/test/data/test-data/GeoIP2-City-Test-Invalid-Node-Count.mmdb +0 -0
  26. data/test/data/test-data/GeoIP2-City-Test.mmdb +0 -0
  27. data/test/data/test-data/GeoIP2-Connection-Type-Test.mmdb +0 -0
  28. data/test/data/test-data/GeoIP2-Country-Test.mmdb +0 -0
  29. data/test/data/test-data/GeoIP2-DensityIncome-Test.mmdb +0 -0
  30. data/test/data/test-data/GeoIP2-Domain-Test.mmdb +0 -0
  31. data/test/data/test-data/GeoIP2-Enterprise-Test.mmdb +0 -0
  32. data/test/data/test-data/GeoIP2-ISP-Test.mmdb +0 -0
  33. data/test/data/test-data/GeoIP2-Precision-Enterprise-Test.mmdb +0 -0
  34. data/test/data/test-data/GeoIP2-Static-IP-Score-Test.mmdb +0 -0
  35. data/test/data/test-data/GeoIP2-User-Count-Test.mmdb +0 -0
  36. data/test/data/test-data/GeoLite2-ASN-Test.mmdb +0 -0
  37. data/test/data/test-data/GeoLite2-City-Test.mmdb +0 -0
  38. data/test/data/test-data/GeoLite2-Country-Test.mmdb +0 -0
  39. data/test/data/test-data/MaxMind-DB-no-ipv4-search-tree.mmdb +0 -0
  40. data/test/data/test-data/MaxMind-DB-string-value-entries.mmdb +0 -0
  41. data/test/data/test-data/MaxMind-DB-test-broken-pointers-24.mmdb +0 -0
  42. data/test/data/test-data/MaxMind-DB-test-broken-search-tree-24.mmdb +0 -0
  43. data/test/data/test-data/MaxMind-DB-test-decoder.mmdb +0 -0
  44. data/test/data/test-data/MaxMind-DB-test-ipv4-24.mmdb +0 -0
  45. data/test/data/test-data/MaxMind-DB-test-ipv4-28.mmdb +0 -0
  46. data/test/data/test-data/MaxMind-DB-test-ipv4-32.mmdb +0 -0
  47. data/test/data/test-data/MaxMind-DB-test-ipv6-24.mmdb +0 -0
  48. data/test/data/test-data/MaxMind-DB-test-ipv6-28.mmdb +0 -0
  49. data/test/data/test-data/MaxMind-DB-test-ipv6-32.mmdb +0 -0
  50. data/test/data/test-data/MaxMind-DB-test-metadata-pointers.mmdb +0 -0
  51. data/test/data/test-data/MaxMind-DB-test-mixed-24.mmdb +0 -0
  52. data/test/data/test-data/MaxMind-DB-test-mixed-28.mmdb +0 -0
  53. data/test/data/test-data/MaxMind-DB-test-mixed-32.mmdb +0 -0
  54. data/test/data/test-data/MaxMind-DB-test-nested.mmdb +0 -0
  55. data/test/data/test-data/MaxMind-DB-test-pointer-decoder.mmdb +0 -0
  56. data/test/data/test-data/README.md +3 -3
  57. data/test/data/test-data/write-test-data.pl +9 -5
  58. data/test/test_reader.rb +18 -2
  59. metadata +8 -4
  60. data/test/data/MaxMind-DB-test-metadata-pointers.mmdb +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 874135d664c0af2e82c184ce30fffbdfe72bb6badc1aa53ee0ec5eba626e5a1d
4
- data.tar.gz: 7aec662f3893322c983f647545f4a155b27bda20b7fd6349201fc386264bdc88
3
+ metadata.gz: 2bdd199c61f9c7116b023d420f1fb0763d05abd7711dd7be0f177113028140ff
4
+ data.tar.gz: 00a081df1adc640dff12ef5fb8670ac9714b9edd1ef3fa65049033a1fa9d385b
5
5
  SHA512:
6
- metadata.gz: 3b5d57920b467ccae6144b6f4aca8266721124481917014025390f02f6728138b704513137596593226506f82d554cf77afb215921a13fa8622e1b4e3a4f8b43
7
- data.tar.gz: 28be946c7ea163e61b641029f3094de7b47ac2ce8bec6d1c68f9ddd5d084f55419403daeb30762fce3d0346ea0fb083de0d946621f6d659eabe03cafe3c3d0dd
6
+ metadata.gz: 85a980183be16fa99909d42db32d295ff68a8c718cb31384aef78db8fb287a970d148d12c112fbb9c3cde0a84d396a438a9faf7c196b3e2bfadce93695ebf8e4
7
+ data.tar.gz: 3ef1e40dd82bbf1acf3ddb14b1d774c246c78219463a00b87bfa46c14af7b623ccdfb9af5046c3a1d0b0b8b18fbe4f575d88d28d9930f3b10913c0035326a611
data/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.1.0 (2021-11-18)
4
+
5
+ * Exceptions from this gem now inherit from `MaxMind::GeoIP2::Error`. IP
6
+ address related exceptions now inherit from
7
+ `MaxMind::GeoIP2::AddressError`, which itself inherits from
8
+ `MaxMind::GeoIP2::Error`. Pull Request by gr8bit. GitHub #35.
9
+ * Support for mobile country code (MCC) and mobile network codes (MNC) was
10
+ added for the GeoIP2 ISP and Enterprise databases as well as the GeoIP2
11
+ City and Insights web services. `mobile_country_code` and
12
+ `mobile_network_code` attributes were added to
13
+ `MaxMind::GeoIP2::Model::ISP` for the GeoIP2 ISP database and
14
+ `MaxMind::GeoIP2::Record::Traits` for the Enterprise database and the
15
+ GeoIP2 City and Insights web services. We expect this data to be
16
+ available by late January, 2022.
17
+
3
18
  ## 1.0.0 (2021-05-14)
4
19
 
5
20
  * Ruby 2.4 is no longer supported. If you're using Ruby 2.4, please use
data/README.md CHANGED
@@ -2,11 +2,9 @@
2
2
 
3
3
  ## Description
4
4
 
5
- This is the Ruby API for the GeoIP2
6
- [webservices](https://dev.maxmind.com/geoip/geoip2/web-services) and
7
- [databases](https://dev.maxmind.com/geoip/geoip2/geolite2/). This API also
8
- works with the free [GeoLite2
9
- databases](https://dev.maxmind.com/geoip/geoip2/geolite2/).
5
+ This is the Ruby API for the GeoIP2 and GeoLite2
6
+ [webservices](https://dev.maxmind.com/geoip/docs/web-services?lang=en)
7
+ and [databases](https://dev.maxmind.com/geoip/docs/databases?lang=en).
10
8
 
11
9
  ## Installation
12
10
 
@@ -282,7 +280,7 @@ point may offer a particular piece of data, MaxMind does not always have
282
280
  every piece of data for any given IP address.
283
281
 
284
282
  See the [GeoIP2 Precision web service
285
- docs](https://dev.maxmind.com/geoip/geoip2/web-services) for details on
283
+ docs](https://dev.maxmind.com/geoip/docs/web-services?lang=en) for details on
286
284
  what data each end point may return.
287
285
 
288
286
  The only piece of data which is always returned is the `ip_address`
@@ -11,7 +11,7 @@ require 'maxmind/geoip2/model/insights'
11
11
  module MaxMind
12
12
  module GeoIP2
13
13
  # This class provides a client API for all the
14
- # {https://dev.maxmind.com/geoip/geoip2/web-services/ GeoIP2 Precision web
14
+ # {https://dev.maxmind.com/geoip/docs/web-services?lang=en GeoIP2 Precision web
15
15
  # services}. The services are Country, City, and Insights. Each service
16
16
  # returns a different set of data about an IP address, with Country returning
17
17
  # the least data and Insights the most.
@@ -2,40 +2,49 @@
2
2
 
3
3
  module MaxMind
4
4
  module GeoIP2
5
+ # Module's base error class
6
+ class Error < StandardError
7
+ end
8
+
9
+ # Base error class for all errors that originate from the IP address
10
+ # itself and will not change when retried.
11
+ class AddressError < Error
12
+ end
13
+
5
14
  # An AddressNotFoundError means the IP address was not found in the
6
15
  # database or the web service said the IP address was not found.
7
- class AddressNotFoundError < RuntimeError
16
+ class AddressNotFoundError < AddressError
8
17
  end
9
18
 
10
19
  # An HTTPError means there was an unexpected HTTP status or response.
11
- class HTTPError < RuntimeError
20
+ class HTTPError < Error
12
21
  end
13
22
 
14
23
  # An AddressInvalidError means the IP address was invalid.
15
- class AddressInvalidError < RuntimeError
24
+ class AddressInvalidError < AddressError
16
25
  end
17
26
 
18
27
  # An AddressReservedError means the IP address is reserved.
19
- class AddressReservedError < RuntimeError
28
+ class AddressReservedError < AddressError
20
29
  end
21
30
 
22
31
  # An AuthenticationError means there was a problem authenticating to the
23
32
  # web service.
24
- class AuthenticationError < RuntimeError
33
+ class AuthenticationError < Error
25
34
  end
26
35
 
27
36
  # An InsufficientFundsError means the account is out of credits.
28
- class InsufficientFundsError < RuntimeError
37
+ class InsufficientFundsError < Error
29
38
  end
30
39
 
31
40
  # A PermissionRequiredError means the account does not have permission to
32
41
  # use the requested service.
33
- class PermissionRequiredError < RuntimeError
42
+ class PermissionRequiredError < Error
34
43
  end
35
44
 
36
45
  # An InvalidRequestError means the web service returned an error and there
37
46
  # is no more specific error class.
38
- class InvalidRequestError < RuntimeError
47
+ class InvalidRequestError < Error
39
48
  end
40
49
  end
41
50
  end
@@ -14,7 +14,7 @@ module MaxMind
14
14
  #
15
15
  # The only difference between the City and Insights model classes is which
16
16
  # fields in each record may be populated. See
17
- # https://dev.maxmind.com/geoip/geoip2/web-services for more details.
17
+ # https://dev.maxmind.com/geoip/docs/web-services?lang=en for more details.
18
18
  #
19
19
  # See {MaxMind::GeoIP2::Model::Country} for inherited methods.
20
20
  class City < Country
@@ -9,7 +9,7 @@ module MaxMind
9
9
  #
10
10
  # The only difference between the City and Insights model classes is which
11
11
  # fields in each record may be populated. See
12
- # https://dev.maxmind.com/geoip/geoip2/web-services for more details.
12
+ # https://dev.maxmind.com/geoip/docs/web-services?lang=en for more details.
13
13
  #
14
14
  # See {MaxMind::GeoIP2::Model::City} for inherited methods.
15
15
  class Enterprise < City
@@ -10,7 +10,7 @@ module MaxMind
10
10
  #
11
11
  # The only difference between the City and Insights model classes is which
12
12
  # fields in each record may be populated. See
13
- # https://dev.maxmind.com/geoip/geoip2/web-services for more details.
13
+ # https://dev.maxmind.com/geoip/docs/web-services?lang=en for more details.
14
14
  class Insights < City
15
15
  end
16
16
  end
@@ -36,6 +36,22 @@ module MaxMind
36
36
  get('isp')
37
37
  end
38
38
 
39
+ # The {https://en.wikipedia.org/wiki/Mobile_country_code mobile country
40
+ # code (MCC)} associated with the IP address and ISP.
41
+ #
42
+ # @return [String, nil]
43
+ def mobile_country_code
44
+ get('mobile_country_code')
45
+ end
46
+
47
+ # The {https://en.wikipedia.org/wiki/Mobile_country_code mobile network
48
+ # code (MNC)} associated with the IP address and ISP.
49
+ #
50
+ # @return [String, nil]
51
+ def mobile_network_code
52
+ get('mobile_network_code')
53
+ end
54
+
39
55
  # The network in CIDR notation associated with the record. In particular,
40
56
  # this is the largest network where all of the fields besides ip_address
41
57
  # have the same value.
@@ -37,8 +37,7 @@ module MaxMind
37
37
  # Reader and reuse it.
38
38
  #
39
39
  # Once created, the Reader is safe to use for lookups from multiple
40
- # threads. It is safe to use after forking only if you use
41
- # MaxMind::DB::MODE_MEMORY or if your version of Ruby supports IO#pread.
40
+ # threads. It is safe to use after forking.
42
41
  #
43
42
  # @overload initialize(database:, locales: ['en'], mode: MaxMind::DB::MODE_AUTO)
44
43
  # @param database [String] a path to a GeoIP2/GeoLite2 database file.
@@ -16,9 +16,7 @@ module MaxMind
16
16
  if record && !record.key?('network') && record.key?('ip_address') &&
17
17
  record.key?('prefix_length')
18
18
  ip = IPAddr.new(record['ip_address']).mask(record['prefix_length'])
19
- # We could use ip.prefix instead of record['prefix_length'], but that
20
- # method only becomes available in Ruby 2.5+.
21
- record['network'] = format('%s/%d', ip.to_s, record['prefix_length'])
19
+ record['network'] = format('%s/%d', ip.to_s, ip.prefix)
22
20
  end
23
21
  end
24
22
 
@@ -111,6 +109,28 @@ module MaxMind
111
109
  get('is_legitimate_proxy')
112
110
  end
113
111
 
112
+ # The {https://en.wikipedia.org/wiki/Mobile_country_code mobile country
113
+ # code (MCC)} associated with the IP address and ISP.
114
+ #
115
+ # This attribute is only available from the City and Insights web service
116
+ # and the GeoIP2 Enterprise database.
117
+ #
118
+ # @return [String, nil]
119
+ def mobile_country_code
120
+ get('mobile_country_code')
121
+ end
122
+
123
+ # The {https://en.wikipedia.org/wiki/Mobile_country_code mobile network
124
+ # code (MNC)} associated with the IP address and ISP.
125
+ #
126
+ # This attribute is only available from the City and Insights web service
127
+ # and the GeoIP2 Enterprise database.
128
+ #
129
+ # @return [String, nil]
130
+ def mobile_network_code
131
+ get('mobile_network_code')
132
+ end
133
+
114
134
  # This is true if the IP address belongs to a public proxy. This property
115
135
  # is only available from GeoIP2 Precision Insights.
116
136
  #
@@ -5,7 +5,7 @@ Gem::Specification.new do |s|
5
5
  s.files = Dir['**/*']
6
6
  s.name = 'maxmind-geoip2'
7
7
  s.summary = 'A gem for interacting with the GeoIP2 webservices and databases.'
8
- s.version = '1.0.0'
8
+ s.version = '1.1.0'
9
9
 
10
10
  s.description = 'A gem for interacting with the GeoIP2 webservices and databases. MaxMind provides geolocation data as downloadable databases as well as through a webservice.'
11
11
  s.email = 'support@maxmind.com'
@@ -16,6 +16,7 @@ Gem::Specification.new do |s|
16
16
  'changelog_uri' => 'https://github.com/maxmind/GeoIP2-ruby/blob/main/CHANGELOG.md',
17
17
  'documentation_uri' => 'https://www.rubydoc.info/gems/maxmind-geoip2',
18
18
  'homepage_uri' => 'https://github.com/maxmind/GeoIP2-ruby',
19
+ 'rubygems_mfa_required' => 'true',
19
20
  'source_code_uri' => 'https://github.com/maxmind/GeoIP2-ruby',
20
21
  }
21
22
  s.required_ruby_version = '>= 2.5.0'
@@ -26,9 +26,10 @@ not be broken by minor version changes to the format.
26
26
  The binary database is split into three parts:
27
27
 
28
28
  1. The binary search tree. Each level of the tree corresponds to a single bit
29
- in the 128 bit representation of an IPv6 address.
30
- 2. The data section. These are the values returned to the client for a
31
- specific IP address, e.g. "US", "New York", or a more complex map type made up
29
+ in the prefix of the network the IP address belongs to.
30
+ 2. The data section with the values for the networks in the binary search
31
+ tree. These values may be comprised of a single data type, e.g., the string
32
+ "US" or "New York", or they may be a more complex map or array type made up
32
33
  of multiple fields.
33
34
  3. Database metadata. Information about the database itself.
34
35
 
@@ -48,15 +49,15 @@ of this sequence.
48
49
  The maximum allowable size for the metadata section, including the marker that
49
50
  starts the metadata, is 128KiB.
50
51
 
51
- The metadata is stored as a map data structure. This structure is described
52
- later in the spec. Changing a key's data type or removing a key would
53
- constitute a major version change for this spec.
52
+ The metadata is stored as a separate data section comprised of a map data
53
+ structure starting at the beginning of that section. This structure is
54
+ described later in the spec.
54
55
 
55
56
  Except where otherwise specified, each key listed is required for the database
56
57
  to be considered valid.
57
58
 
58
- Adding a key constitutes a minor version change. Removing a key or changing
59
- its type constitutes a major version change.
59
+ Changing a key's data type or removing a key would constitute a major version
60
+ change for this spec. Adding a key constitutes a minor version change.
60
61
 
61
62
  The list of known keys for the current version of the format is as follows:
62
63
 
@@ -304,7 +305,8 @@ will point to the beginning of a field. It is illegal for a pointer to point
304
305
  to another pointer.
305
306
 
306
307
  Pointer values start from the beginning of the data section, *not* the
307
- beginning of the file.
308
+ beginning of the file. Pointers in the metadata start from the beginning of
309
+ the metadata section.
308
310
 
309
311
  ### UTF-8 string - 2
310
312
 
@@ -337,8 +339,10 @@ by the length specifier in the control byte. See below for details.
337
339
 
338
340
  A length of zero always indicates the number 0.
339
341
 
340
- When storing a signed integer, the left-most bit is the sign. A 1 is negative
341
- and a 0 is positive.
342
+ When storing a signed integer, fields shorter than the maximum byte length
343
+ are always positive. When the field is the maximum length, e.g., 4 bytes for
344
+ 32-bit integers, the left-most bit is the sign. A 1 is negative and a 0 is
345
+ positive.
342
346
 
343
347
  The type numbers for our integer types are:
344
348