Class: DHLEcommerceAPI::Pickup

Inherits:
Base
  • Object
show all
Defined in:
lib/dhl_ecommerce_api/resources/pickup.rb

Instance Method Summary collapse

Methods inherited from Base

#account_ids, #attributes_with_account_ids, #custom_key_format, #handle_errors

Instance Method Details

#createObject



10
11
12
13
14
15
16
17
# File 'lib/dhl_ecommerce_api/resources/pickup.rb', line 10

def create
  run_callbacks :create do
    connection.post(collection_path, pickup_request.to_json, self.class.headers).tap do |response|
      self.id = id_from_response(response)
      load_attributes_from_response(response)
    end
  end
end

#headersObject



54
55
56
57
58
59
60
61
# File 'lib/dhl_ecommerce_api/resources/pickup.rb', line 54

def headers
  {
    "messageType": "PICKUP",
    "messageDateTime": DateTime.now.to_s,
    "accessToken": DHLEcommerceAPI::Authentication.get_token,
    "messageVersion": "1.2"
  }
end

#load_attributes_from_response(response) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/dhl_ecommerce_api/resources/pickup.rb', line 19

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({response: JSON.parse(response.body)})
  
    load(new_attributes, true, @persisted)
  end
end

#pickup_requestObject



45
46
47
48
49
50
51
52
# File 'lib/dhl_ecommerce_api/resources/pickup.rb', line 45

def pickup_request
  {
    "pickupRequest": {
      "hdr": headers,
      "bd": 
    }
  }
end