nz_post_api 0.3.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: fd7725f76d54c6a212beecd45f4ffc568ae7b0ab010d69ecf0f92d3dcbe57149
4
- data.tar.gz: 7298abe8f2dae29e17f9525839b405e5c88a6be22f9072021002cdc1e3a8c957
3
+ metadata.gz: e8ec46eab576b5d457acc5ba5476a9b31140871f1a67018946abe82e72e6bd6e
4
+ data.tar.gz: e21a327af35cebc7933043c19546a7496b7ff079f07166e24d790a0320d0112c
5
5
  SHA512:
6
- metadata.gz: 233660b133767bfc37a494719f344be5882d5d1093cbd92fc51b835e25481e0ba2d31ac4f5f4991622a9d3f3cae389350580e635bb99e35f55fdd4bce2efad9e
7
- data.tar.gz: '08319bcd20474c2bccbc4f69b14bb57ccbe648e9ef95c186b3bc719067259baa4e8e7bd8840acfdd62d79d7564d0283e986be1cafe1cb87eb3f3775892a85070'
6
+ metadata.gz: 34298ff6a6da402e7eeb864b725d536ff1379db1105e7fc97d8a39e0de961775ff0fa6ab99202cee7a5fa834e5af57025987ccfcb712d05230937bfb7c50be5f
7
+ data.tar.gz: c28df5d11dee6594d65da67e00261b972c1bdf87d73d4ecae3d033f6791929fbc13db0d536eb42bf5c3c7683fba2c4fd34c4b97c1f707d8e8a3be43175f6911b
data/AGENTS.md CHANGED
@@ -15,6 +15,7 @@ This project is a Ruby gem wrapper for the NZ Post API.
15
15
  2. Implement the test case.
16
16
  3. Implement the code to pass the test.
17
17
  4. Refactor.
18
+ 5. **Documentation**: Update README.md if there are any changes to the public API or configuration.
18
19
 
19
20
  ## API Documentation
20
21
 
data/README.md CHANGED
@@ -31,12 +31,26 @@ 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
 
46
+ ### Configuration
47
+
48
+ By default, the gem uses the UAT environment. Configuration options:
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)
53
+
40
54
  ### Parcel Address
41
55
 
42
56
  #### Search
@@ -59,6 +73,10 @@ end
59
73
 
60
74
  Create a new parcel label.
61
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
+
62
80
  ```ruby
63
81
  label_client = client.parcel_label
64
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
@@ -0,0 +1,21 @@
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
@@ -3,14 +3,16 @@
3
3
  module NzPostApi
4
4
  module Resources
5
5
  class ParcelAddress
6
- BASE_URL = "https://api.uat.nzpost.co.nz/parceladdress/2.0/domestic/addresses"
6
+ def base_url
7
+ "#{NzPostApi.configuration.base_url}/parceladdress/2.0/domestic/addresses"
8
+ end
7
9
 
8
10
  def initialize(client)
9
11
  @client = client
10
12
  end
11
13
 
12
14
  def search(q:, count: 10)
13
- response = @client.connection.get(BASE_URL, { q: q, count: count })
15
+ response = @client.connection.get(base_url, { q: q, count: count })
14
16
 
15
17
  if response.success?
16
18
  response.body["addresses"].map { |addr| Objects::Address.new(addr) }
@@ -3,24 +3,26 @@
3
3
  module NzPostApi
4
4
  module Resources
5
5
  class ParcelLabel
6
- BASE_URL = "https://api.uat.nzpost.co.nz/parcellabel/v3/labels"
6
+ def base_url
7
+ "#{NzPostApi.configuration.base_url}/parcellabel/v3/labels"
8
+ end
7
9
 
8
10
  def initialize(client)
9
11
  @client = client
10
12
  end
11
13
 
12
14
  def create(payload)
13
- response = @client.connection.post(BASE_URL, payload)
15
+ response = @client.connection.post(base_url, payload)
14
16
  handle_response(response)
15
17
  end
16
18
 
17
19
  def status(consignment_id)
18
- response = @client.connection.get("#{BASE_URL}/#{consignment_id}/status")
20
+ response = @client.connection.get("#{base_url}/#{consignment_id}/status")
19
21
  handle_response(response)
20
22
  end
21
23
 
22
24
  def download(consignment_id, format: "PDF")
23
- response = @client.connection.get("#{BASE_URL}/#{consignment_id}", { format: format })
25
+ response = @client.connection.get("#{base_url}/#{consignment_id}", { format: format })
24
26
  if response.success?
25
27
  response.body
26
28
  else
@@ -3,14 +3,16 @@
3
3
  module NzPostApi
4
4
  module Resources
5
5
  class ParcelTrack
6
- BASE_URL = "https://api.uat.nzpost.co.nz/parceltrack/3.0/parcels"
6
+ def base_url
7
+ "#{NzPostApi.configuration.base_url}/parceltrack/3.0/parcels"
8
+ end
7
9
 
8
10
  def initialize(client)
9
11
  @client = client
10
12
  end
11
13
 
12
14
  def track(tracking_reference)
13
- response = @client.connection.get("#{BASE_URL}/#{tracking_reference}")
15
+ response = @client.connection.get("#{base_url}/#{tracking_reference}")
14
16
 
15
17
  if response.success?
16
18
  Objects::ParcelTrack.new(response.body["results"])
@@ -24,7 +26,7 @@ module NzPostApi
24
26
  tracking_reference: tracking_reference,
25
27
  notification_endpoint: notification_endpoint
26
28
  }
27
- response = @client.connection.post("#{BASE_URL.sub('parcels', 'subscription/webhook/')}", payload)
29
+ response = @client.connection.post("#{base_url.sub('parcels', 'subscription/webhook/')}", payload)
28
30
 
29
31
  if response.success?
30
32
  Objects::ParcelTrackSubscription.new(response.body)
@@ -34,7 +36,7 @@ module NzPostApi
34
36
  end
35
37
 
36
38
  def unsubscribe(subscription_guid:)
37
- response = @client.connection.delete("#{BASE_URL.sub('parcels', 'subscription/webhook')}/#{subscription_guid}")
39
+ response = @client.connection.delete("#{base_url.sub('parcels', 'subscription/webhook')}/#{subscription_guid}")
38
40
 
39
41
  if response.success?
40
42
  true
@@ -3,14 +3,16 @@
3
3
  module NzPostApi
4
4
  module Resources
5
5
  class ShippingOption
6
- BASE_URL = "https://api.uat.nzpost.co.nz/shippingoptions/2.0/domestic"
6
+ def base_url
7
+ "#{NzPostApi.configuration.base_url}/shippingoptions/2.0/domestic"
8
+ end
7
9
 
8
10
  def initialize(client)
9
11
  @client = client
10
12
  end
11
13
 
12
14
  def list(params = {})
13
- response = @client.connection.get(BASE_URL, params)
15
+ response = @client.connection.get(base_url, params)
14
16
 
15
17
  if response.success?
16
18
  Objects::ShippingOption.new(response.body)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NzPostApi
4
- VERSION = "0.3.0"
4
+ VERSION = "0.5.0"
5
5
  end
data/lib/nz_post_api.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "nz_post_api/version"
4
+ require_relative "nz_post_api/configuration"
4
5
  require_relative "nz_post_api/objects/base"
5
6
  require_relative "nz_post_api/objects/address"
6
7
  require_relative "nz_post_api/objects/label"
@@ -16,5 +17,14 @@ require_relative "nz_post_api/resources/parcel_track"
16
17
 
17
18
  module NzPostApi
18
19
  class Error < StandardError; end
19
- # Your code goes here...
20
+
21
+ class << self
22
+ def configuration
23
+ @configuration ||= Configuration.new
24
+ end
25
+
26
+ def configure
27
+ yield(configuration)
28
+ end
29
+ end
20
30
  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.3.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
@@ -70,6 +70,7 @@ 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
73
74
  - lib/nz_post_api/objects/address.rb
74
75
  - lib/nz_post_api/objects/base.rb
75
76
  - lib/nz_post_api/objects/label.rb