nz_post_api 0.5.0 → 0.7.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e8ec46eab576b5d457acc5ba5476a9b31140871f1a67018946abe82e72e6bd6e
4
- data.tar.gz: e21a327af35cebc7933043c19546a7496b7ff079f07166e24d790a0320d0112c
3
+ metadata.gz: 959f7bc78fae9b31231d76977d9cbba085cf400a0f2c474a836aef47c05560d1
4
+ data.tar.gz: 8d588514158a3f0dc4e2b2b2157228cf4af180bd1fe74a20e8d3165f9fd077db
5
5
  SHA512:
6
- metadata.gz: 34298ff6a6da402e7eeb864b725d536ff1379db1105e7fc97d8a39e0de961775ff0fa6ab99202cee7a5fa834e5af57025987ccfcb712d05230937bfb7c50be5f
7
- data.tar.gz: c28df5d11dee6594d65da67e00261b972c1bdf87d73d4ecae3d033f6791929fbc13db0d536eb42bf5c3c7683fba2c4fd34c4b97c1f707d8e8a3be43175f6911b
6
+ metadata.gz: 552186803f84a8dc134d297a5059d7ecbc1fd6d61ecc42cb823250c50765081efba4ddf1e6e9587a772de787e9378c37fa9b473d239c42ccb642099430e946c3
7
+ data.tar.gz: deadece3e518de6e102bafbba15f2c3a6ddf055a3a245365694a493160d4fde3ed9bce9d5ec332c8667fefce8e24b8e1827e84a8386681c123e31956b2e6af88
data/README.md CHANGED
@@ -31,25 +31,35 @@ expires_in = token_response["expires_in"]
31
31
  > [!IMPORTANT]
32
32
  > The access token expires after a certain period (indicated by `expires_in`). It is highly recommended to cache the `access_token` and reuse it until it expires to avoid unnecessary API calls and potential rate limiting.
33
33
 
34
- Then, configure the gem and initialize the client.
34
+ Then, initialize the client with your credentials:
35
35
 
36
36
  ```ruby
37
- NzPostApi.configure do |config|
38
- config.client_id = "YOUR_CLIENT_ID"
39
- config.access_token = access_token
40
- config.prod = true # set to true for production, defaults to false (UAT)
41
- end
42
-
43
- client = NzPostApi::Client.new
37
+ client = NzPostApi::Client.new(
38
+ "YOUR_CLIENT_ID",
39
+ access_token,
40
+ prod: true # set to true for production, false for UAT (default)
41
+ )
44
42
  ```
45
43
 
46
- ### Configuration
44
+ **Client constructor:**
47
45
 
48
- By default, the gem uses the UAT environment. Configuration options:
46
+ - `client_id` Your NZ Post API client ID (required)
47
+ - `access_token` – Bearer token from `NzPostApi::Auth.fetch_token` (required)
48
+ - `prod:` – Set to `true` for production (`https://api.nzpost.co.nz`), `false` for UAT (`https://api.uat.nzpost.co.nz`). Defaults to `false`.
49
49
 
50
- - `client_id` – Your NZ Post API client ID (required for API calls)
51
- - `access_token` – Bearer token from `NzPostApi::Auth.fetch_token` (required for API calls)
52
- - `prod` Set to `true` for production, `false` for UAT (default)
50
+ ### Error Handling
51
+
52
+ API request failures raise `NzPostApi::Error`. The error object exposes both HTTP status code and response body:
53
+
54
+ ```ruby
55
+ begin
56
+ client.parcel_address.search(q: "Invalid")
57
+ rescue NzPostApi::Error => error
58
+ puts error.message
59
+ puts error.response_http_code
60
+ puts error.response_body
61
+ end
62
+ ```
53
63
 
54
64
  ### Parcel Address
55
65
 
@@ -267,7 +277,3 @@ The gem is available as open source under the terms of the [MIT License](https:/
267
277
  ## Code of Conduct
268
278
 
269
279
  Everyone interacting in the NzPostApi project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/nz_post_api/blob/main/CODE_OF_CONDUCT.md).
270
-
271
- ```
272
-
273
- ```
@@ -20,7 +20,11 @@ module NzPostApi
20
20
  if response.success?
21
21
  JSON.parse(response.body)
22
22
  else
23
- raise NzPostApi::Error, "Failed to fetch token: #{response.status} - #{response.body}"
23
+ raise NzPostApi::Error.new(
24
+ "Failed to fetch token: #{response.status} - #{response.body}",
25
+ response_http_code: response.status,
26
+ response_body: response.body
27
+ )
24
28
  end
25
29
  end
26
30
  end
@@ -4,23 +4,26 @@ require "faraday"
4
4
 
5
5
  module NzPostApi
6
6
  class Client
7
- def initialize
7
+ def initialize(client_id, access_token, prod: false)
8
+ @client_id = client_id
9
+ @access_token = access_token
10
+ @prod = prod
8
11
  end
9
12
 
10
- def client_id
11
- NzPostApi.configuration.client_id
12
- end
13
-
14
- def access_token
15
- NzPostApi.configuration.access_token
13
+ def base_url
14
+ if @prod
15
+ "https://api.nzpost.co.nz"
16
+ else
17
+ "https://api.uat.nzpost.co.nz"
18
+ end
16
19
  end
17
20
 
18
21
  def connection
19
22
  @connection ||= Faraday.new do |f|
20
23
  f.request :json
21
24
  f.response :json
22
- f.headers["client_id"] = client_id
23
- f.headers["Authorization"] = "Bearer #{access_token}"
25
+ f.headers["client_id"] = @client_id
26
+ f.headers["Authorization"] = "Bearer #{@access_token}"
24
27
  f.headers["Content-Type"] = "application/json"
25
28
  end
26
29
  end
@@ -4,7 +4,7 @@ module NzPostApi
4
4
  module Resources
5
5
  class ParcelAddress
6
6
  def base_url
7
- "#{NzPostApi.configuration.base_url}/parceladdress/2.0/domestic/addresses"
7
+ "#{@client.base_url}/parceladdress/2.0/domestic/addresses"
8
8
  end
9
9
 
10
10
  def initialize(client)
@@ -17,7 +17,11 @@ module NzPostApi
17
17
  if response.success?
18
18
  response.body["addresses"].map { |addr| Objects::Address.new(addr) }
19
19
  else
20
- raise NzPostApi::Error, "Failed to search addresses: #{response.status} - #{response.body}"
20
+ raise NzPostApi::Error.new(
21
+ "Failed to search addresses: #{response.status} - #{response.body}",
22
+ response_http_code: response.status,
23
+ response_body: response.body
24
+ )
21
25
  end
22
26
  end
23
27
  end
@@ -4,7 +4,7 @@ module NzPostApi
4
4
  module Resources
5
5
  class ParcelLabel
6
6
  def base_url
7
- "#{NzPostApi.configuration.base_url}/parcellabel/v3/labels"
7
+ "#{@client.base_url}/parcellabel/v3/labels"
8
8
  end
9
9
 
10
10
  def initialize(client)
@@ -26,7 +26,11 @@ module NzPostApi
26
26
  if response.success?
27
27
  response.body
28
28
  else
29
- raise NzPostApi::Error, "Failed to download label: #{response.status} - #{response.body}"
29
+ raise NzPostApi::Error.new(
30
+ "Failed to download label: #{response.status} - #{response.body}",
31
+ response_http_code: response.status,
32
+ response_body: response.body
33
+ )
30
34
  end
31
35
  end
32
36
 
@@ -36,7 +40,11 @@ module NzPostApi
36
40
  if response.success?
37
41
  Objects::Label.new(response.body)
38
42
  else
39
- raise NzPostApi::Error, "Failed to create/get label: #{response.status} - #{response.body}"
43
+ raise NzPostApi::Error.new(
44
+ "Failed to create/get label: #{response.status} - #{response.body}",
45
+ response_http_code: response.status,
46
+ response_body: response.body
47
+ )
40
48
  end
41
49
  end
42
50
  end
@@ -4,7 +4,7 @@ module NzPostApi
4
4
  module Resources
5
5
  class ParcelTrack
6
6
  def base_url
7
- "#{NzPostApi.configuration.base_url}/parceltrack/3.0/parcels"
7
+ "#{@client.base_url}/parceltrack/3.0/parcels"
8
8
  end
9
9
 
10
10
  def initialize(client)
@@ -17,7 +17,11 @@ module NzPostApi
17
17
  if response.success?
18
18
  Objects::ParcelTrack.new(response.body["results"])
19
19
  else
20
- raise NzPostApi::Error, "Failed to track parcel: #{response.status} - #{response.body}"
20
+ raise NzPostApi::Error.new(
21
+ "Failed to track parcel: #{response.status} - #{response.body}",
22
+ response_http_code: response.status,
23
+ response_body: response.body
24
+ )
21
25
  end
22
26
  end
23
27
 
@@ -31,7 +35,11 @@ module NzPostApi
31
35
  if response.success?
32
36
  Objects::ParcelTrackSubscription.new(response.body)
33
37
  else
34
- raise NzPostApi::Error, "Failed to subscribe to parcel: #{response.status} - #{response.body}"
38
+ raise NzPostApi::Error.new(
39
+ "Failed to subscribe to parcel: #{response.status} - #{response.body}",
40
+ response_http_code: response.status,
41
+ response_body: response.body
42
+ )
35
43
  end
36
44
  end
37
45
 
@@ -41,7 +49,11 @@ module NzPostApi
41
49
  if response.success?
42
50
  true
43
51
  else
44
- raise NzPostApi::Error, "Failed to unsubscribe from parcel: #{response.status} - #{response.body}"
52
+ raise NzPostApi::Error.new(
53
+ "Failed to unsubscribe from parcel: #{response.status} - #{response.body}",
54
+ response_http_code: response.status,
55
+ response_body: response.body
56
+ )
45
57
  end
46
58
  end
47
59
  end
@@ -4,7 +4,7 @@ module NzPostApi
4
4
  module Resources
5
5
  class ShippingOption
6
6
  def base_url
7
- "#{NzPostApi.configuration.base_url}/shippingoptions/2.0/domestic"
7
+ "#{@client.base_url}/shippingoptions/2.0/domestic"
8
8
  end
9
9
 
10
10
  def initialize(client)
@@ -17,7 +17,11 @@ module NzPostApi
17
17
  if response.success?
18
18
  Objects::ShippingOption.new(response.body)
19
19
  else
20
- raise NzPostApi::Error, "Failed to list options: #{response.status} - #{response.body}"
20
+ raise NzPostApi::Error.new(
21
+ "Failed to list options: #{response.status} - #{response.body}",
22
+ response_http_code: response.status,
23
+ response_body: response.body
24
+ )
21
25
  end
22
26
  end
23
27
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NzPostApi
4
- VERSION = "0.5.0"
4
+ VERSION = "0.7.0"
5
5
  end
data/lib/nz_post_api.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "nz_post_api/version"
4
- require_relative "nz_post_api/configuration"
5
4
  require_relative "nz_post_api/objects/base"
6
5
  require_relative "nz_post_api/objects/address"
7
6
  require_relative "nz_post_api/objects/label"
@@ -16,15 +15,13 @@ require_relative "nz_post_api/resources/shipping_option"
16
15
  require_relative "nz_post_api/resources/parcel_track"
17
16
 
18
17
  module NzPostApi
19
- class Error < StandardError; end
18
+ class Error < StandardError
19
+ attr_reader :response_http_code, :response_body
20
20
 
21
- class << self
22
- def configuration
23
- @configuration ||= Configuration.new
24
- end
25
-
26
- def configure
27
- yield(configuration)
21
+ def initialize(message = nil, response_http_code: nil, response_body: nil)
22
+ @response_http_code = response_http_code
23
+ @response_body = response_body
24
+ super(message)
28
25
  end
29
26
  end
30
27
  end
data/sig/nz_post_api.rbs CHANGED
@@ -1,4 +1,9 @@
1
1
  module NzPostApi
2
2
  VERSION: String
3
- # See the writing guide of rbs: https://github.com/ruby/rbs#guides
3
+ class Error < ::StandardError
4
+ attr_reader response_http_code: Integer?
5
+ attr_reader response_body: untyped
6
+
7
+ def initialize: (?String message, ?response_http_code: Integer, ?response_body: untyped) -> void
8
+ end
4
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nz_post_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Chong
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2026-02-03 00:00:00.000000000 Z
11
+ date: 2026-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -70,7 +70,6 @@ files:
70
70
  - lib/nz_post_api.rb
71
71
  - lib/nz_post_api/auth.rb
72
72
  - lib/nz_post_api/client.rb
73
- - lib/nz_post_api/configuration.rb
74
73
  - lib/nz_post_api/objects/address.rb
75
74
  - lib/nz_post_api/objects/base.rb
76
75
  - lib/nz_post_api/objects/label.rb
@@ -1,21 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module NzPostApi
4
- class Configuration
5
- attr_accessor :prod, :client_id, :access_token
6
-
7
- def initialize
8
- @prod = false
9
- @client_id = nil
10
- @access_token = nil
11
- end
12
-
13
- def base_url
14
- if @prod
15
- "https://api.nzpost.co.nz"
16
- else
17
- "https://api.uat.nzpost.co.nz"
18
- end
19
- end
20
- end
21
- end