nz_post_api 0.4.0 → 0.5.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: db4c2d341925855b00d54c677d88820dfcb183e16bf3a92e12c284bbdc665f80
4
- data.tar.gz: 6ffccabb911d0b5683984b45c75b5aea2392771452794c0572a65bae0fea8350
3
+ metadata.gz: e8ec46eab576b5d457acc5ba5476a9b31140871f1a67018946abe82e72e6bd6e
4
+ data.tar.gz: e21a327af35cebc7933043c19546a7496b7ff079f07166e24d790a0320d0112c
5
5
  SHA512:
6
- metadata.gz: 4d66466023a5ae7a3d341218fe006cf25d022747bfc7dbf08be4a2026629982507cba8837b884609c68f1b11a3ca270484c13b50f4e43fb02e72683420ada83b
7
- data.tar.gz: 2dac646484e52891d15da15f731625fb28a8fc1a389f54b2f3b66302f5f9b4f111fc8a5727a6ae516c90d51dba0c659944d67c3804a79290c4a4cec4e884a5b2
6
+ metadata.gz: 34298ff6a6da402e7eeb864b725d536ff1379db1105e7fc97d8a39e0de961775ff0fa6ab99202cee7a5fa834e5af57025987ccfcb712d05230937bfb7c50be5f
7
+ data.tar.gz: c28df5d11dee6594d65da67e00261b972c1bdf87d73d4ecae3d033f6791929fbc13db0d536eb42bf5c3c7683fba2c4fd34c4b97c1f707d8e8a3be43175f6911b
data/README.md CHANGED
@@ -31,21 +31,25 @@ 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, initialize the client with the access token.
34
+ Then, configure the gem and initialize the client.
35
35
 
36
36
  ```ruby
37
- client = NzPostApi::Client.new(client_id: "YOUR_CLIENT_ID", access_token: access_token)
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
38
44
  ```
39
45
 
40
46
  ### Configuration
41
47
 
42
- By default, the gem uses the UAT environment. To use the production environment, configure the gem as follows:
48
+ By default, the gem uses the UAT environment. Configuration options:
43
49
 
44
- ```ruby
45
- NzPostApi.configure do |config|
46
- config.prod = true # set to true for production, defaults to false (UAT)
47
- end
48
- ```
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)
49
53
 
50
54
  ### Parcel Address
51
55
 
@@ -69,6 +73,10 @@ end
69
73
 
70
74
  Create a new parcel label.
71
75
 
76
+ - **NZ Post rates**: To use your own NZ Post rates, include `account_number` and `site_code` in the payload.
77
+ - **Notification endpoint**: The `notification_endpoint` is a webhook URL. NZ Post will send HTTP requests to this endpoint when the parcel label status is updated.
78
+ - **Service codes** (in `parcel_details`): `CPOLP` is Overnight Returns (supports pickup); `EROLE` is Economy Returns (only drop off, the pickup address street must have the prefix "PO Box" or "Private Bag" followed by digits).
79
+
72
80
  ```ruby
73
81
  label_client = client.parcel_label
74
82
 
@@ -2,17 +2,20 @@
2
2
 
3
3
  require "faraday"
4
4
  require "json"
5
+ require "uri"
5
6
 
6
7
  module NzPostApi
7
8
  class Auth
8
9
  TOKEN_URL = "https://oauth.nzpost.co.nz/as/token.oauth2"
9
10
 
10
11
  def self.fetch_token(client_id, client_secret)
11
- response = Faraday.post(TOKEN_URL, {
12
+ params = {
12
13
  client_id: client_id,
13
14
  client_secret: client_secret,
14
15
  grant_type: "client_credentials"
15
- })
16
+ }
17
+ url_with_params = "#{TOKEN_URL}?#{URI.encode_www_form(params)}"
18
+ response = Faraday.post(url_with_params, nil)
16
19
 
17
20
  if response.success?
18
21
  JSON.parse(response.body)
@@ -4,11 +4,15 @@ require "faraday"
4
4
 
5
5
  module NzPostApi
6
6
  class Client
7
- attr_reader :client_id, :access_token
7
+ def initialize
8
+ end
9
+
10
+ def client_id
11
+ NzPostApi.configuration.client_id
12
+ end
8
13
 
9
- def initialize(client_id:, access_token:)
10
- @client_id = client_id
11
- @access_token = access_token
14
+ def access_token
15
+ NzPostApi.configuration.access_token
12
16
  end
13
17
 
14
18
  def connection
@@ -2,10 +2,12 @@
2
2
 
3
3
  module NzPostApi
4
4
  class Configuration
5
- attr_accessor :prod
5
+ attr_accessor :prod, :client_id, :access_token
6
6
 
7
7
  def initialize
8
8
  @prod = false
9
+ @client_id = nil
10
+ @access_token = nil
9
11
  end
10
12
 
11
13
  def base_url
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NzPostApi
4
- VERSION = "0.4.0"
4
+ VERSION = "0.5.0"
5
5
  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.4.0
4
+ version: 0.5.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-02 00:00:00.000000000 Z
11
+ date: 2026-02-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday