maxmind-geoip2 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +6 -0
  3. data/Gemfile.lock +3 -3
  4. data/lib/maxmind/geoip2/client.rb +269 -267
  5. data/lib/maxmind/geoip2/errors.rb +0 -7
  6. data/lib/maxmind/geoip2/model/abstract.rb +19 -15
  7. data/lib/maxmind/geoip2/model/anonymous_ip.rb +54 -50
  8. data/lib/maxmind/geoip2/model/asn.rb +33 -29
  9. data/lib/maxmind/geoip2/model/city.rb +59 -55
  10. data/lib/maxmind/geoip2/model/connection_type.rb +27 -23
  11. data/lib/maxmind/geoip2/model/country.rb +57 -53
  12. data/lib/maxmind/geoip2/model/domain.rb +27 -23
  13. data/lib/maxmind/geoip2/model/enterprise.rb +13 -9
  14. data/lib/maxmind/geoip2/model/insights.rb +12 -8
  15. data/lib/maxmind/geoip2/model/isp.rb +45 -41
  16. data/lib/maxmind/geoip2/reader.rb +242 -240
  17. data/lib/maxmind/geoip2/record/abstract.rb +17 -13
  18. data/lib/maxmind/geoip2/record/city.rb +33 -29
  19. data/lib/maxmind/geoip2/record/continent.rb +32 -28
  20. data/lib/maxmind/geoip2/record/country.rb +47 -43
  21. data/lib/maxmind/geoip2/record/location.rb +64 -60
  22. data/lib/maxmind/geoip2/record/maxmind.rb +14 -10
  23. data/lib/maxmind/geoip2/record/place.rb +22 -18
  24. data/lib/maxmind/geoip2/record/postal.rb +26 -22
  25. data/lib/maxmind/geoip2/record/represented_country.rb +20 -16
  26. data/lib/maxmind/geoip2/record/subdivision.rb +42 -38
  27. data/lib/maxmind/geoip2/record/traits.rb +195 -191
  28. data/maxmind-geoip2.gemspec +1 -1
  29. data/test/data/LICENSE +4 -0
  30. data/test/data/MaxMind-DB-spec.md +570 -0
  31. data/test/data/MaxMind-DB-test-metadata-pointers.mmdb +0 -0
  32. data/test/data/README.md +4 -0
  33. data/test/data/bad-data/README.md +7 -0
  34. data/test/data/bad-data/libmaxminddb/libmaxminddb-offset-integer-overflow.mmdb +0 -0
  35. data/test/data/bad-data/maxminddb-golang/cyclic-data-structure.mmdb +0 -0
  36. data/test/data/bad-data/maxminddb-golang/invalid-bytes-length.mmdb +1 -0
  37. data/test/data/bad-data/maxminddb-golang/invalid-data-record-offset.mmdb +0 -0
  38. data/test/data/bad-data/maxminddb-golang/invalid-map-key-length.mmdb +0 -0
  39. data/test/data/bad-data/maxminddb-golang/invalid-string-length.mmdb +1 -0
  40. data/test/data/bad-data/maxminddb-golang/metadata-is-an-uint128.mmdb +1 -0
  41. data/test/data/bad-data/maxminddb-golang/unexpected-bytes.mmdb +0 -0
  42. data/test/data/perltidyrc +12 -0
  43. data/test/data/source-data/GeoIP2-Anonymous-IP-Test.json +48 -0
  44. data/test/data/source-data/GeoIP2-City-Test.json +12852 -0
  45. data/test/data/source-data/GeoIP2-Connection-Type-Test.json +102 -0
  46. data/test/data/source-data/GeoIP2-Country-Test.json +15916 -0
  47. data/test/data/source-data/GeoIP2-DensityIncome-Test.json +14 -0
  48. data/test/data/source-data/GeoIP2-Domain-Test.json +452 -0
  49. data/test/data/source-data/GeoIP2-Enterprise-Test.json +687 -0
  50. data/test/data/source-data/GeoIP2-ISP-Test.json +12593 -0
  51. data/test/data/source-data/GeoIP2-Precision-Enterprise-Test.json +2061 -0
  52. data/test/data/source-data/GeoIP2-Static-IP-Score-Test.json +2132 -0
  53. data/test/data/source-data/GeoIP2-User-Count-Test.json +2837 -0
  54. data/test/data/source-data/GeoLite2-ASN-Test.json +37 -0
  55. data/test/data/source-data/README +15 -0
  56. data/test/data/test-data/GeoIP2-Anonymous-IP-Test.mmdb +0 -0
  57. data/test/data/test-data/GeoIP2-City-Test-Broken-Double-Format.mmdb +0 -0
  58. data/test/data/test-data/GeoIP2-City-Test-Invalid-Node-Count.mmdb +0 -0
  59. data/test/data/test-data/GeoIP2-City-Test.mmdb +0 -0
  60. data/test/data/test-data/GeoIP2-Connection-Type-Test.mmdb +0 -0
  61. data/test/data/test-data/GeoIP2-Country-Test.mmdb +0 -0
  62. data/test/data/test-data/GeoIP2-DensityIncome-Test.mmdb +0 -0
  63. data/test/data/test-data/GeoIP2-Domain-Test.mmdb +0 -0
  64. data/test/data/test-data/GeoIP2-Enterprise-Test.mmdb +0 -0
  65. data/test/data/test-data/GeoIP2-ISP-Test.mmdb +0 -0
  66. data/test/data/test-data/GeoIP2-Precision-Enterprise-Test.mmdb +0 -0
  67. data/test/data/test-data/GeoIP2-Static-IP-Score-Test.mmdb +0 -0
  68. data/test/data/test-data/GeoIP2-User-Count-Test.mmdb +0 -0
  69. data/test/data/test-data/GeoLite2-ASN-Test.mmdb +0 -0
  70. data/test/data/test-data/MaxMind-DB-no-ipv4-search-tree.mmdb +0 -0
  71. data/test/data/test-data/MaxMind-DB-string-value-entries.mmdb +0 -0
  72. data/test/data/test-data/MaxMind-DB-test-broken-pointers-24.mmdb +0 -0
  73. data/test/data/test-data/MaxMind-DB-test-broken-search-tree-24.mmdb +0 -0
  74. data/test/data/test-data/MaxMind-DB-test-decoder.mmdb +0 -0
  75. data/test/data/test-data/MaxMind-DB-test-ipv4-24.mmdb +0 -0
  76. data/test/data/test-data/MaxMind-DB-test-ipv4-28.mmdb +0 -0
  77. data/test/data/test-data/MaxMind-DB-test-ipv4-32.mmdb +0 -0
  78. data/test/data/test-data/MaxMind-DB-test-ipv6-24.mmdb +0 -0
  79. data/test/data/test-data/MaxMind-DB-test-ipv6-28.mmdb +0 -0
  80. data/test/data/test-data/MaxMind-DB-test-ipv6-32.mmdb +0 -0
  81. data/test/data/test-data/MaxMind-DB-test-metadata-pointers.mmdb +0 -0
  82. data/test/data/test-data/MaxMind-DB-test-mixed-24.mmdb +0 -0
  83. data/test/data/test-data/MaxMind-DB-test-mixed-28.mmdb +0 -0
  84. data/test/data/test-data/MaxMind-DB-test-mixed-32.mmdb +0 -0
  85. data/test/data/test-data/MaxMind-DB-test-nested.mmdb +0 -0
  86. data/test/data/test-data/README.md +26 -0
  87. data/test/data/test-data/maps-with-pointers.raw +0 -0
  88. data/test/data/test-data/write-test-data.pl +641 -0
  89. data/test/data/tidyall.ini +5 -0
  90. metadata +64 -3
@@ -2,31 +2,35 @@
2
2
 
3
3
  require 'maxmind/geoip2/model/abstract'
4
4
 
5
- module MaxMind::GeoIP2::Model
6
- # Model class for the GeoIP2 Domain database.
7
- class Domain < Abstract
8
- # The second level domain associated with the IP address. This will be
9
- # something like "example.com" or "example.co.uk", not "foo.example.com".
10
- #
11
- # @return [String, nil]
12
- def domain
13
- get('domain')
14
- end
5
+ module MaxMind
6
+ module GeoIP2
7
+ module Model
8
+ # Model class for the GeoIP2 Domain database.
9
+ class Domain < Abstract
10
+ # The second level domain associated with the IP address. This will be
11
+ # something like "example.com" or "example.co.uk", not "foo.example.com".
12
+ #
13
+ # @return [String, nil]
14
+ def domain
15
+ get('domain')
16
+ end
15
17
 
16
- # The IP address that the data in the model is for.
17
- #
18
- # @return [String]
19
- def ip_address
20
- get('ip_address')
21
- end
18
+ # The IP address that the data in the model is for.
19
+ #
20
+ # @return [String]
21
+ def ip_address
22
+ get('ip_address')
23
+ end
22
24
 
23
- # The network in CIDR notation associated with the record. In particular,
24
- # this is the largest network where all of the fields besides ip_address
25
- # have the same value.
26
- #
27
- # @return [String]
28
- def network
29
- get('network')
25
+ # The network in CIDR notation associated with the record. In particular,
26
+ # this is the largest network where all of the fields besides ip_address
27
+ # have the same value.
28
+ #
29
+ # @return [String]
30
+ def network
31
+ get('network')
32
+ end
33
+ end
30
34
  end
31
35
  end
32
36
  end
@@ -2,14 +2,18 @@
2
2
 
3
3
  require 'maxmind/geoip2/model/city'
4
4
 
5
- module MaxMind::GeoIP2::Model
6
- # Model class for the data returned by GeoIP2 Enterprise database lookups.
7
- #
8
- # The only difference between the City and Insights model classes is which
9
- # fields in each record may be populated. See
10
- # https://dev.maxmind.com/geoip/geoip2/web-services for more details.
11
- #
12
- # See {MaxMind::GeoIP2::Model::City} for inherited methods.
13
- class Enterprise < City
5
+ module MaxMind
6
+ module GeoIP2
7
+ module Model
8
+ # Model class for the data returned by GeoIP2 Enterprise database lookups.
9
+ #
10
+ # The only difference between the City and Insights model classes is which
11
+ # fields in each record may be populated. See
12
+ # https://dev.maxmind.com/geoip/geoip2/web-services for more details.
13
+ #
14
+ # See {MaxMind::GeoIP2::Model::City} for inherited methods.
15
+ class Enterprise < City
16
+ end
17
+ end
14
18
  end
15
19
  end
@@ -2,13 +2,17 @@
2
2
 
3
3
  require 'maxmind/geoip2/model/city'
4
4
 
5
- module MaxMind::GeoIP2::Model
6
- # Model class for the data returned by the GeoIP2 Precision Insights web
7
- # service.
8
- #
9
- # The only difference between the City and Insights model classes is which
10
- # fields in each record may be populated. See
11
- # https://dev.maxmind.com/geoip/geoip2/web-services for more details.
12
- class Insights < City
5
+ module MaxMind
6
+ module GeoIP2
7
+ module Model
8
+ # Model class for the data returned by the GeoIP2 Precision Insights web
9
+ # service.
10
+ #
11
+ # The only difference between the City and Insights model classes is which
12
+ # fields in each record may be populated. See
13
+ # https://dev.maxmind.com/geoip/geoip2/web-services for more details.
14
+ class Insights < City
15
+ end
16
+ end
13
17
  end
14
18
  end
@@ -2,52 +2,56 @@
2
2
 
3
3
  require 'maxmind/geoip2/model/abstract'
4
4
 
5
- module MaxMind::GeoIP2::Model
6
- # Model class for the GeoIP2 ISP database.
7
- class ISP < Abstract
8
- # The autonomous system number associated with the IP address.
9
- #
10
- # @return [Integer, nil]
11
- def autonomous_system_number
12
- get('autonomous_system_number')
13
- end
5
+ module MaxMind
6
+ module GeoIP2
7
+ module Model
8
+ # Model class for the GeoIP2 ISP database.
9
+ class ISP < Abstract
10
+ # The autonomous system number associated with the IP address.
11
+ #
12
+ # @return [Integer, nil]
13
+ def autonomous_system_number
14
+ get('autonomous_system_number')
15
+ end
14
16
 
15
- # The organization associated with the registered autonomous system number
16
- # for the IP address.
17
- #
18
- # @return [String, nil]
19
- def autonomous_system_organization
20
- get('autonomous_system_organization')
21
- end
17
+ # The organization associated with the registered autonomous system number
18
+ # for the IP address.
19
+ #
20
+ # @return [String, nil]
21
+ def autonomous_system_organization
22
+ get('autonomous_system_organization')
23
+ end
22
24
 
23
- # The IP address that the data in the model is for.
24
- #
25
- # @return [String]
26
- def ip_address
27
- get('ip_address')
28
- end
25
+ # The IP address that the data in the model is for.
26
+ #
27
+ # @return [String]
28
+ def ip_address
29
+ get('ip_address')
30
+ end
29
31
 
30
- # The name of the ISP associated with the IP address.
31
- #
32
- # @return [String, nil]
33
- def isp
34
- get('isp')
35
- end
32
+ # The name of the ISP associated with the IP address.
33
+ #
34
+ # @return [String, nil]
35
+ def isp
36
+ get('isp')
37
+ end
36
38
 
37
- # The network in CIDR notation associated with the record. In particular,
38
- # this is the largest network where all of the fields besides ip_address
39
- # have the same value.
40
- #
41
- # @return [String]
42
- def network
43
- get('network')
44
- end
39
+ # The network in CIDR notation associated with the record. In particular,
40
+ # this is the largest network where all of the fields besides ip_address
41
+ # have the same value.
42
+ #
43
+ # @return [String]
44
+ def network
45
+ get('network')
46
+ end
45
47
 
46
- # The name of the organization associated with the IP address.
47
- #
48
- # @return [String, nil]
49
- def organization
50
- get('organization')
48
+ # The name of the organization associated with the IP address.
49
+ #
50
+ # @return [String, nil]
51
+ def organization
52
+ get('organization')
53
+ end
54
+ end
51
55
  end
52
56
  end
53
57
  end
@@ -11,267 +11,269 @@ require 'maxmind/geoip2/model/domain'
11
11
  require 'maxmind/geoip2/model/enterprise'
12
12
  require 'maxmind/geoip2/model/isp'
13
13
 
14
- module MaxMind::GeoIP2
15
- # Reader is a reader for the GeoIP2/GeoLite2 database format. IP addresses
16
- # can be looked up using the database specific methods.
17
- #
18
- # == Example
19
- #
20
- # require 'maxmind/geoip2'
21
- #
22
- # reader = MaxMind::GeoIP2::Reader.new('GeoIP2-Country.mmdb')
23
- #
24
- # record = reader.country('1.2.3.4')
25
- # puts record.country.iso_code
26
- #
27
- # reader.close
28
- class Reader
29
- # Create a Reader for looking up IP addresses in a GeoIP2/GeoLite2 database
30
- # file.
31
- #
32
- # If you're performing multiple lookups, it's most efficient to create one
33
- # Reader and reuse it.
34
- #
35
- # Once created, the Reader is safe to use for lookups from multiple
36
- # threads. It is safe to use after forking only if you use
37
- # MaxMind::DB::MODE_MEMORY or if your version of Ruby supports IO#pread.
38
- #
39
- # @param database [String] a path to a GeoIP2/GeoLite2 database file.
40
- #
41
- # @param locales [Array<String>] a list of locale codes to use in the name
42
- # property from most preferred to least preferred.
43
- #
44
- # @param options [Hash<Symbol, Symbol>] options controlling the behavior of
45
- # the Reader.
46
- #
47
- # @option options [Symbol] :mode Defines how to open the database. It may
48
- # be one of MaxMind::DB::MODE_AUTO, MaxMind::DB::MODE_FILE, or
49
- # MaxMind::DB::MODE_MEMORY. If you don't provide one, the Reader uses
50
- # MaxMind::DB::MODE_AUTO. Refer to the definition of those constants in
51
- # MaxMind::DB for an explanation of their meaning.
52
- #
53
- # @raise [MaxMind::DB::InvalidDatabaseError] if the database is corrupt or
54
- # invalid.
55
- #
56
- # @raise [ArgumentError] if the mode is invalid.
57
- def initialize(database, locales = ['en'], options = {})
58
- @reader = MaxMind::DB.new(database, options)
59
- @type = @reader.metadata.database_type
60
- locales = ['en'] if locales.empty?
61
- @locales = locales
62
- end
63
-
64
- # Look up the IP address in the database.
65
- #
66
- # @param ip_address [String] a string in the standard notation. It may be
67
- # IPv4 or IPv6.
68
- #
69
- # @return [MaxMind::GeoIP2::Model::AnonymousIP]
70
- #
71
- # @raise [ArgumentError] if used against a non-Anonymous IP database or if
72
- # you attempt to look up an IPv6 address in an IPv4 only database.
73
- #
74
- # @raise [AddressNotFoundError] if the IP address is not found in the
75
- # database.
76
- #
77
- # @raise [MaxMind::DB::InvalidDatabaseError] if the database appears
78
- # corrupt.
79
- def anonymous_ip(ip_address)
80
- flat_model_for(
81
- Model::AnonymousIP,
82
- 'anonymous_ip',
83
- 'GeoIP2-Anonymous-IP',
84
- ip_address,
85
- )
86
- end
14
+ module MaxMind
15
+ module GeoIP2
16
+ # Reader is a reader for the GeoIP2/GeoLite2 database format. IP addresses
17
+ # can be looked up using the database specific methods.
18
+ #
19
+ # == Example
20
+ #
21
+ # require 'maxmind/geoip2'
22
+ #
23
+ # reader = MaxMind::GeoIP2::Reader.new('GeoIP2-Country.mmdb')
24
+ #
25
+ # record = reader.country('1.2.3.4')
26
+ # puts record.country.iso_code
27
+ #
28
+ # reader.close
29
+ class Reader
30
+ # Create a Reader for looking up IP addresses in a GeoIP2/GeoLite2 database
31
+ # file.
32
+ #
33
+ # If you're performing multiple lookups, it's most efficient to create one
34
+ # Reader and reuse it.
35
+ #
36
+ # Once created, the Reader is safe to use for lookups from multiple
37
+ # threads. It is safe to use after forking only if you use
38
+ # MaxMind::DB::MODE_MEMORY or if your version of Ruby supports IO#pread.
39
+ #
40
+ # @param database [String] a path to a GeoIP2/GeoLite2 database file.
41
+ #
42
+ # @param locales [Array<String>] a list of locale codes to use in the name
43
+ # property from most preferred to least preferred.
44
+ #
45
+ # @param options [Hash<Symbol, Symbol>] options controlling the behavior of
46
+ # the Reader.
47
+ #
48
+ # @option options [Symbol] :mode Defines how to open the database. It may
49
+ # be one of MaxMind::DB::MODE_AUTO, MaxMind::DB::MODE_FILE, or
50
+ # MaxMind::DB::MODE_MEMORY. If you don't provide one, the Reader uses
51
+ # MaxMind::DB::MODE_AUTO. Refer to the definition of those constants in
52
+ # MaxMind::DB for an explanation of their meaning.
53
+ #
54
+ # @raise [MaxMind::DB::InvalidDatabaseError] if the database is corrupt or
55
+ # invalid.
56
+ #
57
+ # @raise [ArgumentError] if the mode is invalid.
58
+ def initialize(database, locales = ['en'], options = {})
59
+ @reader = MaxMind::DB.new(database, options)
60
+ @type = @reader.metadata.database_type
61
+ locales = ['en'] if locales.empty?
62
+ @locales = locales
63
+ end
87
64
 
88
- # Look up the IP address in an ASN database.
89
- #
90
- # @param ip_address [String] a string in the standard notation. It may be
91
- # IPv4 or IPv6.
92
- #
93
- # @return [MaxMind::GeoIP2::Model::ASN]
94
- #
95
- # @raise [ArgumentError] if used against a non-ASN database or if you
96
- # attempt to look up an IPv6 address in an IPv4 only database.
97
- #
98
- # @raise [AddressNotFoundError] if the IP address is not found in the
99
- # database.
100
- #
101
- # @raise [MaxMind::DB::InvalidDatabaseError] if the database appears
102
- # corrupt.
103
- def asn(ip_address)
104
- flat_model_for(Model::ASN, 'asn', 'GeoLite2-ASN', ip_address)
105
- end
65
+ # Look up the IP address in the database.
66
+ #
67
+ # @param ip_address [String] a string in the standard notation. It may be
68
+ # IPv4 or IPv6.
69
+ #
70
+ # @return [MaxMind::GeoIP2::Model::AnonymousIP]
71
+ #
72
+ # @raise [ArgumentError] if used against a non-Anonymous IP database or if
73
+ # you attempt to look up an IPv6 address in an IPv4 only database.
74
+ #
75
+ # @raise [AddressNotFoundError] if the IP address is not found in the
76
+ # database.
77
+ #
78
+ # @raise [MaxMind::DB::InvalidDatabaseError] if the database appears
79
+ # corrupt.
80
+ def anonymous_ip(ip_address)
81
+ flat_model_for(
82
+ Model::AnonymousIP,
83
+ 'anonymous_ip',
84
+ 'GeoIP2-Anonymous-IP',
85
+ ip_address,
86
+ )
87
+ end
106
88
 
107
- # Look up the IP address in a City database.
108
- #
109
- # @param ip_address [String] a string in the standard notation. It may be
110
- # IPv4 or IPv6.
111
- #
112
- # @return [MaxMind::GeoIP2::Model::City]
113
- #
114
- # @raise [ArgumentError] if used against a non-City database or if you
115
- # attempt to look up an IPv6 address in an IPv4 only database.
116
- #
117
- # @raise [AddressNotFoundError] if the IP address is not found in the
118
- # database.
119
- #
120
- # @raise [MaxMind::DB::InvalidDatabaseError] if the database appears
121
- # corrupt.
122
- def city(ip_address)
123
- model_for(Model::City, 'city', 'City', ip_address)
124
- end
89
+ # Look up the IP address in an ASN database.
90
+ #
91
+ # @param ip_address [String] a string in the standard notation. It may be
92
+ # IPv4 or IPv6.
93
+ #
94
+ # @return [MaxMind::GeoIP2::Model::ASN]
95
+ #
96
+ # @raise [ArgumentError] if used against a non-ASN database or if you
97
+ # attempt to look up an IPv6 address in an IPv4 only database.
98
+ #
99
+ # @raise [AddressNotFoundError] if the IP address is not found in the
100
+ # database.
101
+ #
102
+ # @raise [MaxMind::DB::InvalidDatabaseError] if the database appears
103
+ # corrupt.
104
+ def asn(ip_address)
105
+ flat_model_for(Model::ASN, 'asn', 'GeoLite2-ASN', ip_address)
106
+ end
125
107
 
126
- # Look up the IP address in a Connection Type database.
127
- #
128
- # @param ip_address [String] a string in the standard notation. It may be
129
- # IPv4 or IPv6.
130
- #
131
- # @return [MaxMind::GeoIP2::Model::ConnectionType]
132
- #
133
- # @raise [ArgumentError] if used against a non-Connection Type database or if
134
- # you attempt to look up an IPv6 address in an IPv4 only database.
135
- #
136
- # @raise [AddressNotFoundError] if the IP address is not found in the
137
- # database.
138
- #
139
- # @raise [MaxMind::DB::InvalidDatabaseError] if the database appears
140
- # corrupt.
141
- def connection_type(ip_address)
142
- flat_model_for(
143
- Model::ConnectionType,
144
- 'connection_type',
145
- 'GeoIP2-Connection-Type',
146
- ip_address,
147
- )
148
- end
108
+ # Look up the IP address in a City database.
109
+ #
110
+ # @param ip_address [String] a string in the standard notation. It may be
111
+ # IPv4 or IPv6.
112
+ #
113
+ # @return [MaxMind::GeoIP2::Model::City]
114
+ #
115
+ # @raise [ArgumentError] if used against a non-City database or if you
116
+ # attempt to look up an IPv6 address in an IPv4 only database.
117
+ #
118
+ # @raise [AddressNotFoundError] if the IP address is not found in the
119
+ # database.
120
+ #
121
+ # @raise [MaxMind::DB::InvalidDatabaseError] if the database appears
122
+ # corrupt.
123
+ def city(ip_address)
124
+ model_for(Model::City, 'city', 'City', ip_address)
125
+ end
149
126
 
150
- # Look up the IP address in a Country database.
151
- #
152
- # @param ip_address [String] a string in the standard notation. It may be
153
- # IPv4 or IPv6.
154
- #
155
- # @return [MaxMind::GeoIP2::Model::Country]
156
- #
157
- # @raise [ArgumentError] if used against a non-Country database or if you
158
- # attempt to look up an IPv6 address in an IPv4 only database.
159
- #
160
- # @raise [AddressNotFoundError] if the IP address is not found in the
161
- # database.
162
- #
163
- # @raise [MaxMind::DB::InvalidDatabaseError] if the database appears
164
- # corrupt.
165
- def country(ip_address)
166
- model_for(Model::Country, 'country', 'Country', ip_address)
167
- end
127
+ # Look up the IP address in a Connection Type database.
128
+ #
129
+ # @param ip_address [String] a string in the standard notation. It may be
130
+ # IPv4 or IPv6.
131
+ #
132
+ # @return [MaxMind::GeoIP2::Model::ConnectionType]
133
+ #
134
+ # @raise [ArgumentError] if used against a non-Connection Type database or if
135
+ # you attempt to look up an IPv6 address in an IPv4 only database.
136
+ #
137
+ # @raise [AddressNotFoundError] if the IP address is not found in the
138
+ # database.
139
+ #
140
+ # @raise [MaxMind::DB::InvalidDatabaseError] if the database appears
141
+ # corrupt.
142
+ def connection_type(ip_address)
143
+ flat_model_for(
144
+ Model::ConnectionType,
145
+ 'connection_type',
146
+ 'GeoIP2-Connection-Type',
147
+ ip_address,
148
+ )
149
+ end
168
150
 
169
- # Look up the IP address in a Domain database.
170
- #
171
- # @param ip_address [String] a string in the standard notation. It may be
172
- # IPv4 or IPv6.
173
- #
174
- # @return [MaxMind::GeoIP2::Model::Domain]
175
- #
176
- # @raise [ArgumentError] if used against a non-Domain database or if you
177
- # attempt to look up an IPv6 address in an IPv4 only database.
178
- #
179
- # @raise [AddressNotFoundError] if the IP address is not found in the
180
- # database.
181
- #
182
- # @raise [MaxMind::DB::InvalidDatabaseError] if the database appears
183
- # corrupt.
184
- def domain(ip_address)
185
- flat_model_for(Model::Domain, 'domain', 'GeoIP2-Domain', ip_address)
186
- end
151
+ # Look up the IP address in a Country database.
152
+ #
153
+ # @param ip_address [String] a string in the standard notation. It may be
154
+ # IPv4 or IPv6.
155
+ #
156
+ # @return [MaxMind::GeoIP2::Model::Country]
157
+ #
158
+ # @raise [ArgumentError] if used against a non-Country database or if you
159
+ # attempt to look up an IPv6 address in an IPv4 only database.
160
+ #
161
+ # @raise [AddressNotFoundError] if the IP address is not found in the
162
+ # database.
163
+ #
164
+ # @raise [MaxMind::DB::InvalidDatabaseError] if the database appears
165
+ # corrupt.
166
+ def country(ip_address)
167
+ model_for(Model::Country, 'country', 'Country', ip_address)
168
+ end
187
169
 
188
- # Look up the IP address in an Enterprise database.
189
- #
190
- # @param ip_address [String] a string in the standard notation. It may be
191
- # IPv4 or IPv6.
192
- #
193
- # @return [MaxMind::GeoIP2::Model::Enterprise]
194
- #
195
- # @raise [ArgumentError] if used against a non-Enterprise database or if
196
- # you attempt to look up an IPv6 address in an IPv4 only database.
197
- #
198
- # @raise [AddressNotFoundError] if the IP address is not found in the
199
- # database.
200
- #
201
- # @raise [MaxMind::DB::InvalidDatabaseError] if the database appears
202
- # corrupt.
203
- def enterprise(ip_address)
204
- model_for(Model::Enterprise, 'enterprise', 'Enterprise', ip_address)
205
- end
170
+ # Look up the IP address in a Domain database.
171
+ #
172
+ # @param ip_address [String] a string in the standard notation. It may be
173
+ # IPv4 or IPv6.
174
+ #
175
+ # @return [MaxMind::GeoIP2::Model::Domain]
176
+ #
177
+ # @raise [ArgumentError] if used against a non-Domain database or if you
178
+ # attempt to look up an IPv6 address in an IPv4 only database.
179
+ #
180
+ # @raise [AddressNotFoundError] if the IP address is not found in the
181
+ # database.
182
+ #
183
+ # @raise [MaxMind::DB::InvalidDatabaseError] if the database appears
184
+ # corrupt.
185
+ def domain(ip_address)
186
+ flat_model_for(Model::Domain, 'domain', 'GeoIP2-Domain', ip_address)
187
+ end
206
188
 
207
- # Look up the IP address in an ISP database.
208
- #
209
- # @param ip_address [String] a string in the standard notation. It may be
210
- # IPv4 or IPv6.
211
- #
212
- # @return [MaxMind::GeoIP2::Model::ISP]
213
- #
214
- # @raise [ArgumentError] if used against a non-ISP database or if you
215
- # attempt to look up an IPv6 address in an IPv4 only database.
216
- #
217
- # @raise [AddressNotFoundError] if the IP address is not found in the
218
- # database.
219
- #
220
- # @raise [MaxMind::DB::InvalidDatabaseError] if the database appears
221
- # corrupt.
222
- def isp(ip_address)
223
- flat_model_for(Model::ISP, 'isp', 'GeoIP2-ISP', ip_address)
224
- end
189
+ # Look up the IP address in an Enterprise database.
190
+ #
191
+ # @param ip_address [String] a string in the standard notation. It may be
192
+ # IPv4 or IPv6.
193
+ #
194
+ # @return [MaxMind::GeoIP2::Model::Enterprise]
195
+ #
196
+ # @raise [ArgumentError] if used against a non-Enterprise database or if
197
+ # you attempt to look up an IPv6 address in an IPv4 only database.
198
+ #
199
+ # @raise [AddressNotFoundError] if the IP address is not found in the
200
+ # database.
201
+ #
202
+ # @raise [MaxMind::DB::InvalidDatabaseError] if the database appears
203
+ # corrupt.
204
+ def enterprise(ip_address)
205
+ model_for(Model::Enterprise, 'enterprise', 'Enterprise', ip_address)
206
+ end
225
207
 
226
- # Return the metadata associated with the database.
227
- #
228
- # @return [MaxMind::DB::Metadata]
229
- def metadata
230
- @reader.metadata
231
- end
208
+ # Look up the IP address in an ISP database.
209
+ #
210
+ # @param ip_address [String] a string in the standard notation. It may be
211
+ # IPv4 or IPv6.
212
+ #
213
+ # @return [MaxMind::GeoIP2::Model::ISP]
214
+ #
215
+ # @raise [ArgumentError] if used against a non-ISP database or if you
216
+ # attempt to look up an IPv6 address in an IPv4 only database.
217
+ #
218
+ # @raise [AddressNotFoundError] if the IP address is not found in the
219
+ # database.
220
+ #
221
+ # @raise [MaxMind::DB::InvalidDatabaseError] if the database appears
222
+ # corrupt.
223
+ def isp(ip_address)
224
+ flat_model_for(Model::ISP, 'isp', 'GeoIP2-ISP', ip_address)
225
+ end
232
226
 
233
- # Close the Reader and return resources to the system.
234
- #
235
- # @return [void]
236
- def close
237
- @reader.close
238
- end
227
+ # Return the metadata associated with the database.
228
+ #
229
+ # @return [MaxMind::DB::Metadata]
230
+ def metadata
231
+ @reader.metadata
232
+ end
239
233
 
240
- private
234
+ # Close the Reader and return resources to the system.
235
+ #
236
+ # @return [void]
237
+ def close
238
+ @reader.close
239
+ end
241
240
 
242
- def model_for(model_class, method, type, ip_address)
243
- record, prefix_length = get_record(method, type, ip_address)
241
+ private
244
242
 
245
- record['traits'] = {} if !record.key?('traits')
246
- record['traits']['ip_address'] = ip_address
247
- record['traits']['prefix_length'] = prefix_length
243
+ def model_for(model_class, method, type, ip_address)
244
+ record, prefix_length = get_record(method, type, ip_address)
248
245
 
249
- model_class.new(record, @locales)
250
- end
246
+ record['traits'] = {} if !record.key?('traits')
247
+ record['traits']['ip_address'] = ip_address
248
+ record['traits']['prefix_length'] = prefix_length
251
249
 
252
- def get_record(method, type, ip_address)
253
- if !@type.include?(type)
254
- raise ArgumentError,
255
- "The #{method} method cannot be used with the #{@type} database."
250
+ model_class.new(record, @locales)
256
251
  end
257
252
 
258
- record, prefix_length = @reader.get_with_prefix_length(ip_address)
253
+ def get_record(method, type, ip_address)
254
+ if !@type.include?(type)
255
+ raise ArgumentError,
256
+ "The #{method} method cannot be used with the #{@type} database."
257
+ end
259
258
 
260
- if record.nil?
261
- raise AddressNotFoundError,
262
- "The address #{ip_address} is not in the database."
263
- end
259
+ record, prefix_length = @reader.get_with_prefix_length(ip_address)
264
260
 
265
- [record, prefix_length]
266
- end
261
+ if record.nil?
262
+ raise AddressNotFoundError,
263
+ "The address #{ip_address} is not in the database."
264
+ end
265
+
266
+ [record, prefix_length]
267
+ end
267
268
 
268
- def flat_model_for(model_class, method, type, ip_address)
269
- record, prefix_length = get_record(method, type, ip_address)
269
+ def flat_model_for(model_class, method, type, ip_address)
270
+ record, prefix_length = get_record(method, type, ip_address)
270
271
 
271
- record['ip_address'] = ip_address
272
- record['prefix_length'] = prefix_length
272
+ record['ip_address'] = ip_address
273
+ record['prefix_length'] = prefix_length
273
274
 
274
- model_class.new(record)
275
+ model_class.new(record)
276
+ end
275
277
  end
276
278
  end
277
279
  end