dhl_ecommerce_api 0.1.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.
@@ -0,0 +1,90 @@
1
+ module DHLEcommerceAPI
2
+ class Pickup < Base
3
+ # This is a one-off pickup request.
4
+ # It simply tells DHL to pickup items from the shipper_details address.
5
+ # Used in conjunction with the Shipment::Dropoff class.
6
+
7
+ self.prefix = "/rest/v2/Order/Shipment/Pickup"
8
+ self.element_name = ""
9
+
10
+ def create
11
+ run_callbacks :create do
12
+ connection.post(collection_path, formatted_request_data(pickup_request), self.class.headers).tap do |response|
13
+ load_attributes_from_response(response)
14
+ end
15
+ end
16
+ end
17
+
18
+ def load_attributes_from_response(response)
19
+ if response_code_allows_body?(response.code.to_i) &&
20
+ (response["Content-Length"].nil? || response["Content-Length"] != "0") &&
21
+ !response.body.nil? && response.body.strip.size > 0
22
+
23
+ bd = self.class.format.decode(response.body)["bd"]
24
+ response_status = bd["responseStatus"]
25
+ code = response_status["code"]
26
+
27
+ if code == "200"
28
+ @persisted = true
29
+ elsif code == "204"
30
+ # handle partial success
31
+ @persisted = false
32
+ else
33
+ error_messages = response_status["messageDetails"].map{|err| err["messageDetail"]}
34
+ handle_errors(code, error_messages)
35
+ @persisted = false
36
+ end
37
+
38
+ new_attributes = attributes.merge(bd)
39
+ load(new_attributes, true, @persisted)
40
+ end
41
+ end
42
+
43
+ def pickup_request
44
+ {
45
+ pickup_request: {
46
+ hdr: headers,
47
+ bd: attributes_with_account_ids.deep_transform_keys {|key| key.to_s.underscore.to_sym }
48
+ }
49
+ }
50
+ end
51
+
52
+ def headers
53
+ {
54
+ message_type: "PICKUP",
55
+ message_date_time: DateTime.now.to_s,
56
+ access_token: DHLEcommerceAPI::Authentication.get_token,
57
+ message_version: "1.2"
58
+ }
59
+ end
60
+ end
61
+ end
62
+
63
+ # example_pickup_params = {
64
+ # "handover_items": [
65
+ # {
66
+ # "pickup_date": "2022-03-09",
67
+ # "pickup_start_time": "09:00",
68
+ # "pickup_end_time": "18:00",
69
+ # "shipment_type": "1",
70
+ # "notification_email": nil,
71
+ # "shipper_details": {
72
+ # "company": "PostCo",
73
+ # "name": "PostCo",
74
+ # "email_id": nil,
75
+ # "phone_number": "0169822645",
76
+ # "address_line_1": "no 26 jalan 31/123, petaling jaya",
77
+ # "address_line_2": nil,
78
+ # "address_line_3": nil,
79
+ # "city": "Kuala Lumpur",
80
+ # "state": "Kuala Lumpur",
81
+ # "postal_code": "57000",
82
+ # "country": "MY",
83
+ # },
84
+ # "shipments": {
85
+ # "quantity": 1,
86
+ # "totalWeight": 100
87
+ # }
88
+ # }
89
+ # ]
90
+ # }
@@ -0,0 +1,50 @@
1
+ module DHLEcommerceAPI
2
+ class Shipment::Dropoff < Shipment
3
+ # This creates a Shipment ONLY.
4
+ # DHL will expect the item to be dropped off at one of their locations.
5
+ # Used in conjunction with the Pickup class.
6
+
7
+ self.element_name = ""
8
+
9
+ DEFAULT_ATTRIBUTES = {
10
+ handover_method: 1,
11
+ shipment_items: []
12
+ }
13
+ end
14
+ end
15
+
16
+ # shipment_with_dropoff_params = {
17
+ # "shipment_items" => [
18
+ # {
19
+ # "consignee_address" => {
20
+ # "company_name" => "Test",
21
+ # "name" => "Test1",
22
+ # "address1" => "NO 3 JALAN PPU 1",
23
+ # "address2" => "TAMAN PERINDUSTRIAN PUCHONG UTAMA",
24
+ # "address3" => nil,
25
+ # "city" => "PUCHONG",
26
+ # "state" => "SELANGOR",
27
+ # "district" => nil,
28
+ # "country" => "MY",
29
+ # "post_code" => "57000",
30
+ # "phone" => "0123456798",
31
+ # "email" => nil
32
+ # },
33
+ # "shipment_id" => "MYPTC000103",
34
+ # "package_desc" => "Bread Materials",
35
+ # "total_weight" => 2000,
36
+ # "total_weight_uom" => "G",
37
+ # "dimension_uom" => "CM",
38
+ # "height" => nil,
39
+ # "length" => nil,
40
+ # "width" => nil,
41
+ # "product_code" => "PDO",
42
+ # "cod_value" => nil,
43
+ # "insurance_value" => nil,
44
+ # "total_value" => 300,
45
+ # "currency" => "MYR",
46
+ # "remarks" => nil,
47
+ # "is_routing_info_required" => "Y"
48
+ # }
49
+ # ],
50
+ # }
@@ -0,0 +1,79 @@
1
+ module DHLEcommerceAPI
2
+ class Shipment::Pickup < Shipment
3
+ # This is a coupled shipment + pickup request.
4
+
5
+ self.element_name = ""
6
+
7
+ DEFAULT_ATTRIBUTES = {
8
+ handover_method: 2,
9
+ pickup_date_time: nil,
10
+ pickup_address: {
11
+ company_name: "",
12
+ name: "",
13
+ address1: "",
14
+ address2: nil,
15
+ address3: nil,
16
+ city: "",
17
+ state: "",
18
+ post_code: "",
19
+ country: "MY",
20
+ phone: "",
21
+ email: nil
22
+ },
23
+ shipment_items: []
24
+ }
25
+
26
+ validates_presence_of :pickup_date_time
27
+ end
28
+ end
29
+
30
+ # shipment_with_pickup_params = {
31
+ # "handoverMethod": 2,
32
+ # "pickupDateTime": DateTime.now.to_s,
33
+ # "pickupAddress": {
34
+ # "companyName": "Pickup From Company",
35
+ # "name": "Pickup From Name",
36
+ # "address1": "Holistic Pharmacy PostCo, 55, Jalan Landak",
37
+ # "address2": "",
38
+ # "address3": "",
39
+ # "city": " Kuala Lumpur",
40
+ # "state": " Kuala Lumpur",
41
+ # "postCode": "55100",
42
+ # "country": "MY",
43
+ # "phone": "0169822645",
44
+ # "email": "erwhey@postco.co"
45
+ # },
46
+ # "shipmentItems": [
47
+ # {
48
+ # "shipmentID": "MYPTC0087",
49
+ # "packageDesc": "Laptop Sleeve",
50
+ # "totalWeight": 500,
51
+ # "totalWeightUOM": "G",
52
+ # "dimensionUOM": "CM",
53
+ # "height": nil,
54
+ # "length": nil,
55
+ # "width": nil,
56
+ # "productCode": "PDO",
57
+ # "codValue": nil,
58
+ # "insuranceValue": nil,
59
+ # "totalValue": 300,
60
+ # "currency": "MYR",
61
+ # "remarks": nil,
62
+ # "isRoutingInfoRequired": "Y",
63
+ # "consigneeAddress": {
64
+ # "companyName": "Sleeve Company",
65
+ # "name": "Sleeve Sdn Bhd",
66
+ # "address1": "No. 3, Jalan Bangsar, Kampung Haji Abdullah Hukum",
67
+ # "address2": nil,
68
+ # "address3": nil,
69
+ # "city": "Kuala Lumpur",
70
+ # "state": "Kuala Lumpur",
71
+ # "district": nil,
72
+ # "country": "MY",
73
+ # "postCode": "59200",
74
+ # "phone": "0169822645",
75
+ # "email": nil
76
+ # }
77
+ # },
78
+ # ]
79
+ # }
@@ -0,0 +1,46 @@
1
+ module DHLEcommerceAPI
2
+ # Component item
3
+ class Shipment::ShipmentItem < Base
4
+ # add some validations?
5
+ DEFAULT_ATTRIBUTES = {
6
+ shipment_id: nil,
7
+ package_desc: "",
8
+ total_weight: nil,
9
+ total_weight_uom: "G",
10
+ dimension_uom: "CM",
11
+ height: nil,
12
+ length: nil,
13
+ width: nil,
14
+ product_code: "PDO",
15
+ cod_value: nil,
16
+ insurance_value: nil,
17
+ total_value: 300,
18
+ currency: "MYR",
19
+ remarks: nil,
20
+ is_routing_info_required: "Y",
21
+ consignee_address: {
22
+ company_name: "",
23
+ name: "",
24
+ address1: "",
25
+ address2: nil,
26
+ address3: nil,
27
+ city: "",
28
+ state: "",
29
+ district: nil,
30
+ country: "MY",
31
+ post_code: "",
32
+ phone: "",
33
+ email: nil
34
+ }
35
+ }
36
+
37
+ def initialize(attributes = {}, persisted = false)
38
+ status = attributes["response_status"]
39
+ if status.present? && status["code"] != "200"
40
+ error_messages = status["message_details"].map{|err| err["message_detail"]}
41
+ handle_errors(status["code"], error_messages)
42
+ end
43
+ super
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,154 @@
1
+ module DHLEcommerceAPI
2
+ class Shipment < Base
3
+ # Base Shipment class
4
+ # info returned - delivery_confirmation_no, delivery_depot_code, primary_sort_code, secondary_sort_code
5
+
6
+ self.prefix = "/rest/v3/Shipment"
7
+ self.element_name = ""
8
+
9
+ has_many :shipment_items, class_name: "DHLEcommerceAPI::Shipment::ShipmentItem"
10
+
11
+ validates_presence_of :handover_method
12
+
13
+ def create
14
+ run_callbacks :create do
15
+ data = formatted_request_data(manifest_request)
16
+ connection.post(collection_path, data, self.class.headers).tap do |response|
17
+ load_attributes_from_response(response)
18
+ end
19
+ end
20
+ end
21
+
22
+ def load_attributes_from_response(response)
23
+ if response_code_allows_body?(response.code.to_i) &&
24
+ (response["Content-Length"].nil? || response["Content-Length"] != "0") &&
25
+ !response.body.nil? && response.body.strip.size > 0
26
+
27
+ bd = self.class.format.decode(response.body)["bd"]
28
+ response_status = bd["responseStatus"]
29
+ code = response_status["code"]
30
+
31
+ if code == "200"
32
+ @persisted = true
33
+ elsif code == "204"
34
+ # handle partial success
35
+ @persisted = false
36
+ else
37
+ error_messages = response_status["messageDetails"].map{|err| err["messageDetail"]}
38
+ handle_errors(code, error_messages)
39
+ @persisted = false
40
+ end
41
+
42
+ new_attributes = attributes.merge(bd)
43
+ load(new_attributes, true, @persisted)
44
+ end
45
+ end
46
+
47
+ def manifest_request
48
+ {
49
+ manifest_request: {
50
+ hdr: headers,
51
+ bd: attributes_with_account_ids.deep_transform_keys {|key| key.to_s.underscore.to_sym }
52
+ }
53
+ }
54
+ end
55
+
56
+ def headers
57
+ {
58
+ message_type: "SHIPMENT",
59
+ message_date_time: DateTime.now.to_s,
60
+ access_token: DHLEcommerceAPI::Authentication.get_token,
61
+ message_version: "1.0"
62
+ }
63
+ end
64
+ end
65
+ end
66
+
67
+ # Examples:
68
+ # shipment_with_pickup_params = {
69
+ # "handoverMethod": 2,
70
+ # "pickupDateTime": DateTime.now.to_s,
71
+ # "pickupAddress": {
72
+ # "companyName": "Pickup From Company",
73
+ # "name": "Pickup From Name",
74
+ # "address1": "Holistic Pharmacy PostCo, 55, Jalan Landak",
75
+ # "address2": "",
76
+ # "address3": "",
77
+ # "city": " Kuala Lumpur",
78
+ # "state": " Kuala Lumpur",
79
+ # "postCode": "55100",
80
+ # "country": "MY",
81
+ # "phone": "0169822645",
82
+ # "email": "erwhey@postco.co"
83
+ # },
84
+ # "shipmentItems": [
85
+ # {
86
+ # "shipmentID": "MYPTC0083",
87
+ # "packageDesc": "Laptop Sleeve",
88
+ # "totalWeight": 500,
89
+ # "totalWeightUOM": "G",
90
+ # "dimensionUOM": "CM",
91
+ # "height": nil,
92
+ # "length": nil,
93
+ # "width": nil,
94
+ # "productCode": "PDO",
95
+ # "codValue": nil,
96
+ # "insuranceValue": nil,
97
+ # "totalValue": 300,
98
+ # "currency": "MYR",
99
+ # "remarks": nil,
100
+ # "isRoutingInfoRequired": "Y",
101
+ # "consigneeAddress": {
102
+ # "companyName": "Sleeve Company",
103
+ # "name": "Sleeve Sdn Bhd",
104
+ # "address1": "No. 3, Jalan Bangsar, Kampung Haji Abdullah Hukum",
105
+ # "address2": nil,
106
+ # "address3": nil,
107
+ # "city": "Kuala Lumpur",
108
+ # "state": "Kuala Lumpur",
109
+ # "district": nil,
110
+ # "country": "MY",
111
+ # "postCode": "59200",
112
+ # "phone": "0169822645",
113
+ # "email": nil
114
+ # }
115
+ # },
116
+ # ]
117
+ # }
118
+
119
+ # shipment_with_dropoff_params = {
120
+ # "handover_method" => 1,
121
+ # "shipment_items" => [
122
+ # {
123
+ # "consignee_address" => {
124
+ # "company_name" => "Test",
125
+ # "name" => "Test1",
126
+ # "address1" => "NO 3 JALAN PPU 1",
127
+ # "address2" => "TAMAN PERINDUSTRIAN PUCHONG UTAMA",
128
+ # "address3" => nil,
129
+ # "city" => "PUCHONG",
130
+ # "state" => "SELANGOR",
131
+ # "district" => nil,
132
+ # "country" => "MY",
133
+ # "post_code" => "57000",
134
+ # "phone" => "0123456798",
135
+ # "email" => nil
136
+ # },
137
+ # "shipment_id" => "MYPTC000102",
138
+ # "package_desc" => "Bread Materials",
139
+ # "total_weight" => 2000,
140
+ # "total_weight_uom" => "G",
141
+ # "dimension_uom" => "CM",
142
+ # "height" => nil,
143
+ # "length" => nil,
144
+ # "width" => nil,
145
+ # "product_code" => "PDO",
146
+ # "cod_value" => nil,
147
+ # "insurance_value" => nil,
148
+ # "total_value" => 300,
149
+ # "currency" => "MYR",
150
+ # "remarks" => nil,
151
+ # "is_routing_info_required" => "Y"
152
+ # }
153
+ # ],
154
+ # }
@@ -0,0 +1,88 @@
1
+ module DHLEcommerceAPI
2
+ class Tracking < Base
3
+ # example_tracking_params = {
4
+ # "e_pod_required": "Y",
5
+ # "trackingReferenceNumber": [
6
+ # "MYPTC00012", "MYPTC00013"
7
+ # ]
8
+ # }
9
+
10
+ self.prefix = "/rest/v3/Tracking"
11
+ self.element_name = ""
12
+
13
+ def initialize(attributes = {}, persisted = false)
14
+ attributes = account_ids.merge(attributes)
15
+ super
16
+ end
17
+
18
+ def self.find(arguments)
19
+ tracking = self.new({
20
+ e_pod_required: "Y",
21
+ tracking_reference_number: arguments.is_a?(Array) ? arguments : [arguments]
22
+ })
23
+ tracking.save
24
+ return tracking.shipment_items.presence || []
25
+ end
26
+
27
+ def create
28
+ run_callbacks :create do
29
+ connection.post(collection_path, formatted_request_data(request_data), self.class.headers).tap do |response|
30
+ load_attributes_from_response(response)
31
+ end
32
+ end
33
+ end
34
+
35
+ def load_attributes_from_response(response)
36
+ if response_code_allows_body?(response.code.to_i) &&
37
+ (response["Content-Length"].nil? || response["Content-Length"] != "0") &&
38
+ !response.body.nil? && response.body.strip.size > 0
39
+
40
+ bd = self.class.format.decode(response.body)["bd"]
41
+ response_status = bd["responseStatus"]
42
+ code = response_status["code"]
43
+
44
+ if code == "200"
45
+ @persisted = true
46
+ elsif code == "204"
47
+ # handle partial success
48
+ @persisted = false
49
+ else
50
+ error_messages = response_status["messageDetails"].map{|err| err["messageDetail"]}
51
+ handle_errors(code, error_messages)
52
+ @persisted = false
53
+ end
54
+
55
+ new_attributes = attributes.merge(bd)
56
+ load(new_attributes, true, @persisted)
57
+ end
58
+ end
59
+
60
+ def request_data
61
+ {
62
+ track_item_request: {
63
+ hdr: headers,
64
+ bd: attributes.except("response_status") # dont send responseStatus
65
+ }
66
+ }
67
+ end
68
+
69
+ def headers
70
+ {
71
+ message_type: "TRACKITEM",
72
+ message_date_time: DateTime.now.to_s,
73
+ access_token: DHLEcommerceAPI::Authentication.get_token,
74
+ message_version: "1.0"
75
+ }
76
+ end
77
+
78
+
79
+ # Since request_data isnt the same as object attributes.
80
+ # We have to write our own method to format the request data
81
+ def formatted_request_data(request_data)
82
+ request_data.as_json
83
+ .deep_transform_keys do |key|
84
+ custom_key_format(key) # method from Base
85
+ end.to_json
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DHLEcommerceAPI
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "dhl_ecommerce_api/version"
4
+ require "active_resource"
5
+
6
+ module DHLEcommerceAPI
7
+ require "dhl_ecommerce_api/cache"
8
+ require "dhl_ecommerce_api/configuration"
9
+ require "dhl_ecommerce_api/connection"
10
+
11
+ require "dhl_ecommerce_api/resources/base"
12
+ require "dhl_ecommerce_api/resources/authentication"
13
+
14
+ require "dhl_ecommerce_api/resources/shipment"
15
+ require "dhl_ecommerce_api/resources/pickup"
16
+
17
+ require "dhl_ecommerce_api/resources/tracking"
18
+ end
@@ -0,0 +1,4 @@
1
+ module DHLEcommerceAPI
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end