ninja_van_api 0.2.20 → 0.3.1

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: bf98d0353d3840d49fd319e69af2916a1b55562fd3ea0bc0b6213819b1b4197b
4
- data.tar.gz: 7012d0cbc47bfb80ceba90efb3256ecbe21b8373811e2d1528a56eaf765f1135
3
+ metadata.gz: cb390bd629fd1de1252ecb49fec0d66c75fc3a53da90754c68d99e3670a6833c
4
+ data.tar.gz: f6c902441430971d6995f408e2aa7f322d3ece92517e1ab22b98bffb94cdde0d
5
5
  SHA512:
6
- metadata.gz: 6df589c8bc14176fb7490b1a581aaa7434a8897aa323e9f200ea7605f7f37d758375c89b51d49396dc2d8404f9c07e85f1c252095194bd85687174fd54a83c97
7
- data.tar.gz: 73bf32a5e80bf9c90b7e11ac46b092802ffcc4c3d1dd19b3a51dd5b78977b58e24fd9ab9c277568dda0186fe4cb8291610b2def277f7277a64ced7cea31f7813
6
+ metadata.gz: bc10c585a79c66643e72fb09ec692fc8451098bc056ef5078ab6ca5977167547724b2ef0cf00b56a177988f485cab14c2aaad73a3da60c8bc85be97e71a835b8
7
+ data.tar.gz: 4ca9516bf8002b51babfdabbafa99b0ced8de8d38d228c34d72764aa76e1029f9921bd0625873c1e914879b78c4beac2472717d35edda4f2a7c7b17a654ac70b
data/README.md CHANGED
@@ -16,6 +16,38 @@ And then execute:
16
16
 
17
17
  ## Usage
18
18
 
19
+ ### Retrieving Waybills
20
+
21
+ ```ruby
22
+ # Initialize the client
23
+ client = NinjaVanApi::Client.new(
24
+ client_id: "your_client_id",
25
+ client_secret: "your_client_secret",
26
+ country_code: "SG",
27
+ test_mode: true
28
+ )
29
+
30
+ # Basic waybill retrieval
31
+ waybill = client.waybills.get("TRACKING123")
32
+
33
+ # Get waybill with optional parameters
34
+ waybill = client.waybills.get(
35
+ "TRACKING123",
36
+ hide_shipper_details: true, # Hide shipper's details in the waybill
37
+ orientation: "landscape" # Set waybill orientation to landscape
38
+ )
39
+
40
+ # Access the PDF content
41
+ pdf_content = waybill.pdf
42
+
43
+ # Error handling
44
+ begin
45
+ waybill = client.waybills.get("INVALID_TRACKING")
46
+ rescue NinjaVanApi::Error => e
47
+ puts "Error retrieving waybill: #{e.message}"
48
+ end
49
+ ```
50
+
19
51
  ### Mounting the Engine
20
52
 
21
53
  In your Rails application's `config/routes.rb`, mount the webhook engine. You can mount it multiple times with different country paths. For example:
@@ -7,11 +7,12 @@ module NinjaVanApi
7
7
  if NinjaVanApi.configuration.webhook_job_class
8
8
  klass =
9
9
  begin
10
- job_class.constantize
10
+ NinjaVanApi.configuration.webhook_job_class.constantize
11
11
  rescue NameError
12
12
  raise ArgumentError,
13
13
  "webhook_job_class must be an ActiveJob class name or class that responds to perform_later"
14
14
  end
15
+
15
16
  klass.perform_later(webhook_params.to_h)
16
17
  head :ok
17
18
  else
@@ -27,8 +28,8 @@ module NinjaVanApi
27
28
 
28
29
  def verify_webhook_signature
29
30
  # Extract country code from the request path
30
- # Example: /ninjavan/sg/webhooks -> 'sg'
31
- country_code = request.path.split("/")[2]&.downcase
31
+ # Example: /sg -> 'sg'
32
+ country_code = request.path.split("/")[-1]&.downcase
32
33
  return head :unauthorized unless country_code.present?
33
34
 
34
35
  webhook_secret = NinjaVanApi.configuration.get_webhook_secret(country_code)
@@ -38,6 +38,10 @@ module NinjaVanApi
38
38
  @orders ||= OrderResource.new(self)
39
39
  end
40
40
 
41
+ def waybills
42
+ @waybills ||= WaybillResource.new(self)
43
+ end
44
+
41
45
  private
42
46
 
43
47
  def validate_country_code
@@ -55,7 +59,7 @@ module NinjaVanApi
55
59
  def access_token
56
60
  fetch_access_token if token_expired?
57
61
 
58
- if defined?(Rails) && Rails.respond_to?(:cache)
62
+ if defined?(Rails) && Rails.respond_to?(:cache) && Rails.cache
59
63
  Rails.cache.read(cache_key)["access_token"]
60
64
  else
61
65
  @token_info["access_token"]
@@ -64,7 +68,7 @@ module NinjaVanApi
64
68
 
65
69
  def refresh_access_token
66
70
  @token_info = nil
67
- Rails.cache.delete(cache_key) if defined?(Rails) && Rails.respond_to?(:cache)
71
+ Rails.cache.delete(cache_key) if defined?(Rails) && Rails.respond_to?(:cache) && Rails.cache
68
72
  fetch_access_token
69
73
  end
70
74
 
@@ -99,7 +103,7 @@ module NinjaVanApi
99
103
  def access_token
100
104
  fetch_access_token if token_expired?
101
105
 
102
- if defined?(Rails) && Rails.respond_to?(:cache)
106
+ if defined?(Rails) && Rails.respond_to?(:cache) && Rails.cache
103
107
  Rails.cache.read(cache_key)["access_token"]
104
108
  else
105
109
  @token_info["access_token"]
@@ -108,7 +112,7 @@ module NinjaVanApi
108
112
 
109
113
  def token_expired?
110
114
  token_info =
111
- if defined?(Rails) && Rails.respond_to?(:cache)
115
+ if defined?(Rails) && Rails.respond_to?(:cache) && Rails.cache
112
116
  Rails.cache.read(cache_key)
113
117
  else
114
118
  @token_info
@@ -125,7 +129,7 @@ module NinjaVanApi
125
129
 
126
130
  token_info = JSON.parse(response.body)
127
131
 
128
- if defined?(Rails) && Rails.respond_to?(:cache)
132
+ if defined?(Rails) && Rails.respond_to?(:cache) && Rails.cache
129
133
  Rails.cache.write(cache_key, token_info, expires_in: token_info["expires_in"])
130
134
  else
131
135
  @token_info = token_info
@@ -10,11 +10,11 @@ module NinjaVanApi
10
10
  attr_accessor :webhook_job_class
11
11
 
12
12
  def webhook_secrets=(secrets)
13
- @webhook_secrets = secrets.transform_keys(&:downcase)
13
+ @webhook_secrets = secrets.transform_keys { |key| key.to_sym.downcase }
14
14
  end
15
15
 
16
16
  def get_webhook_secret(country_code)
17
- @webhook_secrets[country_code.to_s.downcase]
17
+ @webhook_secrets[country_code.to_sym.downcase]
18
18
  end
19
19
  end
20
20
 
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NinjaVanApi
4
+ class Waybill < Base
5
+ attr_reader :pdf
6
+
7
+ def initialize(content)
8
+ @pdf = content
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NinjaVanApi
4
+ class WaybillResource < BaseResource
5
+ # Retrieves a waybill for a given tracking number
6
+ # @param tracking_number [String] The tracking number as generated by the Order API
7
+ # @param hide_shipper_details [Boolean] A flag for hiding shipper's details
8
+ # @param orientation [String] Specifies the orientation of the generated waybill ("portrait" or "landscape")
9
+ # @return [Waybill] The waybill object
10
+ def get(tracking_number, hide_shipper_details: nil, orientation: nil)
11
+ params = { tid: tracking_number }
12
+ params[:hide_shipper_details] = hide_shipper_details unless hide_shipper_details.nil?
13
+ params[:orientation] = orientation if orientation
14
+
15
+ response = get_request("2.0/reports/waybill", params: params)
16
+ Waybill.new(response.body)
17
+ end
18
+ end
19
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NinjaVanApi
4
- VERSION = "0.2.20"
4
+ VERSION = "0.3.1"
5
5
  end
data/lib/ninja_van_api.rb CHANGED
@@ -14,10 +14,12 @@ module NinjaVanApi
14
14
  # Objects
15
15
  autoload :Base, "ninja_van_api/objects/base"
16
16
  autoload :Order, "ninja_van_api/objects/order"
17
+ autoload :Waybill, "ninja_van_api/objects/waybill"
17
18
 
18
19
  # Resources
19
20
  autoload :BaseResource, "ninja_van_api/resources/base_resource"
20
21
  autoload :OrderResource, "ninja_van_api/resources/order_resource"
22
+ autoload :WaybillResource, "ninja_van_api/resources/waybill_resource"
21
23
 
22
24
  # Core components
23
25
  autoload :Client, "ninja_van_api/client"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ninja_van_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.20
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jane Trang Mai Nguyen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-02-27 00:00:00.000000000 Z
11
+ date: 2025-02-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -144,8 +144,10 @@ files:
144
144
  - lib/ninja_van_api/error.rb
145
145
  - lib/ninja_van_api/objects/base.rb
146
146
  - lib/ninja_van_api/objects/order.rb
147
+ - lib/ninja_van_api/objects/waybill.rb
147
148
  - lib/ninja_van_api/resources/base_resource.rb
148
149
  - lib/ninja_van_api/resources/order_resource.rb
150
+ - lib/ninja_van_api/resources/waybill_resource.rb
149
151
  - lib/ninja_van_api/version.rb
150
152
  homepage: https://github.com/Postco/ninja_van_api
151
153
  licenses: