maxmind-geoip2 0.1.0 → 0.6.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 (72) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +32 -0
  3. data/Gemfile +1 -5
  4. data/README.dev.md +1 -1
  5. data/README.md +82 -8
  6. data/lib/maxmind/geoip2.rb +1 -0
  7. data/lib/maxmind/geoip2/client.rb +333 -0
  8. data/lib/maxmind/geoip2/errors.rb +37 -3
  9. data/lib/maxmind/geoip2/model/abstract.rb +19 -15
  10. data/lib/maxmind/geoip2/model/anonymous_ip.rb +62 -50
  11. data/lib/maxmind/geoip2/model/asn.rb +33 -29
  12. data/lib/maxmind/geoip2/model/city.rb +59 -55
  13. data/lib/maxmind/geoip2/model/connection_type.rb +27 -23
  14. data/lib/maxmind/geoip2/model/country.rb +64 -53
  15. data/lib/maxmind/geoip2/model/domain.rb +27 -23
  16. data/lib/maxmind/geoip2/model/enterprise.rb +13 -9
  17. data/lib/maxmind/geoip2/model/insights.rb +18 -0
  18. data/lib/maxmind/geoip2/model/isp.rb +45 -41
  19. data/lib/maxmind/geoip2/reader.rb +260 -233
  20. data/lib/maxmind/geoip2/record/abstract.rb +17 -13
  21. data/lib/maxmind/geoip2/record/city.rb +33 -29
  22. data/lib/maxmind/geoip2/record/continent.rb +32 -28
  23. data/lib/maxmind/geoip2/record/country.rb +47 -43
  24. data/lib/maxmind/geoip2/record/location.rb +64 -60
  25. data/lib/maxmind/geoip2/record/maxmind.rb +21 -0
  26. data/lib/maxmind/geoip2/record/place.rb +22 -18
  27. data/lib/maxmind/geoip2/record/postal.rb +26 -22
  28. data/lib/maxmind/geoip2/record/represented_country.rb +20 -16
  29. data/lib/maxmind/geoip2/record/subdivision.rb +42 -38
  30. data/lib/maxmind/geoip2/record/traits.rb +204 -191
  31. data/maxmind-geoip2.gemspec +11 -3
  32. data/test/data/bad-data/maxminddb-python/bad-unicode-in-map-key.mmdb +0 -0
  33. data/test/data/source-data/GeoIP2-Anonymous-IP-Test.json +1 -0
  34. data/test/data/source-data/GeoIP2-ISP-Test.json +3 -1
  35. data/test/data/source-data/GeoIP2-Precision-Enterprise-Test.json +87 -0
  36. data/test/data/test-data/GeoIP2-Anonymous-IP-Test.mmdb +0 -0
  37. data/test/data/test-data/GeoIP2-City-Test-Broken-Double-Format.mmdb +0 -0
  38. data/test/data/test-data/GeoIP2-City-Test-Invalid-Node-Count.mmdb +0 -0
  39. data/test/data/test-data/GeoIP2-City-Test.mmdb +0 -0
  40. data/test/data/test-data/GeoIP2-Connection-Type-Test.mmdb +0 -0
  41. data/test/data/test-data/GeoIP2-Country-Test.mmdb +0 -0
  42. data/test/data/test-data/GeoIP2-DensityIncome-Test.mmdb +0 -0
  43. data/test/data/test-data/GeoIP2-Domain-Test.mmdb +0 -0
  44. data/test/data/test-data/GeoIP2-Enterprise-Test.mmdb +0 -0
  45. data/test/data/test-data/GeoIP2-ISP-Test.mmdb +0 -0
  46. data/test/data/test-data/GeoIP2-Precision-Enterprise-Test.mmdb +0 -0
  47. data/test/data/test-data/GeoIP2-Static-IP-Score-Test.mmdb +0 -0
  48. data/test/data/test-data/GeoIP2-User-Count-Test.mmdb +0 -0
  49. data/test/data/test-data/GeoLite2-ASN-Test.mmdb +0 -0
  50. data/test/data/test-data/MaxMind-DB-no-ipv4-search-tree.mmdb +0 -0
  51. data/test/data/test-data/MaxMind-DB-string-value-entries.mmdb +0 -0
  52. data/test/data/test-data/MaxMind-DB-test-broken-pointers-24.mmdb +0 -0
  53. data/test/data/test-data/MaxMind-DB-test-broken-search-tree-24.mmdb +0 -0
  54. data/test/data/test-data/MaxMind-DB-test-decoder.mmdb +0 -0
  55. data/test/data/test-data/MaxMind-DB-test-ipv4-24.mmdb +0 -0
  56. data/test/data/test-data/MaxMind-DB-test-ipv4-28.mmdb +0 -0
  57. data/test/data/test-data/MaxMind-DB-test-ipv4-32.mmdb +0 -0
  58. data/test/data/test-data/MaxMind-DB-test-ipv6-24.mmdb +0 -0
  59. data/test/data/test-data/MaxMind-DB-test-ipv6-28.mmdb +0 -0
  60. data/test/data/test-data/MaxMind-DB-test-ipv6-32.mmdb +0 -0
  61. data/test/data/test-data/MaxMind-DB-test-metadata-pointers.mmdb +0 -0
  62. data/test/data/test-data/MaxMind-DB-test-mixed-24.mmdb +0 -0
  63. data/test/data/test-data/MaxMind-DB-test-mixed-28.mmdb +0 -0
  64. data/test/data/test-data/MaxMind-DB-test-mixed-32.mmdb +0 -0
  65. data/test/data/test-data/MaxMind-DB-test-nested.mmdb +0 -0
  66. data/test/data/test-data/MaxMind-DB-test-pointer-decoder.mmdb +0 -0
  67. data/test/data/test-data/write-test-data.pl +68 -18
  68. data/test/test_client.rb +426 -0
  69. data/test/test_model_country.rb +16 -0
  70. data/test/test_reader.rb +59 -0
  71. metadata +113 -10
  72. data/Gemfile.lock +0 -38
@@ -1,22 +1,26 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module MaxMind::GeoIP2::Record
4
- # @!visibility private
5
- class Abstract
6
- def initialize(record)
7
- @record = record
8
- 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
9
11
 
10
- protected
12
+ protected
11
13
 
12
- def get(key)
13
- if @record.nil? || !@record.key?(key)
14
- 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_')
15
17
 
16
- return nil
17
- end
18
+ return nil
19
+ end
18
20
 
19
- @record[key]
21
+ @record[key]
22
+ end
23
+ end
20
24
  end
21
25
  end
22
26
  end
@@ -2,37 +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 {MaxMind::GeoIP2::Record::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.
16
- #
17
- # @return [Integer, nil]
18
- def confidence
19
- get('confidence')
20
- 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
21
23
 
22
- # The GeoName ID for the city. This attribute is returned by all location
23
- # services and databases.
24
- #
25
- # @return [Integer, nil]
26
- def geoname_id
27
- get('geoname_id')
28
- 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
29
31
 
30
- # A Hash where the keys are locale codes and the values are names. This
31
- # attribute is returned by all location services and databases.
32
- #
33
- # @return [Hash<String, String>, nil]
34
- def names
35
- 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
36
40
  end
37
41
  end
38
42
  end
@@ -2,36 +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 {MaxMind::GeoIP2::Record::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.
15
- #
16
- # @return [String, nil]
17
- def code
18
- get('code')
19
- 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
20
22
 
21
- # The GeoName ID for the continent. This attribute is returned by all
22
- # location services and databases.
23
- #
24
- # @return [String, nil]
25
- def geoname_id
26
- get('geoname_id')
27
- 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
28
30
 
29
- # A Hash where the keys are locale codes and the values are names. This
30
- # attribute is returned by all location services and databases.
31
- #
32
- # @return [Hash<String, String>, nil]
33
- def names
34
- 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
35
39
  end
36
40
  end
37
41
  end
@@ -2,53 +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 {MaxMind::GeoIP2::Record::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.
15
- #
16
- # @return [Integer, nil]
17
- def confidence
18
- get('confidence')
19
- 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
20
22
 
21
- # The GeoName ID for the country. This attribute is returned by all
22
- # location services and databases.
23
- #
24
- # @return [Integer, nil]
25
- def geoname_id
26
- get('geoname_id')
27
- 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
28
30
 
29
- # This is true if the country is a member state of the European Union. This
30
- # attribute is returned by all location services and databases.
31
- #
32
- # @return [Boolean]
33
- def in_european_union?
34
- get('is_in_european_union')
35
- 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
36
38
 
37
- # The two-character ISO 3166-1 alpha code for the country. See
38
- # https://en.wikipedia.org/wiki/ISO_3166-1. This attribute is returned by
39
- # all location services and databases.
40
- #
41
- # @return [String, nil]
42
- def iso_code
43
- get('iso_code')
44
- 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
45
47
 
46
- # A Hash where the keys are locale codes and the values are names. This
47
- # attribute is returned by all location services and databases.
48
- #
49
- # @return [Hash<String, String>, nil]
50
- def names
51
- 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
52
56
  end
53
57
  end
54
58
  end
@@ -2,72 +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.
15
- #
16
- # @return [Integer, nil]
17
- def accuracy_radius
18
- get('accuracy_radius')
19
- 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
20
22
 
21
- # The average income in US dollars associated with the requested IP
22
- # address. This attribute is only available from the Insights service.
23
- #
24
- # @return [Integer, nil]
25
- def average_income
26
- get('average_income')
27
- 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
28
30
 
29
- # The approximate latitude of the location associated with the IP address.
30
- # This value is not precise and should not be used to identify a particular
31
- # address or household.
32
- #
33
- # @return [Float, nil]
34
- def latitude
35
- get('latitude')
36
- 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
37
39
 
38
- # The approximate longitude of the location associated with the IP address.
39
- # This value is not precise and should not be used to identify a particular
40
- # address or household.
41
- #
42
- # @return [Float, nil]
43
- def longitude
44
- get('longitude')
45
- 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
46
48
 
47
- # The metro code of the location if the location is in the US. MaxMind
48
- # returns the same metro codes as the Google AdWords API. See
49
- # https://developers.google.com/adwords/api/docs/appendix/cities-DMAregions.
50
- #
51
- # @return [Integer, nil]
52
- def metro_code
53
- get('metro_code')
54
- 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
55
57
 
56
- # The estimated population per square kilometer associated with the IP
57
- # address. This attribute is only available from the Insights service.
58
- #
59
- # @return [Integer, nil]
60
- def population_density
61
- get('population_density')
62
- 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
63
65
 
64
- # The time zone associated with location, as specified by the IANA Time
65
- # Zone Database, e.g., "America/New_York". See
66
- # https://www.iana.org/time-zones.
67
- #
68
- # @return [String, nil]
69
- def time_zone
70
- 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
71
75
  end
72
76
  end
73
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,27 +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
- # @!visibility private
9
- def initialize(record, locales)
10
- super(record)
11
- @locales = locales
12
- 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
13
15
 
14
- # The first available localized name in order of preference.
15
- #
16
- # @return [String, nil]
17
- def name
18
- n = names
19
- 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?
20
22
 
21
- @locales.each do |locale|
22
- return n[locale] if n.key?(locale)
23
- end
23
+ @locales.each do |locale|
24
+ return n[locale] if n.key?(locale)
25
+ end
24
26
 
25
- nil
27
+ nil
28
+ end
29
+ end
26
30
  end
27
31
  end
28
32
  end