maxmind-geoip2 0.0.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +30 -0
  3. data/Gemfile +1 -5
  4. data/README.md +121 -0
  5. data/lib/maxmind/geoip2.rb +1 -13
  6. data/lib/maxmind/geoip2/client.rb +328 -0
  7. data/lib/maxmind/geoip2/errors.rb +37 -3
  8. data/lib/maxmind/geoip2/model/abstract.rb +31 -0
  9. data/lib/maxmind/geoip2/model/anonymous_ip.rb +75 -0
  10. data/lib/maxmind/geoip2/model/asn.rb +43 -0
  11. data/lib/maxmind/geoip2/model/city.rb +63 -44
  12. data/lib/maxmind/geoip2/model/connection_type.rb +36 -0
  13. data/lib/maxmind/geoip2/model/country.rb +64 -45
  14. data/lib/maxmind/geoip2/model/domain.rb +36 -0
  15. data/lib/maxmind/geoip2/model/enterprise.rb +13 -9
  16. data/lib/maxmind/geoip2/model/insights.rb +18 -0
  17. data/lib/maxmind/geoip2/model/isp.rb +57 -0
  18. data/lib/maxmind/geoip2/reader.rb +250 -131
  19. data/lib/maxmind/geoip2/record/abstract.rb +17 -12
  20. data/lib/maxmind/geoip2/record/city.rb +33 -24
  21. data/lib/maxmind/geoip2/record/continent.rb +32 -23
  22. data/lib/maxmind/geoip2/record/country.rb +47 -34
  23. data/lib/maxmind/geoip2/record/location.rb +64 -50
  24. data/lib/maxmind/geoip2/record/maxmind.rb +21 -0
  25. data/lib/maxmind/geoip2/record/place.rb +22 -16
  26. data/lib/maxmind/geoip2/record/postal.rb +26 -18
  27. data/lib/maxmind/geoip2/record/represented_country.rb +20 -14
  28. data/lib/maxmind/geoip2/record/subdivision.rb +42 -34
  29. data/lib/maxmind/geoip2/record/traits.rb +204 -160
  30. data/maxmind-geoip2.gemspec +12 -2
  31. data/test/data/LICENSE +4 -0
  32. data/test/data/MaxMind-DB-spec.md +570 -0
  33. data/test/data/MaxMind-DB-test-metadata-pointers.mmdb +0 -0
  34. data/test/data/README.md +4 -0
  35. data/test/data/bad-data/README.md +7 -0
  36. data/test/data/bad-data/libmaxminddb/libmaxminddb-offset-integer-overflow.mmdb +0 -0
  37. data/test/data/bad-data/maxminddb-golang/cyclic-data-structure.mmdb +0 -0
  38. data/test/data/bad-data/maxminddb-golang/invalid-bytes-length.mmdb +1 -0
  39. data/test/data/bad-data/maxminddb-golang/invalid-data-record-offset.mmdb +0 -0
  40. data/test/data/bad-data/maxminddb-golang/invalid-map-key-length.mmdb +0 -0
  41. data/test/data/bad-data/maxminddb-golang/invalid-string-length.mmdb +1 -0
  42. data/test/data/bad-data/maxminddb-golang/metadata-is-an-uint128.mmdb +1 -0
  43. data/test/data/bad-data/maxminddb-golang/unexpected-bytes.mmdb +0 -0
  44. data/test/data/bad-data/maxminddb-python/bad-unicode-in-map-key.mmdb +0 -0
  45. data/test/data/perltidyrc +12 -0
  46. data/test/data/source-data/GeoIP2-Anonymous-IP-Test.json +49 -0
  47. data/test/data/source-data/GeoIP2-City-Test.json +12852 -0
  48. data/test/data/source-data/GeoIP2-Connection-Type-Test.json +102 -0
  49. data/test/data/source-data/GeoIP2-Country-Test.json +15916 -0
  50. data/test/data/source-data/GeoIP2-DensityIncome-Test.json +14 -0
  51. data/test/data/source-data/GeoIP2-Domain-Test.json +452 -0
  52. data/test/data/source-data/GeoIP2-Enterprise-Test.json +687 -0
  53. data/test/data/source-data/GeoIP2-ISP-Test.json +12595 -0
  54. data/test/data/source-data/GeoIP2-Precision-Enterprise-Test.json +2148 -0
  55. data/test/data/source-data/GeoIP2-Static-IP-Score-Test.json +2132 -0
  56. data/test/data/source-data/GeoIP2-User-Count-Test.json +2837 -0
  57. data/test/data/source-data/GeoLite2-ASN-Test.json +37 -0
  58. data/test/data/source-data/README +15 -0
  59. data/test/data/test-data/GeoIP2-Anonymous-IP-Test.mmdb +0 -0
  60. data/test/data/test-data/GeoIP2-City-Test-Broken-Double-Format.mmdb +0 -0
  61. data/test/data/test-data/GeoIP2-City-Test-Invalid-Node-Count.mmdb +0 -0
  62. data/test/data/test-data/GeoIP2-City-Test.mmdb +0 -0
  63. data/test/data/test-data/GeoIP2-Connection-Type-Test.mmdb +0 -0
  64. data/test/data/test-data/GeoIP2-Country-Test.mmdb +0 -0
  65. data/test/data/test-data/GeoIP2-DensityIncome-Test.mmdb +0 -0
  66. data/test/data/test-data/GeoIP2-Domain-Test.mmdb +0 -0
  67. data/test/data/test-data/GeoIP2-Enterprise-Test.mmdb +0 -0
  68. data/test/data/test-data/GeoIP2-ISP-Test.mmdb +0 -0
  69. data/test/data/test-data/GeoIP2-Precision-Enterprise-Test.mmdb +0 -0
  70. data/test/data/test-data/GeoIP2-Static-IP-Score-Test.mmdb +0 -0
  71. data/test/data/test-data/GeoIP2-User-Count-Test.mmdb +0 -0
  72. data/test/data/test-data/GeoLite2-ASN-Test.mmdb +0 -0
  73. data/test/data/test-data/MaxMind-DB-no-ipv4-search-tree.mmdb +0 -0
  74. data/test/data/test-data/MaxMind-DB-string-value-entries.mmdb +0 -0
  75. data/test/data/test-data/MaxMind-DB-test-broken-pointers-24.mmdb +0 -0
  76. data/test/data/test-data/MaxMind-DB-test-broken-search-tree-24.mmdb +0 -0
  77. data/test/data/test-data/MaxMind-DB-test-decoder.mmdb +0 -0
  78. data/test/data/test-data/MaxMind-DB-test-ipv4-24.mmdb +0 -0
  79. data/test/data/test-data/MaxMind-DB-test-ipv4-28.mmdb +0 -0
  80. data/test/data/test-data/MaxMind-DB-test-ipv4-32.mmdb +0 -0
  81. data/test/data/test-data/MaxMind-DB-test-ipv6-24.mmdb +0 -0
  82. data/test/data/test-data/MaxMind-DB-test-ipv6-28.mmdb +0 -0
  83. data/test/data/test-data/MaxMind-DB-test-ipv6-32.mmdb +0 -0
  84. data/test/data/test-data/MaxMind-DB-test-metadata-pointers.mmdb +0 -0
  85. data/test/data/test-data/MaxMind-DB-test-mixed-24.mmdb +0 -0
  86. data/test/data/test-data/MaxMind-DB-test-mixed-28.mmdb +0 -0
  87. data/test/data/test-data/MaxMind-DB-test-mixed-32.mmdb +0 -0
  88. data/test/data/test-data/MaxMind-DB-test-nested.mmdb +0 -0
  89. data/test/data/test-data/MaxMind-DB-test-pointer-decoder.mmdb +0 -0
  90. data/test/data/test-data/README.md +26 -0
  91. data/test/data/test-data/maps-with-pointers.raw +0 -0
  92. data/test/data/test-data/write-test-data.pl +691 -0
  93. data/test/data/tidyall.ini +5 -0
  94. data/test/test_client.rb +426 -0
  95. data/test/test_model_country.rb +1 -1
  96. data/test/test_model_names.rb +1 -1
  97. data/test/test_reader.rb +92 -1
  98. metadata +194 -10
  99. data/Gemfile.lock +0 -36
@@ -1,21 +1,26 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module MaxMind::GeoIP2::Record
4
- class Abstract # :nodoc:
5
- def initialize(record)
6
- @record = record
7
- end
3
+ module MaxMind
4
+ module GeoIP2
5
+ module Record
6
+ # @!visibility private
7
+ class Abstract
8
+ def initialize(record)
9
+ @record = record
10
+ end
8
11
 
9
- protected
12
+ protected
10
13
 
11
- def get(key)
12
- if @record.nil? || !@record.key?(key)
13
- return false if key.start_with?('is_')
14
+ def get(key)
15
+ if @record.nil? || !@record.key?(key)
16
+ return false if key.start_with?('is_')
14
17
 
15
- return nil
16
- end
18
+ return nil
19
+ end
17
20
 
18
- @record[key]
21
+ @record[key]
22
+ end
23
+ end
19
24
  end
20
25
  end
21
26
  end
@@ -2,32 +2,41 @@
2
2
 
3
3
  require 'maxmind/geoip2/record/place'
4
4
 
5
- module MaxMind::GeoIP2::Record
6
- # City-level data associated with an IP address.
7
- #
8
- # This record is returned by all location services and databases besides
9
- # Country.
10
- #
11
- # See Place for inherited methods.
12
- class City < Place
13
- # A value from 0-100 indicating MaxMind's confidence that the city is
14
- # correct. This attribute is only available from the Insights service and
15
- # the GeoIP2 Enterprise database. Integer but may be nil.
16
- def confidence
17
- get('confidence')
18
- end
5
+ module MaxMind
6
+ module GeoIP2
7
+ module Record
8
+ # City-level data associated with an IP address.
9
+ #
10
+ # This record is returned by all location services and databases besides
11
+ # Country.
12
+ #
13
+ # See {MaxMind::GeoIP2::Record::Place} for inherited methods.
14
+ class City < Place
15
+ # A value from 0-100 indicating MaxMind's confidence that the city is
16
+ # correct. This attribute is only available from the Insights service and
17
+ # the GeoIP2 Enterprise database.
18
+ #
19
+ # @return [Integer, nil]
20
+ def confidence
21
+ get('confidence')
22
+ end
19
23
 
20
- # The GeoName ID for the city. This attribute is returned by all location
21
- # services and databases. Integer but may be nil.
22
- def geoname_id
23
- get('geoname_id')
24
- end
24
+ # The GeoName ID for the city. This attribute is returned by all location
25
+ # services and databases.
26
+ #
27
+ # @return [Integer, nil]
28
+ def geoname_id
29
+ get('geoname_id')
30
+ end
25
31
 
26
- # A Hash where the keys are locale codes (Strings) and the values are names
27
- # (Strings). This attribute is returned by all location services and
28
- # databases. Hash but may be nil.
29
- def names
30
- get('names')
32
+ # A Hash where the keys are locale codes and the values are names. This
33
+ # attribute is returned by all location services and databases.
34
+ #
35
+ # @return [Hash<String, String>, nil]
36
+ def names
37
+ get('names')
38
+ end
39
+ end
31
40
  end
32
41
  end
33
42
  end
@@ -2,31 +2,40 @@
2
2
 
3
3
  require 'maxmind/geoip2/record/place'
4
4
 
5
- module MaxMind::GeoIP2::Record
6
- # Contains data for the continent record associated with an IP address.
7
- #
8
- # This record is returned by all location services and databases.
9
- #
10
- # See Place for inherited methods.
11
- class Continent < Place
12
- # A two character continent code like "NA" (North America) or "OC"
13
- # (Oceania). This attribute is returned by all location services and
14
- # databases. String but may be nil.
15
- def code
16
- get('code')
17
- end
5
+ module MaxMind
6
+ module GeoIP2
7
+ module Record
8
+ # Contains data for the continent record associated with an IP address.
9
+ #
10
+ # This record is returned by all location services and databases.
11
+ #
12
+ # See {MaxMind::GeoIP2::Record::Place} for inherited methods.
13
+ class Continent < Place
14
+ # A two character continent code like "NA" (North America) or "OC"
15
+ # (Oceania). This attribute is returned by all location services and
16
+ # databases.
17
+ #
18
+ # @return [String, nil]
19
+ def code
20
+ get('code')
21
+ end
18
22
 
19
- # The GeoName ID for the continent. This attribute is returned by all
20
- # location services and databases. String but may be nil.
21
- def geoname_id
22
- get('geoname_id')
23
- end
23
+ # The GeoName ID for the continent. This attribute is returned by all
24
+ # location services and databases.
25
+ #
26
+ # @return [String, nil]
27
+ def geoname_id
28
+ get('geoname_id')
29
+ end
24
30
 
25
- # A Hash where the keys are locale codes (Strings) and the values are names
26
- # (Strings). This attribute is returned by all location services and
27
- # databases. Hash but may be nil.
28
- def names
29
- get('names')
31
+ # A Hash where the keys are locale codes and the values are names. This
32
+ # attribute is returned by all location services and databases.
33
+ #
34
+ # @return [Hash<String, String>, nil]
35
+ def names
36
+ get('names')
37
+ end
38
+ end
30
39
  end
31
40
  end
32
41
  end
@@ -2,44 +2,57 @@
2
2
 
3
3
  require 'maxmind/geoip2/record/place'
4
4
 
5
- module MaxMind::GeoIP2::Record
6
- # Contains data for the country record associated with an IP address.
7
- #
8
- # This record is returned by all location services and databases.
9
- #
10
- # See Place for inherited methods.
11
- class Country < Place
12
- # A value from 0-100 indicating MaxMind's confidence that the country is
13
- # correct. This attribute is only available from the Insights service and
14
- # the GeoIP2 Enterprise database. Integer but may be nil.
15
- def confidence
16
- get('confidence')
17
- end
5
+ module MaxMind
6
+ module GeoIP2
7
+ module Record
8
+ # Contains data for the country record associated with an IP address.
9
+ #
10
+ # This record is returned by all location services and databases.
11
+ #
12
+ # See {MaxMind::GeoIP2::Record::Place} for inherited methods.
13
+ class Country < Place
14
+ # A value from 0-100 indicating MaxMind's confidence that the country is
15
+ # correct. This attribute is only available from the Insights service and
16
+ # the GeoIP2 Enterprise database.
17
+ #
18
+ # @return [Integer, nil]
19
+ def confidence
20
+ get('confidence')
21
+ end
18
22
 
19
- # The GeoName ID for the country. This attribute is returned by all
20
- # location services and databases. Integer but may be nil.
21
- def geoname_id
22
- get('geoname_id')
23
- end
23
+ # The GeoName ID for the country. This attribute is returned by all
24
+ # location services and databases.
25
+ #
26
+ # @return [Integer, nil]
27
+ def geoname_id
28
+ get('geoname_id')
29
+ end
24
30
 
25
- # This is true if the country is a member state of the European Union. This
26
- # attribute is returned by all location services and databases. Boolean.
27
- def in_european_union?
28
- get('is_in_european_union')
29
- end
31
+ # This is true if the country is a member state of the European Union. This
32
+ # attribute is returned by all location services and databases.
33
+ #
34
+ # @return [Boolean]
35
+ def in_european_union?
36
+ get('is_in_european_union')
37
+ end
30
38
 
31
- # The two-character ISO 3166-1 alpha code for the country. See
32
- # https://en.wikipedia.org/wiki/ISO_3166-1. This attribute is returned by
33
- # all location services and databases. String but may be nil.
34
- def iso_code
35
- get('iso_code')
36
- end
39
+ # The two-character ISO 3166-1 alpha code for the country. See
40
+ # https://en.wikipedia.org/wiki/ISO_3166-1. This attribute is returned by
41
+ # all location services and databases.
42
+ #
43
+ # @return [String, nil]
44
+ def iso_code
45
+ get('iso_code')
46
+ end
37
47
 
38
- # A Hash where the keys are locale codes (Strings) and the values are names
39
- # (Strings). This attribute is returned by all location services and
40
- # databases. Hash but may be nil.
41
- def names
42
- get('names')
48
+ # A Hash where the keys are locale codes and the values are names. This
49
+ # attribute is returned by all location services and databases.
50
+ #
51
+ # @return [Hash<String, String>, nil]
52
+ def names
53
+ get('names')
54
+ end
55
+ end
43
56
  end
44
57
  end
45
58
  end
@@ -2,62 +2,76 @@
2
2
 
3
3
  require 'maxmind/geoip2/record/abstract'
4
4
 
5
- module MaxMind::GeoIP2::Record
6
- # Contains data for the location record associated with an IP address.
7
- #
8
- # This record is returned by all location services and databases besides
9
- # Country.
10
- class Location < Abstract
11
- # The approximate accuracy radius in kilometers around the latitude and
12
- # longitude for the IP address. This is the radius where we have a 67%
13
- # confidence that the device using the IP address resides within the circle
14
- # centered at the latitude and longitude with the provided radius. Integer
15
- # but may be nil.
16
- def accuracy_radius
17
- get('accuracy_radius')
18
- end
5
+ module MaxMind
6
+ module GeoIP2
7
+ module Record
8
+ # Contains data for the location record associated with an IP address.
9
+ #
10
+ # This record is returned by all location services and databases besides
11
+ # Country.
12
+ class Location < Abstract
13
+ # The approximate accuracy radius in kilometers around the latitude and
14
+ # longitude for the IP address. This is the radius where we have a 67%
15
+ # confidence that the device using the IP address resides within the circle
16
+ # centered at the latitude and longitude with the provided radius.
17
+ #
18
+ # @return [Integer, nil]
19
+ def accuracy_radius
20
+ get('accuracy_radius')
21
+ end
19
22
 
20
- # The average income in US dollars associated with the requested IP
21
- # address. This attribute is only available from the Insights service.
22
- # Integer but may be nil.
23
- def average_income
24
- get('average_income')
25
- end
23
+ # The average income in US dollars associated with the requested IP
24
+ # address. This attribute is only available from the Insights service.
25
+ #
26
+ # @return [Integer, nil]
27
+ def average_income
28
+ get('average_income')
29
+ end
26
30
 
27
- # The approximate latitude of the location associated with the IP address.
28
- # This value is not precise and should not be used to identify a particular
29
- # address or household. Float but may be nil.
30
- def latitude
31
- get('latitude')
32
- end
31
+ # The approximate latitude of the location associated with the IP address.
32
+ # This value is not precise and should not be used to identify a particular
33
+ # address or household.
34
+ #
35
+ # @return [Float, nil]
36
+ def latitude
37
+ get('latitude')
38
+ end
33
39
 
34
- # The approximate longitude of the location associated with the IP address.
35
- # This value is not precise and should not be used to identify a particular
36
- # address or household. Float but may be nil.
37
- def longitude
38
- get('longitude')
39
- end
40
+ # The approximate longitude of the location associated with the IP address.
41
+ # This value is not precise and should not be used to identify a particular
42
+ # address or household.
43
+ #
44
+ # @return [Float, nil]
45
+ def longitude
46
+ get('longitude')
47
+ end
40
48
 
41
- # The metro code of the location if the location is in the US. MaxMind
42
- # returns the same metro codes as the Google AdWords API. See
43
- # https://developers.google.com/adwords/api/docs/appendix/cities-DMAregions.
44
- # Integer but may be nil.
45
- def metro_code
46
- get('metro_code')
47
- end
49
+ # The metro code of the location if the location is in the US. MaxMind
50
+ # returns the same metro codes as the Google AdWords API. See
51
+ # https://developers.google.com/adwords/api/docs/appendix/cities-DMAregions.
52
+ #
53
+ # @return [Integer, nil]
54
+ def metro_code
55
+ get('metro_code')
56
+ end
48
57
 
49
- # The estimated population per square kilometer associated with the IP
50
- # address. This attribute is only available from the Insights service.
51
- # Integer but may be nil.
52
- def population_density
53
- get('population_density')
54
- end
58
+ # The estimated population per square kilometer associated with the IP
59
+ # address. This attribute is only available from the Insights service.
60
+ #
61
+ # @return [Integer, nil]
62
+ def population_density
63
+ get('population_density')
64
+ end
55
65
 
56
- # The time zone associated with location, as specified by the IANA Time
57
- # Zone Database, e.g., "America/New_York". See
58
- # https://www.iana.org/time-zones. String but may be nil.
59
- def time_zone
60
- get('time_zone')
66
+ # The time zone associated with location, as specified by the IANA Time
67
+ # Zone Database, e.g., "America/New_York". See
68
+ # https://www.iana.org/time-zones.
69
+ #
70
+ # @return [String, nil]
71
+ def time_zone
72
+ get('time_zone')
73
+ end
74
+ end
61
75
  end
62
76
  end
63
77
  end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'maxmind/geoip2/record/abstract'
4
+
5
+ module MaxMind
6
+ module GeoIP2
7
+ module Record
8
+ # Contains data about your account.
9
+ #
10
+ # This record is returned by all location services.
11
+ class MaxMind < Abstract
12
+ # The number of remaining queries you have for the service you are calling.
13
+ #
14
+ # @return [Integer, nil]
15
+ def queries_remaining
16
+ get('queries_remaining')
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -2,25 +2,31 @@
2
2
 
3
3
  require 'maxmind/geoip2/record/abstract'
4
4
 
5
- module MaxMind::GeoIP2::Record
6
- # Location data common to different location types.
7
- class Place < Abstract
8
- def initialize(record, locales) # :nodoc:
9
- super(record)
10
- @locales = locales
11
- end
5
+ module MaxMind
6
+ module GeoIP2
7
+ module Record
8
+ # Location data common to different location types.
9
+ class Place < Abstract
10
+ # @!visibility private
11
+ def initialize(record, locales)
12
+ super(record)
13
+ @locales = locales
14
+ end
12
15
 
13
- # A string containing the first available localized name in order of
14
- # preference.
15
- def name
16
- n = names
17
- return nil if n.nil?
16
+ # The first available localized name in order of preference.
17
+ #
18
+ # @return [String, nil]
19
+ def name
20
+ n = names
21
+ return nil if n.nil?
18
22
 
19
- @locales.each do |locale|
20
- return n[locale] if n.key?(locale)
21
- end
23
+ @locales.each do |locale|
24
+ return n[locale] if n.key?(locale)
25
+ end
22
26
 
23
- nil
27
+ nil
28
+ end
29
+ end
24
30
  end
25
31
  end
26
32
  end