module DHLEcommerceAPI class Tracking < Base # example_tracking_params = { # "e_pod_required": "Y", # "trackingReferenceNumber": [ # "MYPTC00012", "MYPTC00013" # ] # } self.prefix = "/rest/v3/Tracking" self.element_name = "" def initialize(attributes = {}, persisted = false) attributes = account_ids.merge(attributes) super end def self.find(arguments) tracking = self.new({ e_pod_required: "Y", tracking_reference_number: arguments.is_a?(Array) ? arguments : [arguments] }) tracking.save return tracking.shipment_items.presence || [] end def create run_callbacks :create do connection.post(collection_path, formatted_request_data(request_data), self.class.headers).tap do |response| load_attributes_from_response(response) end end end def load_attributes_from_response(response) if response_code_allows_body?(response.code.to_i) && (response["Content-Length"].nil? || response["Content-Length"] != "0") && !response.body.nil? && response.body.strip.size > 0 bd = self.class.format.decode(response.body)["bd"] response_status = bd["responseStatus"] code = response_status["code"] if code == "200" @persisted = true elsif code == "204" # handle partial success @persisted = false else error_messages = response_status["messageDetails"].map{|err| err["messageDetail"]} handle_errors(code, error_messages) @persisted = false end new_attributes = attributes.merge(bd) load(new_attributes, true, @persisted) end end def request_data { track_item_request: { hdr: headers, bd: attributes.except("response_status") # dont send responseStatus } } end def headers { message_type: "TRACKITEM", message_date_time: DateTime.now.to_s, access_token: DHLEcommerceAPI::Authentication.get_token, message_version: "1.0" } end # Since request_data isnt the same as object attributes. # We have to write our own method to format the request data def formatted_request_data(request_data) request_data.as_json .deep_transform_keys do |key| custom_key_format(key) # method from Base end.to_json end end end