nz_post_api 0.2.0 → 0.4.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/AGENTS.md +1 -0
- data/README.md +60 -1
- data/lib/nz_post_api/client.rb +4 -0
- data/lib/nz_post_api/configuration.rb +19 -0
- data/lib/nz_post_api/objects/parcel_track.rb +8 -0
- data/lib/nz_post_api/objects/parcel_track_subscription.rb +8 -0
- data/lib/nz_post_api/resources/parcel_address.rb +4 -2
- data/lib/nz_post_api/resources/parcel_label.rb +6 -4
- data/lib/nz_post_api/resources/parcel_track.rb +49 -0
- data/lib/nz_post_api/resources/shipping_option.rb +4 -2
- data/lib/nz_post_api/version.rb +1 -1
- data/lib/nz_post_api.rb +14 -1
- metadata +5 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: db4c2d341925855b00d54c677d88820dfcb183e16bf3a92e12c284bbdc665f80
|
|
4
|
+
data.tar.gz: 6ffccabb911d0b5683984b45c75b5aea2392771452794c0572a65bae0fea8350
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 4d66466023a5ae7a3d341218fe006cf25d022747bfc7dbf08be4a2026629982507cba8837b884609c68f1b11a3ca270484c13b50f4e43fb02e72683420ada83b
|
|
7
|
+
data.tar.gz: 2dac646484e52891d15da15f731625fb28a8fc1a389f54b2f3b66302f5f9b4f111fc8a5727a6ae516c90d51dba0c659944d67c3804a79290c4a4cec4e884a5b2
|
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
|
@@ -37,6 +37,16 @@ Then, initialize the client with the access token.
|
|
|
37
37
|
client = NzPostApi::Client.new(client_id: "YOUR_CLIENT_ID", access_token: access_token)
|
|
38
38
|
```
|
|
39
39
|
|
|
40
|
+
### Configuration
|
|
41
|
+
|
|
42
|
+
By default, the gem uses the UAT environment. To use the production environment, configure the gem as follows:
|
|
43
|
+
|
|
44
|
+
```ruby
|
|
45
|
+
NzPostApi.configure do |config|
|
|
46
|
+
config.prod = true # set to true for production, defaults to false (UAT)
|
|
47
|
+
end
|
|
48
|
+
```
|
|
49
|
+
|
|
40
50
|
### Parcel Address
|
|
41
51
|
|
|
42
52
|
#### Search
|
|
@@ -168,7 +178,7 @@ file.unlink # deletes the temp file
|
|
|
168
178
|
|
|
169
179
|
List available shipping options based on package details.
|
|
170
180
|
|
|
171
|
-
|
|
181
|
+
````ruby
|
|
172
182
|
shipping_client = client.shipping_options
|
|
173
183
|
|
|
174
184
|
params = {
|
|
@@ -182,10 +192,56 @@ params = {
|
|
|
182
192
|
|
|
183
193
|
response = shipping_client.list(params)
|
|
184
194
|
response.services.each do |service|
|
|
195
|
+
puts service.service_code
|
|
185
196
|
puts service.service_code
|
|
186
197
|
puts service.description
|
|
187
198
|
end
|
|
188
199
|
|
|
200
|
+
### Parcel Track
|
|
201
|
+
|
|
202
|
+
#### Track Parcel
|
|
203
|
+
|
|
204
|
+
Retrieve tracking events for a parcel.
|
|
205
|
+
|
|
206
|
+
```ruby
|
|
207
|
+
track_client = client.parcel_track
|
|
208
|
+
tracking = track_client.track("TRACKING_NUMBER")
|
|
209
|
+
|
|
210
|
+
puts tracking.tracking_reference
|
|
211
|
+
tracking.tracking_events.each do |event|
|
|
212
|
+
puts "#{event.event_datetime}: #{event.event_description}"
|
|
213
|
+
end
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
#### Subscribe to Updates
|
|
217
|
+
|
|
218
|
+
Subscribe to tracking updates for a parcel via webhook.
|
|
219
|
+
|
|
220
|
+
```ruby
|
|
221
|
+
track_client = client.parcel_track
|
|
222
|
+
subscription = track_client.subscribe(
|
|
223
|
+
tracking_reference: "TRACKING_NUMBER",
|
|
224
|
+
notification_endpoint: "https://my.endpoint.com/nz_post_tracking"
|
|
225
|
+
)
|
|
226
|
+
|
|
227
|
+
puts subscription.subscription_guid
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
#### Unsubscribe
|
|
231
|
+
|
|
232
|
+
Unsubscribe from tracking updates.
|
|
233
|
+
|
|
234
|
+
```ruby
|
|
235
|
+
track_client = client.parcel_track
|
|
236
|
+
success = track_client.unsubscribe(subscription_guid: "SUBSCRIPTION_GUID")
|
|
237
|
+
|
|
238
|
+
if success
|
|
239
|
+
puts "Unsubscribed successfully"
|
|
240
|
+
else
|
|
241
|
+
puts "Failed to unsubscribe"
|
|
242
|
+
end
|
|
243
|
+
````
|
|
244
|
+
|
|
189
245
|
## Development
|
|
190
246
|
|
|
191
247
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
@@ -203,4 +259,7 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
|
203
259
|
## Code of Conduct
|
|
204
260
|
|
|
205
261
|
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
|
+
|
|
206
265
|
```
|
data/lib/nz_post_api/client.rb
CHANGED
|
@@ -0,0 +1,19 @@
|
|
|
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
|
|
@@ -3,14 +3,16 @@
|
|
|
3
3
|
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"
|
|
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(
|
|
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
|
-
|
|
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(
|
|
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("#{
|
|
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("#{
|
|
25
|
+
response = @client.connection.get("#{base_url}/#{consignment_id}", { format: format })
|
|
24
26
|
if response.success?
|
|
25
27
|
response.body
|
|
26
28
|
else
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module NzPostApi
|
|
4
|
+
module Resources
|
|
5
|
+
class ParcelTrack
|
|
6
|
+
def base_url
|
|
7
|
+
"#{NzPostApi.configuration.base_url}/parceltrack/3.0/parcels"
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def initialize(client)
|
|
11
|
+
@client = client
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def track(tracking_reference)
|
|
15
|
+
response = @client.connection.get("#{base_url}/#{tracking_reference}")
|
|
16
|
+
|
|
17
|
+
if response.success?
|
|
18
|
+
Objects::ParcelTrack.new(response.body["results"])
|
|
19
|
+
else
|
|
20
|
+
raise NzPostApi::Error, "Failed to track parcel: #{response.status} - #{response.body}"
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def subscribe(tracking_reference:, notification_endpoint:)
|
|
25
|
+
payload = {
|
|
26
|
+
tracking_reference: tracking_reference,
|
|
27
|
+
notification_endpoint: notification_endpoint
|
|
28
|
+
}
|
|
29
|
+
response = @client.connection.post("#{base_url.sub('parcels', 'subscription/webhook/')}", payload)
|
|
30
|
+
|
|
31
|
+
if response.success?
|
|
32
|
+
Objects::ParcelTrackSubscription.new(response.body)
|
|
33
|
+
else
|
|
34
|
+
raise NzPostApi::Error, "Failed to subscribe to parcel: #{response.status} - #{response.body}"
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def unsubscribe(subscription_guid:)
|
|
39
|
+
response = @client.connection.delete("#{base_url.sub('parcels', 'subscription/webhook')}/#{subscription_guid}")
|
|
40
|
+
|
|
41
|
+
if response.success?
|
|
42
|
+
true
|
|
43
|
+
else
|
|
44
|
+
raise NzPostApi::Error, "Failed to unsubscribe from parcel: #{response.status} - #{response.body}"
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -3,14 +3,16 @@
|
|
|
3
3
|
module NzPostApi
|
|
4
4
|
module Resources
|
|
5
5
|
class ShippingOption
|
|
6
|
-
|
|
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(
|
|
15
|
+
response = @client.connection.get(base_url, params)
|
|
14
16
|
|
|
15
17
|
if response.success?
|
|
16
18
|
Objects::ShippingOption.new(response.body)
|
data/lib/nz_post_api/version.rb
CHANGED
data/lib/nz_post_api.rb
CHANGED
|
@@ -1,17 +1,30 @@
|
|
|
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"
|
|
7
8
|
require_relative "nz_post_api/objects/shipping_option"
|
|
9
|
+
require_relative "nz_post_api/objects/parcel_track"
|
|
10
|
+
require_relative "nz_post_api/objects/parcel_track_subscription"
|
|
8
11
|
require_relative "nz_post_api/auth"
|
|
9
12
|
require_relative "nz_post_api/client"
|
|
10
13
|
require_relative "nz_post_api/resources/parcel_address"
|
|
11
14
|
require_relative "nz_post_api/resources/parcel_label"
|
|
12
15
|
require_relative "nz_post_api/resources/shipping_option"
|
|
16
|
+
require_relative "nz_post_api/resources/parcel_track"
|
|
13
17
|
|
|
14
18
|
module NzPostApi
|
|
15
19
|
class Error < StandardError; end
|
|
16
|
-
|
|
20
|
+
|
|
21
|
+
class << self
|
|
22
|
+
def configuration
|
|
23
|
+
@configuration ||= Configuration.new
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def configure
|
|
27
|
+
yield(configuration)
|
|
28
|
+
end
|
|
29
|
+
end
|
|
17
30
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
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.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Andy Chong
|
|
@@ -70,12 +70,16 @@ 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
|
|
77
|
+
- lib/nz_post_api/objects/parcel_track.rb
|
|
78
|
+
- lib/nz_post_api/objects/parcel_track_subscription.rb
|
|
76
79
|
- lib/nz_post_api/objects/shipping_option.rb
|
|
77
80
|
- lib/nz_post_api/resources/parcel_address.rb
|
|
78
81
|
- lib/nz_post_api/resources/parcel_label.rb
|
|
82
|
+
- lib/nz_post_api/resources/parcel_track.rb
|
|
79
83
|
- lib/nz_post_api/resources/shipping_option.rb
|
|
80
84
|
- lib/nz_post_api/version.rb
|
|
81
85
|
- sig/nz_post_api.rbs
|