maxmind-geoip2 0.4.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 +28 -0
  3. data/Gemfile +12 -0
  4. data/Gemfile.lock +72 -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 +328 -0
  12. data/lib/maxmind/geoip2/errors.rb +41 -0
  13. data/lib/maxmind/geoip2/model/abstract.rb +31 -0
  14. data/lib/maxmind/geoip2/model/anonymous_ip.rb +67 -0
  15. data/lib/maxmind/geoip2/model/asn.rb +43 -0
  16. data/lib/maxmind/geoip2/model/city.rb +79 -0
  17. data/lib/maxmind/geoip2/model/connection_type.rb +36 -0
  18. data/lib/maxmind/geoip2/model/country.rb +74 -0
  19. data/lib/maxmind/geoip2/model/domain.rb +36 -0
  20. data/lib/maxmind/geoip2/model/enterprise.rb +19 -0
  21. data/lib/maxmind/geoip2/model/insights.rb +18 -0
  22. data/lib/maxmind/geoip2/model/isp.rb +57 -0
  23. data/lib/maxmind/geoip2/reader.rb +279 -0
  24. data/lib/maxmind/geoip2/record/abstract.rb +26 -0
  25. data/lib/maxmind/geoip2/record/city.rb +42 -0
  26. data/lib/maxmind/geoip2/record/continent.rb +41 -0
  27. data/lib/maxmind/geoip2/record/country.rb +58 -0
  28. data/lib/maxmind/geoip2/record/location.rb +77 -0
  29. data/lib/maxmind/geoip2/record/maxmind.rb +21 -0
  30. data/lib/maxmind/geoip2/record/place.rb +32 -0
  31. data/lib/maxmind/geoip2/record/postal.rb +34 -0
  32. data/lib/maxmind/geoip2/record/represented_country.rb +27 -0
  33. data/lib/maxmind/geoip2/record/subdivision.rb +52 -0
  34. data/lib/maxmind/geoip2/record/traits.rb +204 -0
  35. data/maxmind-geoip2.gemspec +26 -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 +130 -0
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MaxMind
4
+ module GeoIP2
5
+ module Record
6
+ # @!visibility private
7
+ class Abstract
8
+ def initialize(record)
9
+ @record = record
10
+ end
11
+
12
+ protected
13
+
14
+ def get(key)
15
+ if @record.nil? || !@record.key?(key)
16
+ return false if key.start_with?('is_')
17
+
18
+ return nil
19
+ end
20
+
21
+ @record[key]
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'maxmind/geoip2/record/place'
4
+
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
23
+
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
31
+
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
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'maxmind/geoip2/record/place'
4
+
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
22
+
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
30
+
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
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'maxmind/geoip2/record/place'
4
+
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
22
+
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
30
+
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
38
+
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
47
+
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
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,77 @@
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 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
22
+
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
30
+
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
39
+
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
48
+
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
57
+
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
65
+
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
75
+ end
76
+ end
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
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'maxmind/geoip2/record/abstract'
4
+
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
15
+
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?
22
+
23
+ @locales.each do |locale|
24
+ return n[locale] if n.key?(locale)
25
+ end
26
+
27
+ nil
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,34 @@
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 for the postal record associated with an IP address.
9
+ #
10
+ # This record is returned by all location services and databases besides
11
+ # Country.
12
+ class Postal < Abstract
13
+ # The postal code of the location. Postal codes are not available for all
14
+ # countries. In some countries, this will only contain part of the postal
15
+ # code. This attribute is returned by all location databases and services
16
+ # besides Country.
17
+ #
18
+ # @return [String, nil]
19
+ def code
20
+ get('code')
21
+ end
22
+
23
+ # A value from 0-100 indicating MaxMind's confidence that the postal code
24
+ # is correct. This attribute is only available from the Insights service
25
+ # and the GeoIP2 Enterprise database.
26
+ #
27
+ # @return [Integer, nil]
28
+ def confidence
29
+ get('confidence')
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'maxmind/geoip2/record/country'
4
+
5
+ module MaxMind
6
+ module GeoIP2
7
+ module Record
8
+ # Contains data for the represented country associated with an IP address.
9
+ #
10
+ # This class contains the country-level data associated with an IP address
11
+ # for the IP's represented country. The represented country is the country
12
+ # represented by something like a military base.
13
+ #
14
+ # See {MaxMind::GeoIP2::Record::Country} for inherited methods.
15
+ class RepresentedCountry < Country
16
+ # A string indicating the type of entity that is representing the country.
17
+ # Currently we only return +military+ but this could expand to include
18
+ # other types in the future.
19
+ #
20
+ # @return [String, nil]
21
+ def type
22
+ get('type')
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'maxmind/geoip2/record/place'
4
+
5
+ module MaxMind
6
+ module GeoIP2
7
+ module Record
8
+ # Contains data for the subdivisions associated with an IP address.
9
+ #
10
+ # This record is returned by all location databases and services besides
11
+ # Country.
12
+ #
13
+ # See {MaxMind::GeoIP2::Record::Place} for inherited methods.
14
+ class Subdivision < Place
15
+ # This is a value from 0-100 indicating MaxMind's confidence that the
16
+ # subdivision is correct. This attribute is only available from the
17
+ # Insights service and the GeoIP2 Enterprise database.
18
+ #
19
+ # @return [Integer, nil]
20
+ def confidence
21
+ get('confidence')
22
+ end
23
+
24
+ # This is a GeoName ID for the subdivision. This attribute is returned by
25
+ # all location databases and services besides Country.
26
+ #
27
+ # @return [Integer, nil]
28
+ def geoname_id
29
+ get('geoname_id')
30
+ end
31
+
32
+ # This is a string up to three characters long contain the subdivision
33
+ # portion of the ISO 3166-2 code. See
34
+ # https://en.wikipedia.org/wiki/ISO_3166-2. This attribute is returned by
35
+ # all location databases and services except Country.
36
+ #
37
+ # @return [String, nil]
38
+ def iso_code
39
+ get('iso_code')
40
+ end
41
+
42
+ # A Hash where the keys are locale codes and the values are names. This attribute is returned by all location services and
43
+ # databases besides country.
44
+ #
45
+ # @return [Hash<String, String>, nil]
46
+ def names
47
+ get('names')
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,204 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ipaddr'
4
+ require 'maxmind/geoip2/record/abstract'
5
+
6
+ module MaxMind
7
+ module GeoIP2
8
+ module Record
9
+ # Contains data for the traits record associated with an IP address.
10
+ #
11
+ # This record is returned by all location services and databases.
12
+ class Traits < Abstract
13
+ # @!visibility private
14
+ def initialize(record)
15
+ super(record)
16
+ if !record.key?('network') && record.key?('ip_address') &&
17
+ record.key?('prefix_length')
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'])
22
+ end
23
+ end
24
+
25
+ # The autonomous system number associated with the IP address. See
26
+ # Wikipedia[https://en.wikipedia.org/wiki/Autonomous_system_(Internet)].
27
+ # This attribute is only available from the City and Insights web service
28
+ # and the GeoIP2 Enterprise database.
29
+ #
30
+ # @return [Integer, nil]
31
+ def autonomous_system_number
32
+ get('autonomous_system_number')
33
+ end
34
+
35
+ # The organization associated with the registered autonomous system number
36
+ # for the IP address. See
37
+ # Wikipedia[https://en.wikipedia.org/wiki/Autonomous_system_(Internet)].
38
+ # This attribute is only available from the City and Insights web service
39
+ # and the GeoIP2 Enterprise database.
40
+ #
41
+ # @return [String, nil]
42
+ def autonomous_system_organization
43
+ get('autonomous_system_organization')
44
+ end
45
+
46
+ # The connection type may take the following values: "Dialup",
47
+ # "Cable/DSL", "Corporate", "Cellular". Additional values may be added in
48
+ # the future. This attribute is only available in the GeoIP2 Enterprise
49
+ # database.
50
+ #
51
+ # @return [String, nil]
52
+ def connection_type
53
+ get('connection_type')
54
+ end
55
+
56
+ # The second level domain associated with the IP address. This will be
57
+ # something like "example.com" or "example.co.uk", not "foo.example.com".
58
+ # This attribute is only available from the City and Insights web service
59
+ # and the GeoIP2 Enterprise database.
60
+ #
61
+ # @return [String, nil]
62
+ def domain
63
+ get('domain')
64
+ end
65
+
66
+ # The IP address that the data in the model is for. If you performed a "me"
67
+ # lookup against the web service, this will be the externally routable IP
68
+ # address for the system the code is running on. If the system is behind a
69
+ # NAT, this may differ from the IP address locally assigned to it. This
70
+ # attribute is returned by all end points.
71
+ #
72
+ # @return [String]
73
+ def ip_address
74
+ get('ip_address')
75
+ end
76
+
77
+ # This is true if the IP address belongs to any sort of anonymous network.
78
+ # This property is only available from GeoIP2 Precision Insights.
79
+ #
80
+ # @return [Boolean]
81
+ def anonymous?
82
+ get('is_anonymous')
83
+ end
84
+
85
+ # This is true if the IP address is registered to an anonymous VPN
86
+ # provider. If a VPN provider does not register subnets under names
87
+ # associated with them, we will likely only flag their IP ranges using the
88
+ # hosting_provider? property. This property is only available from GeoIP2
89
+ # Precision Insights.
90
+ #
91
+ # @return [Boolean]
92
+ def anonymous_vpn?
93
+ get('is_anonymous_vpn')
94
+ end
95
+
96
+ # This is true if the IP address belongs to a hosting or VPN provider (see
97
+ # description of the anonymous_vpn? property). This property is only
98
+ # available from GeoIP2 Precision Insights.
99
+ #
100
+ # @return [Boolean]
101
+ def hosting_provider?
102
+ get('is_hosting_provider')
103
+ end
104
+
105
+ # This attribute is true if MaxMind believes this IP address to be a
106
+ # legitimate proxy, such as an internal VPN used by a corporation. This
107
+ # attribute is only available in the GeoIP2 Enterprise database.
108
+ #
109
+ # @return [Boolean]
110
+ def legitimate_proxy?
111
+ get('is_legitimate_proxy')
112
+ end
113
+
114
+ # This is true if the IP address belongs to a public proxy. This property
115
+ # is only available from GeoIP2 Precision Insights.
116
+ #
117
+ # @return [Boolean]
118
+ def public_proxy?
119
+ get('is_public_proxy')
120
+ end
121
+
122
+ # This is true if the IP address is a Tor exit node. This property is only
123
+ # available from GeoIP2 Precision Insights.
124
+ #
125
+ # @return [Boolean]
126
+ def tor_exit_node?
127
+ get('is_tor_exit_node')
128
+ end
129
+
130
+ # The name of the ISP associated with the IP address. This attribute is
131
+ # only available from the City and Insights web services and the GeoIP2
132
+ # Enterprise database.
133
+ #
134
+ # @return [String, nil]
135
+ def isp
136
+ get('isp')
137
+ end
138
+
139
+ # The network in CIDR notation associated with the record. In particular,
140
+ # this is the largest network where all of the fields besides ip_address
141
+ # have the same value.
142
+ #
143
+ # @return [String]
144
+ def network
145
+ get('network')
146
+ end
147
+
148
+ # The name of the organization associated with the IP address. This
149
+ # attribute is only available from the City and Insights web services and
150
+ # the GeoIP2 Enterprise database.
151
+ #
152
+ # @return [String, nil]
153
+ def organization
154
+ get('organization')
155
+ end
156
+
157
+ # An indicator of how static or dynamic an IP address is. This property is
158
+ # only available from GeoIP2 Precision Insights.
159
+ #
160
+ # @return [Float, nil]
161
+ def static_ip_score
162
+ get('static_ip_score')
163
+ end
164
+
165
+ # The estimated number of users sharing the IP/network during the past 24
166
+ # hours. For IPv4, the count is for the individual IP. For IPv6, the count
167
+ # is for the /64 network. This property is only available from GeoIP2
168
+ # Precision Insights.
169
+ #
170
+ # @return [Integer, nil]
171
+ def user_count
172
+ get('user_count')
173
+ end
174
+
175
+ # The user type associated with the IP address. This can be one of the
176
+ # following values:
177
+ #
178
+ # * business
179
+ # * cafe
180
+ # * cellular
181
+ # * college
182
+ # * content_delivery_network
183
+ # * dialup
184
+ # * government
185
+ # * hosting
186
+ # * library
187
+ # * military
188
+ # * residential
189
+ # * router
190
+ # * school
191
+ # * search_engine_spider
192
+ # * traveler
193
+ #
194
+ # This attribute is only available from the Insights web service and the
195
+ # GeoIP2 Enterprise database.
196
+ #
197
+ # @return [String, nil]
198
+ def user_type
199
+ get('user_type')
200
+ end
201
+ end
202
+ end
203
+ end
204
+ end