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 +4 -4
- data/README.md +23 -17
- data/lib/nz_post_api/auth.rb +5 -1
- data/lib/nz_post_api/client.rb +12 -9
- data/lib/nz_post_api/resources/parcel_address.rb +6 -2
- data/lib/nz_post_api/resources/parcel_label.rb +11 -3
- data/lib/nz_post_api/resources/parcel_track.rb +16 -4
- data/lib/nz_post_api/resources/shipping_option.rb +6 -2
- data/lib/nz_post_api/version.rb +1 -1
- data/lib/nz_post_api.rb +6 -9
- data/sig/nz_post_api.rbs +6 -1
- metadata +2 -3
- data/lib/nz_post_api/configuration.rb +0 -21
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 959f7bc78fae9b31231d76977d9cbba085cf400a0f2c474a836aef47c05560d1
|
|
4
|
+
data.tar.gz: 8d588514158a3f0dc4e2b2b2157228cf4af180bd1fe74a20e8d3165f9fd077db
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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,
|
|
34
|
+
Then, initialize the client with your credentials:
|
|
35
35
|
|
|
36
36
|
```ruby
|
|
37
|
-
NzPostApi.
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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
|
-
|
|
44
|
+
**Client constructor:**
|
|
47
45
|
|
|
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
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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
|
-
```
|
data/lib/nz_post_api/auth.rb
CHANGED
|
@@ -20,7 +20,11 @@ module NzPostApi
|
|
|
20
20
|
if response.success?
|
|
21
21
|
JSON.parse(response.body)
|
|
22
22
|
else
|
|
23
|
-
raise NzPostApi::Error
|
|
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
|
data/lib/nz_post_api/client.rb
CHANGED
|
@@ -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
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
-
"#{
|
|
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
|
|
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
|
-
"#{
|
|
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
|
|
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
|
|
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
|
-
"#{
|
|
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
|
|
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
|
|
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
|
|
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
|
-
"#{
|
|
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
|
|
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
|
data/lib/nz_post_api/version.rb
CHANGED
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
|
|
18
|
+
class Error < StandardError
|
|
19
|
+
attr_reader :response_http_code, :response_body
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
@
|
|
24
|
-
|
|
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
|
-
|
|
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.
|
|
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-
|
|
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
|