ninja_van_api 0.3.0 → 0.3.2
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 +32 -0
- data/app/controllers/ninja_van_api/webhooks_controller.rb +4 -3
- data/lib/ninja_van_api/client.rb +0 -11
- data/lib/ninja_van_api/configuration.rb +2 -2
- data/lib/ninja_van_api/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3c0500f6e4ff9f487125e31177607ea5540d449090baaf969074343652c3f685
|
4
|
+
data.tar.gz: 0d4df61f525236b14a729e2e28cd10a5bdfc05774c0e86db5a0c19226048739b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 75d348ca0a519c74708855d372cb96a117caa233026eda42c6cb6a8c97051315de8dbc3f51d1fb86f96b12504b7fa48ceb59255f0ca6c4927b074ceb6aff0f95
|
7
|
+
data.tar.gz: cd320d7a2d2dc7c8bf7b4452f83c23ceebd26fae8f82cf8cb7fa9b7dfa8a0fb92930667a52f02ec2e9cfe5697f7228c70734db922ec35adf4a4c065e3ed49e84
|
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
|
-
|
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: /
|
31
|
-
country_code = request.path.split("/")[
|
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)
|
data/lib/ninja_van_api/client.rb
CHANGED
@@ -86,17 +86,6 @@ module NinjaVanApi
|
|
86
86
|
}.to_json
|
87
87
|
end
|
88
88
|
|
89
|
-
raise NinjaVanApi::AuthenticationError, response.body unless response.success?
|
90
|
-
|
91
|
-
JSON.parse(response.body)["access_token"]
|
92
|
-
|
93
|
-
response =
|
94
|
-
Faraday.post(endpoint) do |req|
|
95
|
-
req.headers["Content-Type"] = "application/json"
|
96
|
-
req.headers["Accept"] = "application/json"
|
97
|
-
req.body = { client_id: @client_id, client_secret: @client_secret, grant_type: "client_credentials" }.to_json
|
98
|
-
end
|
99
|
-
|
100
89
|
handle_access_token_response(response)
|
101
90
|
end
|
102
91
|
|
@@ -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
|
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.
|
17
|
+
@webhook_secrets[country_code.to_sym.downcase]
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|