tcat 0.3.5 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f40a1e40fc125273e5928860f2afa18f22ad88a22a2b02d63f441b9197b0153d
4
- data.tar.gz: 6e9d0c2f7bf65f9cc81a9764761092b76dce77faedc57836f3d9f4866b327b00
3
+ metadata.gz: 5ed5fe975474cdb30f8d660719b0a59d0ea5f1813b4e86433c95cd2ee82c795e
4
+ data.tar.gz: 025bf6aa6cfc46a4437d72e1d8abc966853606c1ba58b56ea5f6334a541b063e
5
5
  SHA512:
6
- metadata.gz: c402bffc3cc32eb481d930958107d08e278b362390cc4d6f62bbe9b8357ebc5501e74dfce68ca1326040a32ef9bc51274b8853c9b57e6cd7b0b98ca19c52cbf4
7
- data.tar.gz: 83fb6ec4ec1e9731fbd7d8fc0288221676afd14904101c3063304ae687cb54cc83112a9baf28eb616251d3618452976e0e7bc6ba284ef49d3a49d20b19a4b6e9
6
+ metadata.gz: b59a640cf706df074a2daa9bac89dfed2d96b1cfa1720278afe00b66ca8991cf64c096e4fca3701b05e7f43009b71f5f2e57186c902b339000d76db6098f4b13
7
+ data.tar.gz: 4d678f18e49855e6faf85ac8f4b0969f7c3c6d7d8a176686cd31ecc875e871a36ac3452a19dc9e5c7cd02ef559fdccf4e727085984c52d0066456f1b8b27302c
data/README.md CHANGED
@@ -64,11 +64,16 @@ Tcat.configure do |config|
64
64
  config.secret_key = 'your_secret_key'
65
65
  end
66
66
 
67
- # Create a query instance
68
- query = Tcat::Query.new('your_tracking_number')
67
+ # Two equivalent shapes — pick whichever fits your code:
69
68
 
70
- # Get shipment status
69
+ # A) tracking number bound at construction (one-shot)
70
+ query = Tcat::Query.new('your_tracking_number')
71
71
  status = query.status_code
72
+
73
+ # B) tracking number per call (reuse one client for many lookups,
74
+ # matches Tcat::WorkerClient's shape)
75
+ query = Tcat::Query.new
76
+ status = query.status_code('your_tracking_number')
72
77
  # Returns one of the following:
73
78
  # :done - Successfully delivered
74
79
  # :delivering - Out for delivery
@@ -265,6 +270,12 @@ This gem is available as open source under the terms of the [MIT License](https:
265
270
 
266
271
  ## Changelog
267
272
 
273
+ ### 0.4.0
274
+
275
+ - `Tcat::Query#status_code`, `#history`, `#latest_status` now accept an optional tracking-number argument, mirroring `Tcat::WorkerClient`'s shape
276
+ - `Tcat::Query.new` may be called without a tracking number for stateless reuse: `Tcat::Query.new.status_code('1234567890')`
277
+ - Existing one-shot construction (`Tcat::Query.new(tn).status_code`) is still supported
278
+
268
279
  ### 0.3.5
269
280
 
270
281
  - Added optional `worker_url` and `worker_token` to `Tcat.configure`; explicit `Tcat::WorkerClient.new(url, token:)` arguments still take precedence
data/lib/tcat/query.rb CHANGED
@@ -11,7 +11,11 @@ module Tcat
11
11
  class Query
12
12
  DeliveryItem = Struct.new(:status, :status_code, :time, :office, :last_update, keyword_init: true)
13
13
 
14
- def initialize(tracking_number)
14
+ # @param tracking_number [String, nil] Default tracking number used when
15
+ # the per-call methods are invoked without an explicit argument. Pass nil
16
+ # to construct a stateless client and supply the tracking number on each
17
+ # call (matching Tcat::WorkerClient's shape).
18
+ def initialize(tracking_number = nil)
15
19
  @secret_string = Tcat.configuration.secret_string
16
20
  @secret_key = Tcat.configuration.secret_key
17
21
  validate_secrets!
@@ -25,22 +29,27 @@ module Tcat
25
29
  end
26
30
 
27
31
  # Get current delivery status code
32
+ # @param tracking_number [String, nil] Overrides the constructor value.
28
33
  # @return [Symbol] Status code (:done, :delivering, :collected, :in_transit, :unknown)
29
- def status_code
30
- response_body = @http_client.post(data)
34
+ def status_code(tracking_number = nil)
35
+ tn = resolve_tracking_number(tracking_number)
36
+ response_body = @http_client.post(data(tn))
31
37
  parse_status_code(response_body) if response_body
32
- rescue HttpClient::RequestError => e
38
+ rescue HttpClient::RequestError
33
39
  # Log error or handle it appropriately
34
40
  nil
35
41
  end
36
42
 
37
43
  # Get complete delivery history
44
+ # @param tracking_number [String, nil] Overrides the constructor value.
38
45
  # @return [Array<DeliveryItem>] Array of delivery status items, sorted by time (newest first)
39
- def history
40
- response_body = @http_client.post(data)
46
+ def history(tracking_number = nil)
47
+ tn = resolve_tracking_number(tracking_number)
48
+ response_body = @http_client.post(data(tn))
41
49
  if response_body
42
50
  result = Ox.load(response_body, mode: :hash, with_cdata: true)
43
51
  return [] if result.dig(:Result, :Status) != '0'
52
+
44
53
  extract_delivery_history(result)
45
54
  end
46
55
  rescue StandardError => e
@@ -49,10 +58,12 @@ module Tcat
49
58
  end
50
59
 
51
60
  # Get latest delivery status with details
61
+ # @param tracking_number [String, nil] Overrides the constructor value.
52
62
  # @return [DeliveryItem, nil] Latest delivery status or nil if no history
53
- def latest_status
54
- items = history
63
+ def latest_status(tracking_number = nil)
64
+ items = history(tracking_number)
55
65
  return nil if items.empty?
66
+
56
67
  items.first
57
68
  end
58
69
 
@@ -75,9 +86,16 @@ module Tcat
75
86
  warn e.backtrace.first(5).join("\n") if $DEBUG
76
87
  end
77
88
 
78
- def data
89
+ def resolve_tracking_number(arg)
90
+ tn = arg || @tracking_number
91
+ raise ArgumentError, 'tracking number required' if tn.nil? || tn.to_s.empty?
92
+
93
+ tn
94
+ end
95
+
96
+ def data(tracking_number)
79
97
  {
80
- ConsignmentNo: @tracking_number,
98
+ ConsignmentNo: tracking_number,
81
99
  f: 5,
82
100
  isForeign: 'N',
83
101
  secret: generate_secret,
data/lib/tcat/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Tcat
4
- VERSION = '0.3.5'
4
+ VERSION = '0.4.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tcat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.5
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zac