nz_post_api 0.4.0 → 0.6.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: 79fc7d9823bc2ee076dd26982aad65e2ec9cd2354403ffd0afd6414f0a1b9f49
4
+ data.tar.gz: 22a970d9bba8db00bf083b2a586d024a1e8905c8430af12218bfce287926ebb1
5
5
  SHA512:
6
- metadata.gz: 4d66466023a5ae7a3d341218fe006cf25d022747bfc7dbf08be4a2026629982507cba8837b884609c68f1b11a3ca270484c13b50f4e43fb02e72683420ada83b
7
- data.tar.gz: 2dac646484e52891d15da15f731625fb28a8fc1a389f54b2f3b66302f5f9b4f111fc8a5727a6ae516c90d51dba0c659944d67c3804a79290c4a4cec4e884a5b2
6
+ metadata.gz: c6f21391ee069bcb2e08742f2b7c4e6aec58703063c5ed76c0e3bded858e6d4f6fa6180bafbaeb45178661997c826fcb8af40520532713f5e7d30c5e13acde78
7
+ data.tar.gz: f5c356b9498efcacfdc383b2f9333c936cfea3a1603cd724b3dccb812ae50c39176e18d847fc8e82f4c1b0b98e02590b1e68a8ba2fd434ddaa538b0c8ccd3cee
data/README.md CHANGED
@@ -31,21 +31,21 @@ 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, initialize the client with your credentials:
35
35
 
36
36
  ```ruby
37
- client = NzPostApi::Client.new(client_id: "YOUR_CLIENT_ID", access_token: access_token)
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
+ )
38
42
  ```
39
43
 
40
- ### Configuration
41
-
42
- By default, the gem uses the UAT environment. To use the production environment, configure the gem as follows:
44
+ **Client constructor:**
43
45
 
44
- ```ruby
45
- NzPostApi.configure do |config|
46
- config.prod = true # set to true for production, defaults to false (UAT)
47
- end
48
- ```
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
50
  ### Parcel Address
51
51
 
@@ -69,6 +69,10 @@ end
69
69
 
70
70
  Create a new parcel label.
71
71
 
72
+ - **NZ Post rates**: To use your own NZ Post rates, include `account_number` and `site_code` in the payload.
73
+ - **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.
74
+ - **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).
75
+
72
76
  ```ruby
73
77
  label_client = client.parcel_label
74
78
 
@@ -259,7 +263,3 @@ The gem is available as open source under the terms of the [MIT License](https:/
259
263
  ## Code of Conduct
260
264
 
261
265
  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).
262
-
263
- ```
264
-
265
- ```
@@ -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,19 +4,26 @@ require "faraday"
4
4
 
5
5
  module NzPostApi
6
6
  class Client
7
- attr_reader :client_id, :access_token
8
-
9
- def initialize(client_id:, access_token:)
7
+ def initialize(client_id, access_token, prod: false)
10
8
  @client_id = client_id
11
9
  @access_token = access_token
10
+ @prod = prod
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
12
19
  end
13
20
 
14
21
  def connection
15
22
  @connection ||= Faraday.new do |f|
16
23
  f.request :json
17
24
  f.response :json
18
- f.headers["client_id"] = client_id
19
- f.headers["Authorization"] = "Bearer #{access_token}"
25
+ f.headers["client_id"] = @client_id
26
+ f.headers["Authorization"] = "Bearer #{@access_token}"
20
27
  f.headers["Content-Type"] = "application/json"
21
28
  end
22
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)
@@ -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)
@@ -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)
@@ -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)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NzPostApi
4
- VERSION = "0.4.0"
4
+ VERSION = "0.6.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"
@@ -17,14 +16,4 @@ require_relative "nz_post_api/resources/parcel_track"
17
16
 
18
17
  module NzPostApi
19
18
  class Error < StandardError; end
20
-
21
- class << self
22
- def configuration
23
- @configuration ||= Configuration.new
24
- end
25
-
26
- def configure
27
- yield(configuration)
28
- end
29
- end
30
19
  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.6.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-05 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,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module NzPostApi
4
- class Configuration
5
- attr_accessor :prod
6
-
7
- def initialize
8
- @prod = false
9
- end
10
-
11
- def base_url
12
- if @prod
13
- "https://api.nzpost.co.nz"
14
- else
15
- "https://api.uat.nzpost.co.nz"
16
- end
17
- end
18
- end
19
- end