nzbn-ruby 0.1.2 → 0.1.4
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 +4 -4
- data/CHANGELOG.md +14 -1
- data/README.md +3 -0
- data/lib/nzbn/client.rb +38 -15
- data/lib/nzbn/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 0bc9736578fcb4a9663e87a28d910e91dbfdc9acf724f30c09bfc03fb47b5e3f
|
|
4
|
+
data.tar.gz: 1fffb71fac5bf19a6192220b89674212066f2be603fd37c2a6d8d4f7f8f6cf3f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c5de87bf46f1d4bb736a5bb42815e25524c359e7c3e98b9f5b83fdf854862a4de960c80a1a43a25c18214e485660820cbbcd30003b40ba9cee7a5e7b52ff0403
|
|
7
|
+
data.tar.gz: 7efe36fe483fe7a4ca917b313e65466aabef6539b8e8dc1ec83666df6ca0c64c96923d53f54782e5e69656fedf697ad74a5d63e80365be2268547aafde5d9f28
|
data/CHANGELOG.md
CHANGED
|
@@ -5,10 +5,23 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [0.1.4] - 2026-02-09
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
- Added request/response logging with configurable logger
|
|
12
|
+
- Logs request ID, HTTP method, URL before each API call
|
|
13
|
+
- Logs response status after each API call
|
|
14
|
+
|
|
15
|
+
## [0.1.3] - 2026-02-09
|
|
16
|
+
|
|
17
|
+
### Fixed
|
|
18
|
+
- Fixed `:json is not registered on Faraday::Request` error by removing middleware dependency
|
|
19
|
+
- Manually handle JSON encoding/decoding for better compatibility with Faraday 1.x
|
|
20
|
+
|
|
8
21
|
## [0.1.2] - 2026-02-09
|
|
9
22
|
|
|
10
23
|
### Fixed
|
|
11
|
-
- Update
|
|
24
|
+
- Update dependency gems.
|
|
12
25
|
|
|
13
26
|
## [0.1.1] - 2026-02-09
|
|
14
27
|
|
data/README.md
CHANGED
|
@@ -33,10 +33,13 @@ gem install nzbn-ruby
|
|
|
33
33
|
Configure the gem with your NZBN API key (obtain from [api.business.govt.nz](https://api.business.govt.nz)):
|
|
34
34
|
|
|
35
35
|
```ruby
|
|
36
|
+
require 'logger'
|
|
37
|
+
|
|
36
38
|
Nzbn.configure do |config|
|
|
37
39
|
config.api_key = 'your-api-key'
|
|
38
40
|
config.base_url = 'https://api.business.govt.nz/gateway/nzbn/v5' # default
|
|
39
41
|
config.timeout = 30 # optional, default 30 seconds
|
|
42
|
+
config.logger = Logger.new(STDOUT) # optional, enables request/response logging
|
|
40
43
|
end
|
|
41
44
|
```
|
|
42
45
|
|
data/lib/nzbn/client.rb
CHANGED
|
@@ -109,21 +109,18 @@ module Nzbn
|
|
|
109
109
|
|
|
110
110
|
def connection
|
|
111
111
|
@connection ||= Faraday.new(url: configuration.base_url) do |conn|
|
|
112
|
-
conn.request :json
|
|
113
|
-
conn.response :json, content_type: /\bjson$/
|
|
114
112
|
conn.adapter Faraday.default_adapter
|
|
115
113
|
|
|
116
114
|
conn.options.timeout = configuration.timeout
|
|
117
115
|
conn.options.open_timeout = configuration.open_timeout
|
|
118
|
-
|
|
119
|
-
if configuration.logger
|
|
120
|
-
conn.response :logger, configuration.logger
|
|
121
|
-
end
|
|
122
116
|
end
|
|
123
117
|
end
|
|
124
118
|
|
|
125
119
|
def request(method, path, data, headers)
|
|
126
120
|
request_id = SecureRandom.uuid
|
|
121
|
+
full_url = "#{configuration.base_url}#{path}"
|
|
122
|
+
|
|
123
|
+
log_request(method, full_url, request_id, data)
|
|
127
124
|
|
|
128
125
|
merged_headers = default_headers(request_id).merge(headers)
|
|
129
126
|
|
|
@@ -135,10 +132,11 @@ module Nzbn
|
|
|
135
132
|
when :get, :delete
|
|
136
133
|
req.params = data unless data.empty?
|
|
137
134
|
else
|
|
138
|
-
req.body = data
|
|
135
|
+
req.body = JSON.generate(data) unless data.empty?
|
|
139
136
|
end
|
|
140
137
|
end
|
|
141
138
|
|
|
139
|
+
log_response(response, request_id)
|
|
142
140
|
handle_response(response)
|
|
143
141
|
rescue Faraday::TimeoutError => e
|
|
144
142
|
raise TimeoutError, "Request timed out: #{e.message}"
|
|
@@ -146,6 +144,21 @@ module Nzbn
|
|
|
146
144
|
raise ConnectionError, "Connection failed: #{e.message}"
|
|
147
145
|
end
|
|
148
146
|
|
|
147
|
+
def log_request(method, url, request_id, data)
|
|
148
|
+
return unless configuration.logger
|
|
149
|
+
|
|
150
|
+
configuration.logger.info("[NZBN] Request ID: #{request_id}")
|
|
151
|
+
configuration.logger.info("[NZBN] #{method.to_s.upcase} #{url}")
|
|
152
|
+
configuration.logger.debug("[NZBN] Request data: #{data.inspect}") unless data.empty?
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def log_response(response, request_id)
|
|
156
|
+
return unless configuration.logger
|
|
157
|
+
|
|
158
|
+
configuration.logger.info("[NZBN] Response ID: #{request_id} - Status: #{response.status}")
|
|
159
|
+
configuration.logger.debug("[NZBN] Response body: #{response.body[0..500]}...")
|
|
160
|
+
end
|
|
161
|
+
|
|
149
162
|
def default_headers(request_id)
|
|
150
163
|
{
|
|
151
164
|
'Ocp-Apim-Subscription-Key' => configuration.api_key,
|
|
@@ -156,24 +169,34 @@ module Nzbn
|
|
|
156
169
|
end
|
|
157
170
|
|
|
158
171
|
def handle_response(response)
|
|
172
|
+
body = parse_json(response.body)
|
|
173
|
+
|
|
159
174
|
case response.status
|
|
160
175
|
when 200..299
|
|
161
|
-
|
|
176
|
+
body
|
|
162
177
|
when 400
|
|
163
|
-
raise ValidationError.new(response:
|
|
178
|
+
raise ValidationError.new(response: body, status: response.status)
|
|
164
179
|
when 401
|
|
165
|
-
raise AuthenticationError.new('Authentication failed', response:
|
|
180
|
+
raise AuthenticationError.new('Authentication failed', response: body, status: response.status)
|
|
166
181
|
when 403
|
|
167
|
-
raise AuthorizationError.new('Authorization denied', response:
|
|
182
|
+
raise AuthorizationError.new('Authorization denied', response: body, status: response.status)
|
|
168
183
|
when 404
|
|
169
|
-
raise NotFoundError.new('Resource not found', response:
|
|
184
|
+
raise NotFoundError.new('Resource not found', response: body, status: response.status)
|
|
170
185
|
when 412
|
|
171
|
-
raise PreconditionFailedError.new('Precondition failed', response:
|
|
186
|
+
raise PreconditionFailedError.new('Precondition failed', response: body, status: response.status)
|
|
172
187
|
when 500..599
|
|
173
|
-
raise ServerError.new('Server error', response:
|
|
188
|
+
raise ServerError.new('Server error', response: body, status: response.status)
|
|
174
189
|
else
|
|
175
|
-
raise ApiError.new("Unexpected response: #{response.status}", response:
|
|
190
|
+
raise ApiError.new("Unexpected response: #{response.status}", response: body, status: response.status)
|
|
176
191
|
end
|
|
177
192
|
end
|
|
193
|
+
|
|
194
|
+
def parse_json(body)
|
|
195
|
+
return nil if body.nil? || body.empty?
|
|
196
|
+
|
|
197
|
+
JSON.parse(body)
|
|
198
|
+
rescue JSON::ParserError
|
|
199
|
+
body
|
|
200
|
+
end
|
|
178
201
|
end
|
|
179
202
|
end
|
data/lib/nzbn/version.rb
CHANGED