ipgeolocation_sdk 2.0.1 → 2.0.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f60b9f97f28aba01519af07108337e42f2038fe5788a4da3bdd00b8000d397d1
4
- data.tar.gz: 22de7283e595d32c77ff4380a1cd016fb04470e92fab36b625e9d7843d9b25c0
3
+ metadata.gz: 8d423893c8a9578520671ea730d24c1a80716752647682c9a6ad998494c9a517
4
+ data.tar.gz: 219631c7ed033d981c66b905b9bbb729b5cce38d47f732a77b94a155d1b3026c
5
5
  SHA512:
6
- metadata.gz: aa0d558ed904b8f1664b88f4d4e5fb5160c2d1190adeb0371101cd81350e5a8e6d22bdd6c48548de210c5864a3d7aac13dd75c113e519d5bf7b6ed769ae3b410
7
- data.tar.gz: ef5a76d5e37060f1e756cd2dcf3d92ac0c7a62ae16fe0cdd7b21656bf44f3e1ce44ddbef9855c1f1997efe9c087fc5f4a44864f5e600f133248ef1f2db63961a
6
+ metadata.gz: e2e3c2609a5fed045b6b8181f56803e3691fc3304c09722b605d06956f9b3eddea0a9c3a22fb7a93a874b90b230c1c6b7b767e102e8e14773826193cf2a56e36
7
+ data.tar.gz: 75bee2d56d4067c2cf69132e3c7f35be16514db513b2fb74039671c1beabeeee3c71f3d102ed7de69b4aac0983b19e17f62e6651a939dd95cf4be916a8487bb6
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changelog
2
2
 
3
+ ## 2.0.2
4
+
5
+ - Hide API keys in request errors and typed bulk error messages.
6
+ - Preserve HTTP error types and status codes when error responses are incomplete or invalid.
7
+ - Add Arabic (`ar`) for single and bulk lookups.
8
+
9
+ Existing public methods and Ruby 2.7+ support remain unchanged.
10
+
3
11
  ## 2.0.1
4
12
 
5
13
  - Mask API keys in configuration serialization and client/config inspection.
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Official Ruby SDK for the IPGeolocation.io IP Geolocation API.
4
4
 
5
- Look up IPv4, IPv6, and domains with `/v3/ipgeo` and `/v3/ipgeo-bulk`. Get geolocation, company, ASN, timezone, network, hostname, abuse, user-agent, and security data from one API.
5
+ Look up IPv4, IPv6, and domains with `/v3/ipgeo` and `/v3/ipgeo-bulk`. Get geolocation, company, ASN, time zone, network, hostname, abuse, user-agent, and security data from one API.
6
6
 
7
7
  - Ruby 2.7+
8
8
  - Sync client built on `Net::HTTP`
@@ -85,8 +85,8 @@ You can also pass `LookupIpGeolocationRequest` and `BulkLookupIpGeolocationReque
85
85
  | Module | `IpgeolocationSdk` |
86
86
  | Supported Endpoints | `/v3/ipgeo`, `/v3/ipgeo-bulk` |
87
87
  | Supported Inputs | IPv4, IPv6, domain |
88
- | Main Data Returned | Geolocation, company, ASN, timezone, network, hostname, abuse, user-agent, currency, security |
89
- | Authentication | API key, request-origin auth for `/v3/ipgeo` only |
88
+ | Main Data Returned | Geolocation, company, ASN, time zone, network, hostname, abuse, user-agent, currency, security |
89
+ | Authentication | API key |
90
90
  | Response Formats | Structured JSON, raw JSON, raw XML |
91
91
  | Bulk Limit | Up to 50,000 IPs or domains per request |
92
92
  | Transport | `Net::HTTP` |
@@ -101,7 +101,7 @@ To use most SDK features, create or access your IPGeolocation.io account and cop
101
101
  4. Open your dashboard: <https://app.ipgeolocation.io/dashboard>
102
102
  5. Copy an API key from the `API Keys` section
103
103
 
104
- For server-side code, keep the API key in an environment variable or secret manager. For browser-based single lookups on paid plans, use request-origin auth instead of exposing an API key in frontend code.
104
+ Keep the API key in an environment variable or secret manager.
105
105
 
106
106
  ## Authentication
107
107
 
@@ -113,21 +113,7 @@ client = IpgeolocationSdk::IpGeolocationClient.new(
113
113
  )
114
114
  ```
115
115
 
116
- ### Request-Origin Auth
117
-
118
- ```ruby
119
- client = IpgeolocationSdk::IpGeolocationClient.new(
120
- request_origin: "https://app.example.com"
121
- )
122
- ```
123
-
124
- `request_origin` must be an absolute `http` or `https` origin with no path, query string, fragment, or userinfo.
125
-
126
- > [!IMPORTANT]
127
- > Request-origin auth does not work with `/v3/ipgeo-bulk`. Bulk lookup always requires `api_key`.
128
-
129
- > [!NOTE]
130
- > If you set both `api_key` and `request_origin`, single lookup still uses the API key. The API key is sent as the `apiKey` query parameter, so avoid logging full request URLs.
116
+ The API key is sent as the `apiKey` query parameter, so avoid logging full request URLs.
131
117
 
132
118
  ## Plan Behavior
133
119
 
@@ -139,7 +125,6 @@ Feature availability depends on your plan and request parameters.
139
125
  | Domain lookup | Not supported | Supported |
140
126
  | Bulk lookup | Not supported | Supported |
141
127
  | Non-English `lang` | Not supported | Supported |
142
- | Request-origin auth | Not supported | Supported for `/v3/ipgeo` only |
143
128
  | Optional modules via `include` | Not supported | Supported |
144
129
  | `include: ["*"]` | Base response only | All plan-available modules |
145
130
 
@@ -149,8 +134,7 @@ Paid plans still need `include` for optional modules. `fields` and `excludes` on
149
134
 
150
135
  | Field | Type | Default | Notes |
151
136
  |-------|------|---------|-------|
152
- | `api_key` | `String` | unset | Required for bulk lookup. Optional for single lookup if `request_origin` is set. |
153
- | `request_origin` | `String` | unset | Must be an absolute `http` or `https` origin. |
137
+ | `api_key` | `String` | unset | Your IPGeolocation.io API key. |
154
138
  | `base_url` | `String` | `https://api.ipgeolocation.io` | Override the API base URL. |
155
139
  | `connect_timeout` | `Numeric` | `10.0` | Time to open the connection, in seconds. |
156
140
  | `read_timeout` | `Numeric` | `30.0` | Time to wait while reading the response body, in seconds. |
@@ -176,7 +160,7 @@ The client constructor accepts either an `IpGeolocationClientConfig` or a hash w
176
160
  |-------|------------|-------|
177
161
  | `ip` | Single lookup | IPv4, IPv6, or domain. Omit it for caller IP lookup. |
178
162
  | `ips` | Bulk lookup | Array of 1 to 50,000 IPs or domains. |
179
- | `lang` | Single and bulk | One of `en`, `de`, `ru`, `ja`, `fr`, `cn`, `es`, `cs`, `it`, `ko`, `fa`, `pt`. |
163
+ | `lang` | Single and bulk | One of `en`, `de`, `ru`, `ja`, `fr`, `cn`, `es`, `cs`, `it`, `ko`, `fa`, `pt`, `ar` (Arabic). |
180
164
  | `include` | Single and bulk | Array of module names such as `security`, `abuse`, `user_agent`, `hostname`, `liveHostname`, `hostnameFallbackLive`, `geo_accuracy`, `dma_code`, or `*`. |
181
165
  | `fields` | Single and bulk | Array of field paths to keep, for example `["location.country_name", "security.threat_score"]`. |
182
166
  | `excludes` | Single and bulk | Array of field paths to remove from the response. |
@@ -335,6 +319,8 @@ Compact mode omits `nil` fields. Full mode keeps them. When serializing a client
335
319
 
336
320
  ## Errors
337
321
 
322
+ If a request error includes your configured API key, the SDK replaces it with `[REDACTED]`. This also applies to errors in typed bulk results. SDK error types and HTTP status codes stay the same. Some error details may be shortened to keep the key hidden.
323
+
338
324
  The SDK raises these exception classes:
339
325
 
340
326
  - `IpgeolocationSdk::ValidationError`
@@ -367,10 +353,8 @@ end
367
353
 
368
354
  ## Troubleshooting
369
355
 
370
- - `bulk lookup requires api_key in client config`
371
- Bulk lookup does not support request-origin auth on its own.
372
- - `single lookup requires api_key or request_origin in client config`
373
- Set at least one authentication option before calling the single lookup methods.
356
+ - Missing API key
357
+ Set `api_key` in the client configuration before making a lookup.
374
358
  - `XML output is not supported by typed methods`
375
359
  Use `lookup_ip_geolocation_raw` or `bulk_lookup_ip_geolocation_raw` for XML output.
376
360
  - `client is closed`
@@ -33,61 +33,85 @@ module IpgeolocationSdk
33
33
  end
34
34
 
35
35
  def lookup_ip_geolocation(request = nil)
36
+ api_key = @config.api_key&.strip
37
+ redactor = ErrorRedactor.new(api_key)
36
38
  ensure_open
37
39
  typed_request = to_lookup_request(request)
38
40
  validate_lookup_request_base
39
41
  Serde.validate_json_output(typed_request.output)
40
42
 
41
- response, duration_ms = execute_with_metrics(build_lookup_http_request(typed_request))
42
- raise Serde.to_api_error(response.status_code, response.body) unless success_status?(response.status_code)
43
+ response, duration_ms = execute_with_metrics(build_lookup_http_request(typed_request, api_key))
44
+ raise Serde.to_api_error(response.status_code, response.body, redactor) unless success_status?(response.status_code)
43
45
 
44
46
  ApiResponse.new(
45
47
  data: Serde.parse_single_lookup(response.body),
46
48
  metadata: Serde.to_metadata(response.status_code, duration_ms, response.headers)
47
49
  )
50
+ rescue StandardError => error
51
+ raise if api_key.nil?
52
+
53
+ raise redactor.copy(error), cause: nil
48
54
  end
49
55
 
50
56
  def lookup_ip_geolocation_raw(request = nil)
57
+ api_key = @config.api_key&.strip
58
+ redactor = ErrorRedactor.new(api_key)
51
59
  ensure_open
52
60
  typed_request = to_lookup_request(request)
53
61
  validate_lookup_request_base
54
62
 
55
- response, duration_ms = execute_with_metrics(build_lookup_http_request(typed_request))
56
- raise Serde.to_api_error(response.status_code, response.body) unless success_status?(response.status_code)
63
+ response, duration_ms = execute_with_metrics(build_lookup_http_request(typed_request, api_key))
64
+ raise Serde.to_api_error(response.status_code, response.body, redactor) unless success_status?(response.status_code)
57
65
 
58
66
  ApiResponse.new(
59
67
  data: response.body,
60
68
  metadata: Serde.to_metadata(response.status_code, duration_ms, response.headers)
61
69
  )
70
+ rescue StandardError => error
71
+ raise if api_key.nil?
72
+
73
+ raise redactor.copy(error), cause: nil
62
74
  end
63
75
 
64
76
  def bulk_lookup_ip_geolocation(request)
77
+ api_key = @config.api_key&.strip
78
+ redactor = ErrorRedactor.new(api_key)
65
79
  ensure_open
66
80
  typed_request = to_bulk_request(request)
67
81
  validate_bulk_request_base
68
82
  Serde.validate_json_output(typed_request.output)
69
83
 
70
- response, duration_ms = execute_with_metrics(build_bulk_http_request(typed_request))
71
- raise Serde.to_api_error(response.status_code, response.body) unless success_status?(response.status_code)
84
+ response, duration_ms = execute_with_metrics(build_bulk_http_request(typed_request, api_key))
85
+ raise Serde.to_api_error(response.status_code, response.body, redactor) unless success_status?(response.status_code)
72
86
 
73
87
  ApiResponse.new(
74
- data: Serde.parse_bulk_lookup(response.body),
88
+ data: Serde.parse_bulk_lookup(response.body, redactor),
75
89
  metadata: Serde.to_metadata(response.status_code, duration_ms, response.headers)
76
90
  )
91
+ rescue StandardError => error
92
+ raise if api_key.nil?
93
+
94
+ raise redactor.copy(error), cause: nil
77
95
  end
78
96
 
79
97
  def bulk_lookup_ip_geolocation_raw(request)
98
+ api_key = @config.api_key&.strip
99
+ redactor = ErrorRedactor.new(api_key)
80
100
  ensure_open
81
101
  typed_request = to_bulk_request(request)
82
102
  validate_bulk_request_base
83
103
 
84
- response, duration_ms = execute_with_metrics(build_bulk_http_request(typed_request))
85
- raise Serde.to_api_error(response.status_code, response.body) unless success_status?(response.status_code)
104
+ response, duration_ms = execute_with_metrics(build_bulk_http_request(typed_request, api_key))
105
+ raise Serde.to_api_error(response.status_code, response.body, redactor) unless success_status?(response.status_code)
86
106
 
87
107
  ApiResponse.new(
88
108
  data: response.body,
89
109
  metadata: Serde.to_metadata(response.status_code, duration_ms, response.headers)
90
110
  )
111
+ rescue StandardError => error
112
+ raise if api_key.nil?
113
+
114
+ raise redactor.copy(error), cause: nil
91
115
  end
92
116
 
93
117
  def close
@@ -140,10 +164,10 @@ module IpgeolocationSdk
140
164
  end
141
165
  end
142
166
 
143
- def build_lookup_http_request(request)
167
+ def build_lookup_http_request(request, api_key)
144
168
  output = request.output || ResponseFormat::JSON
145
169
  query = Serde.build_query(
146
- "apiKey" => @config.api_key,
170
+ "apiKey" => api_key,
147
171
  "ip" => request.ip,
148
172
  "lang" => request.lang,
149
173
  "include" => request.include.empty? ? nil : request.include.join(","),
@@ -175,10 +199,10 @@ module IpgeolocationSdk
175
199
  )
176
200
  end
177
201
 
178
- def build_bulk_http_request(request)
202
+ def build_bulk_http_request(request, api_key)
179
203
  output = request.output || ResponseFormat::JSON
180
204
  query = Serde.build_query(
181
- "apiKey" => @config.api_key,
205
+ "apiKey" => api_key,
182
206
  "lang" => request.lang,
183
207
  "include" => request.include.empty? ? nil : request.include.join(","),
184
208
  "fields" => request.fields.empty? ? nil : request.fields.join(","),
@@ -14,9 +14,10 @@ module IpgeolocationSdk
14
14
  KO = "ko"
15
15
  FA = "fa"
16
16
  PT = "pt"
17
+ AR = "ar"
17
18
 
18
19
  VALUES = [
19
- EN, DE, RU, JA, FR, CN, ES, CS, IT, KO, FA, PT
20
+ EN, DE, RU, JA, FR, CN, ES, CS, IT, KO, FA, PT, AR
20
21
  ].freeze
21
22
 
22
23
  module_function
@@ -1,5 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "cgi"
4
+ require "json"
5
+
3
6
  module IpgeolocationSdk
4
7
  class IpGeolocationError < StandardError
5
8
  attr_reader :cause
@@ -35,4 +38,56 @@ module IpgeolocationSdk
35
38
  class RateLimitError < ApiError; end
36
39
  class ClientClosedRequestError < ApiError; end
37
40
  class ServerError < ApiError; end
41
+
42
+ class ErrorRedactor
43
+ def initialize(api_key)
44
+ return if api_key.nil? || api_key.empty?
45
+
46
+ key = api_key.encode(Encoding::UTF_8, invalid: :replace, undef: :replace)
47
+ escaped = CGI.escape(key)
48
+ forms = [key, escaped, escaped.gsub("+", "%20"), CGI.escapeHTML(key),
49
+ JSON.generate(key)[1...-1], JSON.generate(key, ascii_only: true)[1...-1]]
50
+ patterns = forms.uniq.sort_by { |form| -form.length }.map do |form|
51
+ Regexp.escape(form).gsub(/%[0-9A-F]{2}/) { |escape| "(?i:#{escape})" }
52
+ end
53
+ @pattern = Regexp.new(patterns.join("|"))
54
+ end
55
+
56
+ def text(value)
57
+ return value if value.nil? || @pattern.nil?
58
+
59
+ value.encode(Encoding::UTF_8, invalid: :replace, undef: :replace).gsub(@pattern, "[REDACTED]")
60
+ end
61
+
62
+ def copy(error, depth = 0)
63
+ return nil if error.nil? || depth >= 8
64
+
65
+ message = error.is_a?(JSON::ParserError) ? "Invalid JSON response" : text(error.message)
66
+ cause = copy(error.cause, depth + 1)
67
+ sanitized =
68
+ begin
69
+ if error.is_a?(ApiError)
70
+ error.class.new(message, error.status_code, text(error.api_message))
71
+ elsif error.is_a?(IpGeolocationError)
72
+ error.class.new(message, cause: cause)
73
+ else
74
+ error.class.new(message)
75
+ end
76
+ rescue StandardError
77
+ if error.is_a?(ApiError)
78
+ ApiError.new(message, error.status_code, text(error.api_message))
79
+ elsif error.is_a?(IpGeolocationError)
80
+ klass = [RequestTimeoutError, TransportError, ValidationError, SerializationError, IpGeolocationError].find do |type|
81
+ error.is_a?(type)
82
+ end
83
+ klass.new(message, cause: cause)
84
+ else
85
+ StandardError.new(message)
86
+ end
87
+ end
88
+ sanitized.set_backtrace(error.backtrace&.map { |line| text(line) })
89
+ sanitized
90
+ end
91
+ end
92
+ private_constant :ErrorRedactor
38
93
  end
@@ -14,7 +14,7 @@ module IpgeolocationSdk
14
14
  parse_ip_geolocation_response(payload)
15
15
  end
16
16
 
17
- def parse_bulk_lookup(body)
17
+ def parse_bulk_lookup(body, redactor = nil)
18
18
  payload = parse_json(body, "Failed to deserialize bulk lookup response")
19
19
  unless payload.is_a?(Array)
20
20
  raise SerializationError, "Failed to deserialize bulk response: expected an array payload"
@@ -22,8 +22,10 @@ module IpgeolocationSdk
22
22
 
23
23
  payload.map do |item|
24
24
  if bulk_error_item?(item)
25
+ message = string_or_nil(item["message"])
26
+ message = redactor.text(message) unless redactor.nil?
25
27
  BulkLookupError.new(
26
- error: BulkLookupErrorDetails.new(message: string_or_nil(item["message"]))
28
+ error: BulkLookupErrorDetails.new(message: message)
27
29
  )
28
30
  else
29
31
  raise SerializationError, "Failed to deserialize API response" unless item.is_a?(Hash)
@@ -83,8 +85,8 @@ module IpgeolocationSdk
83
85
  raise ValidationError, "XML output is not supported by typed methods. Use ResponseFormat::JSON."
84
86
  end
85
87
 
86
- def to_api_error(status_code, body)
87
- api_message = extract_api_message(body)
88
+ def to_api_error(status_code, body, redactor = nil)
89
+ api_message = extract_api_message(body, redactor)
88
90
  message =
89
91
  if api_message.nil?
90
92
  "API request failed with HTTP status #{status_code}"
@@ -110,22 +112,25 @@ module IpgeolocationSdk
110
112
  klass.new(message, status_code, api_message)
111
113
  end
112
114
 
113
- def extract_api_message(body)
115
+ def extract_api_message(body, redactor = nil)
114
116
  return nil if body.nil?
115
117
 
116
118
  normalized = body.to_s.strip
117
119
  return nil if normalized.empty?
118
120
 
121
+ fallback = (redactor.nil? ? normalized : redactor.text(normalized))[0, 512]
119
122
  payload = JSON.parse(normalized)
120
- return normalized[0, 512] unless payload.is_a?(Hash)
123
+ return fallback unless payload.is_a?(Hash)
121
124
 
122
125
  message = payload["message"]
123
126
  if message.nil? && payload["error"].is_a?(Hash)
124
127
  message = payload["error"]["message"]
125
128
  end
126
- string_or_nil(message) || normalized[0, 512]
129
+ return fallback unless message.is_a?(String)
130
+
131
+ redactor.nil? ? message : redactor.text(message)
127
132
  rescue JSON::ParserError
128
- normalized[0, 512]
133
+ fallback
129
134
  end
130
135
 
131
136
  def parse_int_header(value)
@@ -1,3 +1,3 @@
1
1
  module IpgeolocationSdk
2
- VERSION = "2.0.1"
2
+ VERSION = "2.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ipgeolocation_sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - IPGeolocation.io