internetdata 2.21.0 → 3.0.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 (75) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +23 -6
  3. data/lib/internetdata/api/database_v2_api.rb +1 -1
  4. data/lib/internetdata/api_client.rb +1 -1
  5. data/lib/internetdata/api_error.rb +1 -1
  6. data/lib/internetdata/api_model_base.rb +1 -1
  7. data/lib/internetdata/asn_result.rb +5 -15
  8. data/lib/internetdata/cache.rb +7 -7
  9. data/lib/internetdata/client.rb +90 -49
  10. data/lib/internetdata/configuration.rb +1 -1
  11. data/lib/internetdata/domain_result.rb +5 -15
  12. data/lib/internetdata/errors.rb +21 -1
  13. data/lib/internetdata/middleware/core.rb +55 -3
  14. data/lib/internetdata/models/abuse_contact.rb +1 -1
  15. data/lib/internetdata/models/asn.rb +1 -1
  16. data/lib/internetdata/models/asn_hosted_domains.rb +1 -1
  17. data/lib/internetdata/models/asn_lookup_response.rb +3 -13
  18. data/lib/internetdata/models/asn_prefix.rb +1 -1
  19. data/lib/internetdata/models/batch_lookup_error.rb +1 -1
  20. data/lib/internetdata/models/bit_torrent_detail.rb +1 -1
  21. data/lib/internetdata/models/bot_detail.rb +1 -1
  22. data/lib/internetdata/models/carrier_detail.rb +1 -1
  23. data/lib/internetdata/models/class_detail.rb +1 -1
  24. data/lib/internetdata/models/client_registration.rb +1 -1
  25. data/lib/internetdata/models/cohosted_domains.rb +1 -1
  26. data/lib/internetdata/models/company.rb +1 -1
  27. data/lib/internetdata/models/database.rb +1 -1
  28. data/lib/internetdata/models/database_checksums_response.rb +1 -1
  29. data/lib/internetdata/models/database_format.rb +1 -1
  30. data/lib/internetdata/models/database_list.rb +1 -1
  31. data/lib/internetdata/models/database_metadata.rb +1 -1
  32. data/lib/internetdata/models/database_metadata_column.rb +1 -1
  33. data/lib/internetdata/models/database_version.rb +1 -1
  34. data/lib/internetdata/models/db_checksums.rb +1 -1
  35. data/lib/internetdata/models/device_authorization.rb +1 -1
  36. data/lib/internetdata/models/disposable_detail.rb +1 -1
  37. data/lib/internetdata/models/dns_address_record.rb +1 -1
  38. data/lib/internetdata/models/dns_cname_record.rb +1 -1
  39. data/lib/internetdata/models/dns_mx_record.rb +1 -1
  40. data/lib/internetdata/models/dns_ns_record.rb +1 -1
  41. data/lib/internetdata/models/dns_resolver_detail.rb +1 -1
  42. data/lib/internetdata/models/domain_backlinks.rb +1 -1
  43. data/lib/internetdata/models/domain_dns.rb +1 -1
  44. data/lib/internetdata/models/domain_host.rb +1 -1
  45. data/lib/internetdata/models/domain_ip_range.rb +1 -1
  46. data/lib/internetdata/models/domain_ip_ranges.rb +1 -1
  47. data/lib/internetdata/models/domain_lookup_response.rb +3 -13
  48. data/lib/internetdata/models/domain_redirects.rb +1 -1
  49. data/lib/internetdata/models/domain_web.rb +1 -1
  50. data/lib/internetdata/models/domain_web_links.rb +1 -1
  51. data/lib/internetdata/models/download.rb +1 -1
  52. data/lib/internetdata/models/download_list.rb +1 -1
  53. data/lib/internetdata/models/entitlement.rb +1 -1
  54. data/lib/internetdata/models/entitlement_apikey.rb +1 -1
  55. data/lib/internetdata/models/entitlement_plan.rb +1 -1
  56. data/lib/internetdata/models/entitlement_usage.rb +1 -1
  57. data/lib/internetdata/models/error_envelope.rb +1 -1
  58. data/lib/internetdata/models/hosted_domains.rb +1 -1
  59. data/lib/internetdata/models/location.rb +1 -1
  60. data/lib/internetdata/models/lookup_response.rb +3 -13
  61. data/lib/internetdata/models/oauth_metadata.rb +1 -1
  62. data/lib/internetdata/models/proxy_detail.rb +1 -1
  63. data/lib/internetdata/models/pubproxy_detail.rb +1 -1
  64. data/lib/internetdata/models/rwhois.rb +1 -1
  65. data/lib/internetdata/models/satellite_detail.rb +1 -1
  66. data/lib/internetdata/models/smtp_detail.rb +1 -1
  67. data/lib/internetdata/models/ssh_detail.rb +1 -1
  68. data/lib/internetdata/models/standing.rb +1 -1
  69. data/lib/internetdata/models/token_response.rb +1 -1
  70. data/lib/internetdata/models/user_agent_verdict.rb +1 -1
  71. data/lib/internetdata/models/web_detail.rb +1 -1
  72. data/lib/internetdata/models/whois.rb +1 -1
  73. data/lib/internetdata/result.rb +5 -15
  74. data/lib/internetdata/version.rb +1 -1
  75. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5ffb036f8d9a9d5580bee8eed53d36d64bf5527946dea423df6a348ba926aa9a
4
- data.tar.gz: fb7537f99dbfdfef3f593bafb9caab8fa0556a1be6cc776aca1964609db1d583
3
+ metadata.gz: 126b0136f922a39d089527459ef9a1892c908f6c5cab8d596bcef8ec1dce74fe
4
+ data.tar.gz: 1f7960503b7b8864019b9073df1841f9dec53a5fe7de67c3ae92c0d6b5125537
5
5
  SHA512:
6
- metadata.gz: bc91a8296a714763639de2e1914d98f1cbbb3b6efb9c468d8c1e4cb7acf7691744bac8d7621ffdb42bbbf102764869afeab17188b7d5652fd90793a9001bd5de
7
- data.tar.gz: 9823af087d0151cd057b846ad99dcff7e203b032cf86494f2d25b2e244f9cfde1b197176a78b5fb1c4035ec6ec21467c137d2005e93e8561c12071e34e53d9ea
6
+ metadata.gz: d45135ee843772f0133ae4df875b143f32fb21cc5d4e64a679716ba1bb75b87c6f76cc511f9485703ab854ca349b7ba0a8605afc8ca31caec25e72512a1bfdb1
7
+ data.tar.gz: c9c921c4652811409504d4d881fe88e794d281aef7ab155061d89c34db91bcdb17971c4a9277a9d04e352cfeb0d5865e122f046eee19ea7f263969c48ab54350
data/README.md CHANGED
@@ -23,7 +23,7 @@ Requires Ruby 3.3 or newer, and libcurl at runtime, which the `curl` package bri
23
23
 
24
24
  ## Usage
25
25
 
26
- **No API key needed to try it.** A lookup without one answers the basics: an address's location, network and reverse DNS, and its Tor, relay and anycast flags. The address's company and its abuse contact are not in the basics: they come with a free key's full answers or a paid plan. A free key answers every field on its organization's first lookups each UTC day, then the basics.
26
+ **No API key needed to try it.** A lookup without one answers the basics: an address's location, network and reverse DNS, and its Tor, relay and anycast flags. The address's company and its abuse contact are not in the basics: they come with a free key's full answers or a paid plan. A free key answers every field on its organization's first lookups each UTC day, then a lookup raises `quota_exceeded` with the basics ([below](#when-the-allowance-is-used-up)).
27
27
 
28
28
  ```ruby
29
29
  require 'internetdata'
@@ -38,7 +38,7 @@ result.is_tor # => false
38
38
 
39
39
  ### With an API key
40
40
 
41
- A paid plan answers every field: the VPN, proxy, hosting and CDN flags as well, the detail behind each flag, and an address's city, Whois, RWHOIS and hosted domains. A free key answers every field on its organization's first lookups each UTC day, then the basics. Create a key in the [console](https://app.internetdata.io), then pass it in:
41
+ A paid plan answers every field: the VPN, proxy, hosting and CDN flags as well, the detail behind each flag, and an address's city, Whois, RWHOIS and hosted domains. A free key answers every field on its organization's first lookups each UTC day, then a lookup raises `quota_exceeded` ([below](#when-the-allowance-is-used-up)). Create a key in the [console](https://app.internetdata.io), then pass it in:
42
42
 
43
43
  ```ruby
44
44
  client = InternetData::Client.new(api_key: ENV['INTERNETDATA_API_KEY'])
@@ -49,6 +49,22 @@ result.vpn['provider_name'] # => "Mullvad", on a paid plan
49
49
  result.location['city'] # => "Vienna", on a paid plan
50
50
  ```
51
51
 
52
+ ### When the allowance is used up
53
+
54
+ A key answers every field until its organization's lookups with every field are used up: a free key's for the UTC day, a paid key's quota and then its overage cap. Past that the API keeps answering, with the basics and `cap_reached: true`, so an app reading the answer would run on without the fields it was built on and nobody would notice. From 3.0.0 that lookup raises instead: an `InternetData::Error` of kind `:quota_exceeded`, status 200, never retried, carrying the answer as `result`. No client option turns it off, and catching it is how you carry on:
55
+
56
+ ```ruby
57
+ begin
58
+ result = client.lookup('146.70.116.130')
59
+ rescue InternetData::Error => e
60
+ raise unless e.kind == :quota_exceeded && e.result
61
+
62
+ result = e.result # the basics, with cap_reached true
63
+ end
64
+ ```
65
+
66
+ `lookup`, `my_ip` with a key, `lookup_asn` and `lookup_domain` raise it, and so does an answer from the cache. A batch entry that is capped carries it as its value, as a failing address does, and the batch still returns. A private address and an answer to no key never raise: neither carries the flag, and a day of keyless lookups spent is a `:quota_exceeded` with status 429 and no `result`. A capped answer is cached for at most a minute, and each hit raises again.
67
+
52
68
  ### AS and domain lookups
53
69
 
54
70
  ```ruby
@@ -115,7 +131,7 @@ results.each do |ip, result|
115
131
  end
116
132
  ```
117
133
 
118
- Results are keyed by address, in the order you first listed each one, so duplicates in your list collapse into a single entry and one address failing never loses the rest: it carries its error as its value, with the status the API would have given that address on its own.
134
+ Results are keyed by address, in the order you first listed each one, so duplicates in your list collapse into a single entry and one address failing never loses the rest: it carries its error as its value, with the status the API would have given that address on its own. An address whose answer is capped carries `:quota_exceeded`, and its `result` is that answer.
119
135
 
120
136
  How many chunks are in flight at once, how many times a failed chunk is retried, and how long each chunk's request may take are configurable per call:
121
137
 
@@ -134,6 +150,8 @@ result = client.lookup('146.70.116.130') # API request
134
150
  result2 = client.lookup('146.70.116.130') # no API request, result was cached
135
151
  ```
136
152
 
153
+ A capped answer is held for a minute at most, or your TTL when that is shorter, and each lookup of it raises `:quota_exceeded` again without a request.
154
+
137
155
  You can change the default cache variables (max size, TTL in seconds) on initialization, or even disable it:
138
156
 
139
157
  ```ruby
@@ -317,15 +335,14 @@ Each redirect URI must be https on a public host name, or http on a loopback add
317
335
 
318
336
  ### Absent is not false
319
337
 
320
- The basics leave out the VPN, proxy, hosting, CDN, bot, carrier, satellite, SSH, SMTP, web, DNS resolver and BitTorrent flags and their detail (`bot` names the bot behind `is_bot`), the network's type, an address's company and its abuse contact, an address's city and the rest of its paid location, its Whois, RWHOIS and hosted domains, and the paid members inside each shared object, such as `location['city']`. A free key's answer carries `trial_expired`, which keeps that name: true once its organization's full answers for the UTC day are used, when the answer has the basics. A paid key's carries `cap_reached`, true once its organization's overage cap is reached, when the answer has the basics and isn't billed. A field is present when your plan includes it and `nil` when it does not. `nil` means "not in your plan"; `false` means "we checked, and no".
338
+ The basics leave out the VPN, proxy, hosting, CDN, bot, carrier, satellite, SSH, SMTP, web, DNS resolver and BitTorrent flags and their detail (`bot` names the bot behind `is_bot`), the network's type, an address's company and its abuse contact, an address's city and the rest of its paid location, its Whois, RWHOIS and hosted domains, and the paid members inside each shared object, such as `location['city']`. Every answer to a key carries `cap_reached`: false while the answer has every field, true once the organization's lookups with every field are used up, and then the call raises (above). A field is present when your plan includes it and `nil` when it does not, the flags, the network's type, `company` and `abuse` included, never `false`, `""` or `{}`. `nil` means "not in your plan"; `false` means "we checked, and no".
321
339
 
322
340
  ```ruby
323
341
  result.vpn? # false when absent, for when you only want the flag
324
342
  result.included?(:is_vpn) # false on the basics: not in your plan
325
343
  result.company # nil on the basics, and so is result.abuse
326
344
  result.location.key?('city') # false on the basics
327
- result.trial_expired == true # a free key's full answers for the day are used
328
- result.cap_reached == true # a paid key is past its overage cap
345
+ result.cap_reached # false on an answer with every field, nil without a key
329
346
  ```
330
347
 
331
348
  ## Other Libraries
@@ -3,7 +3,7 @@
3
3
 
4
4
  #The InternetData API: look up what every database InternetData publishes says about an IP address, an AS or a domain, download the databases your organization is licensed for, and manage the account behind them. See https://docs.internetdata.io for guides and https://github.com/internetdata for the official client libraries.
5
5
 
6
- The version of the OpenAPI document: 2026.11.28
6
+ The version of the OpenAPI document: 2026.11.29
7
7
  Contact: support@internetdata.io
8
8
  Generated by: https://openapi-generator.tech
9
9
  Generator version: 7.25.0
@@ -3,7 +3,7 @@
3
3
 
4
4
  #The InternetData API: look up what every database InternetData publishes says about an IP address, an AS or a domain, download the databases your organization is licensed for, and manage the account behind them. See https://docs.internetdata.io for guides and https://github.com/internetdata for the official client libraries.
5
5
 
6
- The version of the OpenAPI document: 2026.11.28
6
+ The version of the OpenAPI document: 2026.11.29
7
7
  Contact: support@internetdata.io
8
8
  Generated by: https://openapi-generator.tech
9
9
  Generator version: 7.25.0
@@ -3,7 +3,7 @@
3
3
 
4
4
  #The InternetData API: look up what every database InternetData publishes says about an IP address, an AS or a domain, download the databases your organization is licensed for, and manage the account behind them. See https://docs.internetdata.io for guides and https://github.com/internetdata for the official client libraries.
5
5
 
6
- The version of the OpenAPI document: 2026.11.28
6
+ The version of the OpenAPI document: 2026.11.29
7
7
  Contact: support@internetdata.io
8
8
  Generated by: https://openapi-generator.tech
9
9
  Generator version: 7.25.0
@@ -3,7 +3,7 @@
3
3
 
4
4
  #The InternetData API: look up what every database InternetData publishes says about an IP address, an AS or a domain, download the databases your organization is licensed for, and manage the account behind them. See https://docs.internetdata.io for guides and https://github.com/internetdata for the official client libraries.
5
5
 
6
- The version of the OpenAPI document: 2026.11.28
6
+ The version of the OpenAPI document: 2026.11.29
7
7
  Contact: support@internetdata.io
8
8
  Generated by: https://openapi-generator.tech
9
9
  Generator version: 7.25.0
@@ -153,21 +153,11 @@ module InternetData
153
153
  @raw['hosted_domains']
154
154
  end
155
155
 
156
- # On answers to a key on the free plan only: false while its organization's
157
- # lookups of the UTC day answer every field, true once those are used and
158
- # the answer has the basics; nil on any other.
159
- def trial_expired
160
- @raw['trial_expired']
161
- end
162
-
163
- # `trial_expired` with an absent member read as false.
164
- def trial_expired?
165
- @raw['trial_expired'] == true
166
- end
167
-
168
- # On answers to a key on a paid plan only: false while the answer has every
169
- # field, true once its organization's overage cap is reached and the answer
170
- # has the basics, which is not billed; nil on any other.
156
+ # On every answer to a key: false while the answer has every field, true
157
+ # once its organization's lookups with every field are used up (a free key's
158
+ # for the UTC day, a paid key's quota or overage cap) and the answer has the
159
+ # basics; nil on an answer to no key. A true one is never returned: the call
160
+ # raises `:quota_exceeded` carrying it as {Error#result}.
171
161
  def cap_reached
172
162
  @raw['cap_reached']
173
163
  end
@@ -10,12 +10,13 @@ module InternetData
10
10
  # serve one of them the other's shape. Errors are never stored and bogons
11
11
  # never reach it.
12
12
  #
13
- # A reduced answer, one past a free key's full answers for the day or a
14
- # reached cap's, ends with the allowance's window (on the free plan, the UTC
15
- # day) or a raised cap, so it stays a minute at most rather than the TTL. It
16
- # is still cached, or a busy process would send every repeat to the API.
13
+ # A capped answer, one whose `cap_reached` is true (past a free key's full
14
+ # answers for the day, or a paid key's quota or overage cap), ends with the
15
+ # allowance's window (on the free plan, the UTC day) or a raised cap, so it
16
+ # stays a minute at most rather than the TTL. It is still cached, or a busy
17
+ # process would send every repeat to the API; the client raises on each hit.
17
18
  class Cache
18
- # The longest a reduced answer is held, in seconds.
19
+ # The longest a capped answer is held, in seconds.
19
20
  REDUCED_TTL = 60
20
21
 
21
22
  def initialize(max_size:, ttl:)
@@ -54,8 +55,7 @@ module InternetData
54
55
  private
55
56
 
56
57
  def life_of(value)
57
- reduced = value.respond_to?(:trial_expired?) && (value.trial_expired? || value.cap_reached?)
58
- reduced ? [@ttl, REDUCED_TTL].min : @ttl
58
+ value.respond_to?(:cap_reached?) && value.cap_reached? ? [@ttl, REDUCED_TTL].min : @ttl
59
59
  end
60
60
 
61
61
  def now
@@ -26,9 +26,11 @@ module InternetData
26
26
  # @param api_key [String, nil] without one a lookup answers the basics: an
27
27
  # address's location, network, reverse DNS and its Tor, relay and anycast
28
28
  # flags, an AS's registration and a domain's. A free key answers every
29
- # field on its organization's first lookups each UTC day, then the basics.
30
- # A batch needs a key on a paid plan, and the databases a key carrying the
31
- # `db.download` scope. It is trimmed, and a key of blanks alone is no key.
29
+ # field on its organization's first lookups each UTC day, then raises
30
+ # `:quota_exceeded` carrying the basics as {Error#result}, as a paid key
31
+ # past its quota or overage cap does. A batch needs a key on a paid plan,
32
+ # and the databases a key carrying the `db.download` scope. It is trimmed,
33
+ # and a key of blanks alone is no key.
32
34
  # @param base_url [String] the API; {#oauth} signs in at its apex, `api.`
33
35
  # dropped and `api-<env>.` becoming `<env>.`, and at any other URL as given.
34
36
  # @param cache [Boolean] pass false to disable caching.
@@ -86,6 +88,12 @@ module InternetData
86
88
  # A bogon is answered locally and never reaches the network. Everything else
87
89
  # is served, then cached for this instance.
88
90
  #
91
+ # An answer with `cap_reached` true, the basics alone because the
92
+ # organization's lookups with every field are used up, is raised as
93
+ # `:quota_exceeded`, status 200, never retried, fresh or from the cache (which
94
+ # holds it a minute at most). Catch it to carry on: {Error#result} is the
95
+ # answer, with the basics. No option turns the raise off.
96
+ #
89
97
  # @param retries [Integer, nil] extra attempts, for THIS call only.
90
98
  # @param timeout [Numeric, nil] seconds each attempt may take, for THIS call only.
91
99
  # @param user_agent [String, nil] the `User-Agent` of the request you are
@@ -96,44 +104,11 @@ module InternetData
96
104
  # agent's. A bogon is still answered locally, with no `user_agent`.
97
105
  # @raise [Error] `:bad_request`, before any request, for anything but an IP
98
106
  # address: {#lookup_asn} and {#lookup_domain} look an AS and a domain up.
107
+ # @raise [Error] `:quota_exceeded` for a capped answer, whose `result` is it.
99
108
  # @raise [ArgumentError] for a `user_agent` that is not a String.
100
109
  # @return [Result]
101
110
  def lookup(ip, retries: nil, timeout: nil, user_agent: nil)
102
- # Here as well as in the transport: a bogon or a cached answer returns before any request.
103
- Transport.checked_timeout(timeout) unless timeout.nil?
104
- Retries.checked_count(retries) unless retries.nil?
105
- agent = Transport.checked_user_agent(user_agent)
106
- # Judged, cached and sent as the IPv4 address it carries, if it is mapped.
107
- ip = Bogon.unmapped(ip)
108
- # The API answers an AS or a domain at this path too, as an answer this call
109
- # cannot read, so one is refused unsent rather than retried as unreadable.
110
- unless Bogon.address?(ip)
111
- raise Error.new(:bad_request, "'#{ip}' is not an IP address: look up an AS with lookup_asn, " \
112
- 'and a domain with lookup_domain')
113
- end
114
- return Bogon.result(ip) if Bogon.bogon?(ip)
115
-
116
- key = key_of(ip, agent)
117
- loop do
118
- hit = @cache&.get(key)
119
- return hit unless hit.nil?
120
-
121
- flight, leader = board(key)
122
- return fetch(ip, agent, retries, timeout) if flight.nil?
123
-
124
- if leader
125
- # Read once more now the address is boarded: a leader that landed
126
- # between the miss above and boarding has already cached its answer.
127
- hit = @cache.get(key)
128
- return unboard(key, flight, :served, hit) unless hit.nil?
129
-
130
- return lead(key, flight) { fetch(ip, agent, retries, timeout) }
131
- end
132
- state, value = flight.wait
133
- return value if state == :served
134
- raise value if state == :failed
135
- # :abandoned - its leader never finished, so this caller asks again.
136
- end
111
+ uncapped(answer_for(ip, retries, timeout, user_agent))
137
112
  end
138
113
 
139
114
  # What every database says about one AS. Sent as given, so `9009` and
@@ -142,12 +117,15 @@ module InternetData
142
117
  #
143
118
  # @param retries [Integer, nil] extra attempts, for THIS call only.
144
119
  # @param timeout [Numeric, nil] seconds each attempt may take, for THIS call only.
120
+ # @raise [Error] `:quota_exceeded` for a capped answer, as {#lookup} raises it.
145
121
  # @return [AsnResult]
146
122
  def lookup_asn(asn, retries: nil, timeout: nil)
147
123
  Retries.checked_count(retries) unless retries.nil?
148
- Retries.with_retries(retries || @retries) do
149
- Transport.asn_result(@transport.asn_request(asn, timeout: timeout).run)
150
- end
124
+ uncapped(
125
+ Retries.with_retries(retries || @retries) do
126
+ Transport.asn_result(@transport.asn_request(asn, timeout: timeout).run)
127
+ end,
128
+ )
151
129
  end
152
130
 
153
131
  # What every database says about one domain. Sent as given: the API reads it
@@ -156,12 +134,15 @@ module InternetData
156
134
  #
157
135
  # @param retries [Integer, nil] extra attempts, for THIS call only.
158
136
  # @param timeout [Numeric, nil] seconds each attempt may take, for THIS call only.
137
+ # @raise [Error] `:quota_exceeded` for a capped answer, as {#lookup} raises it.
159
138
  # @return [DomainResult]
160
139
  def lookup_domain(domain, retries: nil, timeout: nil)
161
140
  Retries.checked_count(retries) unless retries.nil?
162
- Retries.with_retries(retries || @retries) do
163
- Transport.domain_result(@transport.domain_request(domain, timeout: timeout).run)
164
- end
141
+ uncapped(
142
+ Retries.with_retries(retries || @retries) do
143
+ Transport.domain_result(@transport.domain_request(domain, timeout: timeout).run)
144
+ end,
145
+ )
165
146
  end
166
147
 
167
148
  # What every database says about the address this client is calling from.
@@ -176,13 +157,16 @@ module InternetData
176
157
  # otherwise be told where it used to be.
177
158
  #
178
159
  # @param user_agent [String, nil] as {#lookup} takes it.
160
+ # @raise [Error] `:quota_exceeded` for a capped answer, as {#lookup} raises it.
179
161
  # @return [Result]
180
162
  def my_ip(retries: nil, timeout: nil, user_agent: nil)
181
163
  Retries.checked_count(retries) unless retries.nil?
182
164
  agent = Transport.checked_user_agent(user_agent)
183
- Retries.with_retries(retries || @retries) do
184
- Transport.lookup_result(@transport.myip_request(timeout: timeout, user_agent: agent).run)
185
- end
165
+ uncapped(
166
+ Retries.with_retries(retries || @retries) do
167
+ Transport.lookup_result(@transport.myip_request(timeout: timeout, user_agent: agent).run)
168
+ end,
169
+ )
186
170
  end
187
171
 
188
172
  # What this client's key is entitled to, and how much of it has been used.
@@ -225,7 +209,10 @@ module InternetData
225
209
  # endpoint needs a paid plan: without a key every address in a request of
226
210
  # two or more carries an `:unauthorized` error, and a free-plan key's
227
211
  # request of two or more a `:bad_request` (HTTP 402). A request of one
228
- # address is answered as its lookup would be.
212
+ # address is answered as its lookup would be. An address whose answer has
213
+ # `cap_reached` true carries `:quota_exceeded`, with that answer as its
214
+ # {Error#result}, as {#lookup} raises it, fresh or from the cache; the batch
215
+ # still returns.
229
216
  #
230
217
  # @param concurrency [Integer, nil] chunks in flight, for THIS batch only.
231
218
  # Anything but an Integer from 1 is refused as `:bad_request` before any
@@ -291,11 +278,59 @@ module InternetData
291
278
 
292
279
  # Reinstated in input order: a hydra settles in completion order, and a
293
280
  # caller iterating the hash should see what they passed in.
294
- asked.to_h { |ip| [ip, answers[Bogon.unmapped(ip)]] }
281
+ asked.to_h { |ip| [ip, entry_of(answers[Bogon.unmapped(ip)])] }
295
282
  end
296
283
 
297
284
  private
298
285
 
286
+ # A lookup's answer, or the `:quota_exceeded` it is when `cap_reached` is true,
287
+ # holding it. Raised outside the retries: a capped answer is a 200 and is
288
+ # never asked for again.
289
+ def uncapped(answer)
290
+ raise Error.capped(answer) if answer.cap_reached?
291
+
292
+ answer
293
+ end
294
+
295
+ # The address lookup before the raise: a cached answer, one a concurrent call
296
+ # fetched, or one fetched here, capped or not.
297
+ def answer_for(ip, retries, timeout, user_agent)
298
+ Transport.checked_timeout(timeout) unless timeout.nil?
299
+ Retries.checked_count(retries) unless retries.nil?
300
+ agent = Transport.checked_user_agent(user_agent)
301
+ # Judged, cached and sent as the IPv4 address it carries, if it is mapped.
302
+ ip = Bogon.unmapped(ip)
303
+ # The API answers an AS or a domain at this path too, as an answer this call
304
+ # cannot read, so one is refused unsent rather than retried as unreadable.
305
+ unless Bogon.address?(ip)
306
+ raise Error.new(:bad_request, "'#{ip}' is not an IP address: look up an AS with lookup_asn, " \
307
+ 'and a domain with lookup_domain')
308
+ end
309
+ return Bogon.result(ip) if Bogon.bogon?(ip)
310
+
311
+ key = key_of(ip, agent)
312
+ loop do
313
+ hit = @cache&.get(key)
314
+ return hit unless hit.nil?
315
+
316
+ flight, leader = board(key)
317
+ return fetch(ip, agent, retries, timeout) if flight.nil?
318
+
319
+ if leader
320
+ # Read once more now the address is boarded: a leader that landed
321
+ # between the miss above and boarding has already cached its answer.
322
+ hit = @cache.get(key)
323
+ return unboard(key, flight, :served, hit) unless hit.nil?
324
+
325
+ return lead(key, flight) { fetch(ip, agent, retries, timeout) }
326
+ end
327
+ state, value = flight.wait
328
+ return value if state == :served
329
+ raise value if state == :failed
330
+ # :abandoned - its leader never finished, so this caller asks again.
331
+ end
332
+ end
333
+
299
334
  # Whether `value` can be a batch's concurrency: chunks in flight are counted
300
335
  # whole, and a hydra allowed fewer than one runs nothing.
301
336
  def concurrency?(value)
@@ -431,6 +466,12 @@ module InternetData
431
466
  nil
432
467
  end
433
468
 
469
+ # A batch entry as the caller gets it: a capped answer is the error a lookup of
470
+ # it raises, one per address, and any other answer or error as it is.
471
+ def entry_of(answer)
472
+ answer.is_a?(Result) && answer.cap_reached? ? Error.capped(answer) : answer
473
+ end
474
+
434
475
  # What a batch takes for an address another call was fetching: that call's
435
476
  # answer or error, or, when its leader never finished, its own lookup.
436
477
  def joined_answer(ip, agent, flight, retries, timeout)
@@ -3,7 +3,7 @@
3
3
 
4
4
  #The InternetData API: look up what every database InternetData publishes says about an IP address, an AS or a domain, download the databases your organization is licensed for, and manage the account behind them. See https://docs.internetdata.io for guides and https://github.com/internetdata for the official client libraries.
5
5
 
6
- The version of the OpenAPI document: 2026.11.28
6
+ The version of the OpenAPI document: 2026.11.29
7
7
  Contact: support@internetdata.io
8
8
  Generated by: https://openapi-generator.tech
9
9
  Generator version: 7.25.0
@@ -143,21 +143,11 @@ module InternetData
143
143
  @raw['backlinks']
144
144
  end
145
145
 
146
- # On answers to a key on the free plan only: false while its organization's
147
- # lookups of the UTC day answer every field, true once those are used and
148
- # the answer has the basics; nil on any other.
149
- def trial_expired
150
- @raw['trial_expired']
151
- end
152
-
153
- # `trial_expired` with an absent member read as false.
154
- def trial_expired?
155
- @raw['trial_expired'] == true
156
- end
157
-
158
- # On answers to a key on a paid plan only: false while the answer has every
159
- # field, true once its organization's overage cap is reached and the answer
160
- # has the basics, which is not billed; nil on any other.
146
+ # On every answer to a key: false while the answer has every field, true
147
+ # once its organization's lookups with every field are used up (a free key's
148
+ # for the UTC day, a paid key's quota or overage cap) and the answer has the
149
+ # basics; nil on an answer to no key. A true one is never returned: the call
150
+ # raises `:quota_exceeded` carrying it as {Error#result}.
161
151
  def cap_reached
162
152
  @raw['cap_reached']
163
153
  end
@@ -11,6 +11,11 @@ module InternetData
11
11
  # `Retry-After`; retrying works. A spent quota carries no such header and
12
12
  # retrying will not help until the window rolls over or the limit is raised.
13
13
  # The header is the only thing that distinguishes them.
14
+ #
15
+ # A third `:quota_exceeded` is an answer rather than a failure: a 200 whose
16
+ # `cap_reached` is true, carrying the basics because the organization's lookups
17
+ # with every field are used up (a free key's for the UTC day, a paid key's quota
18
+ # or overage cap). {Error#result} holds that answer; its `status` is 200.
14
19
  class Error < StandardError
15
20
  KINDS = %i[
16
21
  bad_request unauthorized forbidden rate_limited quota_exceeded server_error network
@@ -25,12 +30,27 @@ module InternetData
25
30
  # message is the API's own words.
26
31
  attr_reader :kind, :status, :rc, :retry_after_seconds
27
32
 
28
- def initialize(kind, message, status: nil, rc: nil, retry_after_seconds: nil)
33
+ # The answer a `:quota_exceeded` that carries one holds, a {Result},
34
+ # {AsnResult} or {DomainResult} as the call returns, so a rescue still has
35
+ # the basics. nil on every other error.
36
+ attr_reader :result
37
+
38
+ # What a capped answer says, whatever the plan and whatever the figure.
39
+ CAPPED_MESSAGE = "this organization's lookups with every field are used up; this answer has the basics"
40
+
41
+ def initialize(kind, message, status: nil, rc: nil, retry_after_seconds: nil, result: nil)
29
42
  super(message)
30
43
  @kind = kind
31
44
  @status = status
32
45
  @rc = rc
33
46
  @retry_after_seconds = retry_after_seconds
47
+ @result = result
48
+ end
49
+
50
+ # The error a capped answer is: `:quota_exceeded`, status 200, never retried,
51
+ # holding `result`.
52
+ def self.capped(result)
53
+ new(:quota_exceeded, CAPPED_MESSAGE, status: 200, result: result)
34
54
  end
35
55
 
36
56
  # Whether retrying this exact request could succeed.
@@ -42,6 +42,33 @@ module InternetData
42
42
  AGENTS_MAX_ADDRESSES = 10_000
43
43
  private_constant :AGENTS_TTL, :AGENTS_MAX_ADDRESSES
44
44
 
45
+ # The shortest gap, in seconds, between two errors logged for capped lookups
46
+ # by one middleware.
47
+ CAPPED_LOG_INTERVAL = 60
48
+
49
+ # Lets one event through per interval, on the monotonic clock, from any thread.
50
+ # @api private
51
+ class Throttle
52
+ def initialize(interval, clock: -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) })
53
+ @interval = interval
54
+ @clock = clock
55
+ @last = nil
56
+ @lock = Mutex.new
57
+ end
58
+
59
+ # Whether this event is the first in the interval that began at the last
60
+ # one that passed.
61
+ def pass?
62
+ now = @clock.call
63
+ @lock.synchronize do
64
+ return false if @last && now - @last < @interval
65
+
66
+ @last = now
67
+ true
68
+ end
69
+ end
70
+ end
71
+
45
72
  # Whether a `User-Agent` names a crawler, the only kind of agent whose verdict
46
73
  # can be `verified` or `spoofed`. It errs toward a match, which costs a lookup.
47
74
  # Case is folded for A to Z alone, as the corpus's terms are written.
@@ -71,6 +98,14 @@ module InternetData
71
98
  # `bot.verified` is true. A keyless client, or a free key past its full
72
99
  # answers for the day, never gets a verdict, so nothing is let through
73
100
  # there, and that is warned of once under `on_missing_field`.
101
+ # @option options [Boolean] :fail_closed (false) refuse a request whose
102
+ # lookup failed, a capped answer included, rather than let it through.
103
+ # @option options [#call] :on_error receives the message of an error the
104
+ # middleware logs: a lookup that raised `quota_exceeded` with its answer,
105
+ # because the organization's lookups with every field are used up, at most
106
+ # once a minute per middleware however many requests fail that way. The
107
+ # request fails open with nothing attached, or is refused under
108
+ # `fail_closed`. Written to stderr when none is given.
74
109
  def initialize(default_ip_selector, user_agent_of = DEFAULT_USER_AGENT, **options)
75
110
  Condition.validate!(options[:block_condition])
76
111
  @condition = options[:block_condition]
@@ -82,6 +117,8 @@ module InternetData
82
117
  @on_missing_field = options.fetch(:on_missing_field, :warn)
83
118
  @skip = options[:skip]
84
119
  @on_warn = options[:on_warn]
120
+ @on_error = options[:on_error]
121
+ @capped_log = Throttle.new(CAPPED_LOG_INTERVAL)
85
122
  @retries = options.fetch(:retries, DEFAULT_RETRIES)
86
123
  Retries.checked_count(@retries) unless @retries.nil?
87
124
  @warned = {}
@@ -100,9 +137,10 @@ module InternetData
100
137
  # Classify one request. Answers nil when `skip` claimed it.
101
138
  #
102
139
  # A failed LOOKUP is not raised: it lands on `Lookup#error` and the
103
- # request is let through. What CAN raise is a misconfiguration - a
104
- # condition naming a member the plan does not serve, with
105
- # `on_missing_field: :raise`.
140
+ # request is let through. A capped answer is a failed lookup, `quota_exceeded`
141
+ # with the basics as `error.result`: nothing is attached, and it is logged.
142
+ # What CAN raise is a misconfiguration - a condition naming a member the plan
143
+ # does not serve, with `on_missing_field: :raise`.
106
144
  def evaluate(request)
107
145
  return nil if @skip&.call(request)
108
146
 
@@ -125,6 +163,7 @@ module InternetData
125
163
  begin
126
164
  result = @client.lookup(ip, retries: @retries, **(agent ? { user_agent: agent } : {}))
127
165
  rescue InternetData::Error => e
166
+ log_capped(e)
128
167
  return Lookup.new(blocked: @fail_closed, ip: ip, error: e)
129
168
  end
130
169
  decide(ip, result, claim)
@@ -132,6 +171,19 @@ module InternetData
132
171
 
133
172
  private
134
173
 
174
+ # A capped answer raised `quota_exceeded` with its basics: every lookup now
175
+ # fails until the allowance is raised or its window ends, so it is an error
176
+ # worth a line, once a minute and never one per request.
177
+ def log_capped(error)
178
+ return unless error.kind == :quota_exceeded && !error.result.nil?
179
+ return unless @capped_log.pass?
180
+
181
+ message = "lookups fail with quota_exceeded: #{error.message}. Requests " \
182
+ "#{@fail_closed ? 'are refused' : 'pass with no answer attached'} until the allowance " \
183
+ 'is raised or its window ends.'
184
+ @on_error ? @on_error.call(message) : Kernel.warn("[internetdata] error: #{message}")
185
+ end
186
+
135
187
  def unresolved
136
188
  warn_once(
137
189
  'could not resolve a client address from this request; pass an ip_selector that ' \
@@ -3,7 +3,7 @@
3
3
 
4
4
  #The InternetData API: look up what every database InternetData publishes says about an IP address, an AS or a domain, download the databases your organization is licensed for, and manage the account behind them. See https://docs.internetdata.io for guides and https://github.com/internetdata for the official client libraries.
5
5
 
6
- The version of the OpenAPI document: 2026.11.28
6
+ The version of the OpenAPI document: 2026.11.29
7
7
  Contact: support@internetdata.io
8
8
  Generated by: https://openapi-generator.tech
9
9
  Generator version: 7.25.0
@@ -3,7 +3,7 @@
3
3
 
4
4
  #The InternetData API: look up what every database InternetData publishes says about an IP address, an AS or a domain, download the databases your organization is licensed for, and manage the account behind them. See https://docs.internetdata.io for guides and https://github.com/internetdata for the official client libraries.
5
5
 
6
- The version of the OpenAPI document: 2026.11.28
6
+ The version of the OpenAPI document: 2026.11.29
7
7
  Contact: support@internetdata.io
8
8
  Generated by: https://openapi-generator.tech
9
9
  Generator version: 7.25.0
@@ -3,7 +3,7 @@
3
3
 
4
4
  #The InternetData API: look up what every database InternetData publishes says about an IP address, an AS or a domain, download the databases your organization is licensed for, and manage the account behind them. See https://docs.internetdata.io for guides and https://github.com/internetdata for the official client libraries.
5
5
 
6
- The version of the OpenAPI document: 2026.11.28
6
+ The version of the OpenAPI document: 2026.11.29
7
7
  Contact: support@internetdata.io
8
8
  Generated by: https://openapi-generator.tech
9
9
  Generator version: 7.25.0