Class: Shipvine::InboundShipment

Inherits:
Base
  • Object
show all
Defined in:
lib/shipvine/inbound_shipment.rb

Constant Summary

Constants inherited from Base

Base::WAREHOUSE_XSD_FILE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

client, xml_to_hash

Constructor Details

#initialize(attributes) ⇒ InboundShipment

Returns a new instance of InboundShipment.



6
7
8
# File 'lib/shipvine/inbound_shipment.rb', line 6

def initialize(attributes)
  @attributes = attributes
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



4
5
6
# File 'lib/shipvine/inbound_shipment.rb', line 4

def attributes
  @attributes
end

Class Method Details

.get(merchant_identifier) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/shipvine/inbound_shipment.rb', line 33

def self.get(merchant_identifier)
  request = self.client.request(
    :get,
    '/inbound-shipments/' + ERB::Util.url_encode(Shipvine.merchant_code) + '/' + ERB::Util.url_encode(merchant_identifier)
  )

  self.new(self.xml_to_hash(request.body))
end

.list(since, state = 'Complete') ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/shipvine/inbound_shipment.rb', line 10

def self.list(since, state = 'Complete')
  request = self.client.request(
    :get,
    '/inbound-shipments/' + ERB::Util.url_encode(Shipvine.merchant_code),
    {
      'changed-since' => since,
      'state' => state
    }
  )

  response = self.xml_to_hash(request.body)

  # the GET response only returns the identifiers, we have to make another call
  # to pull all of the actual inbound shipments
  merchant_identifiers = Array.wrap(response.dig(:inbound_shipments, :inbound_shipment)).map do |shipment_hash|
    shipment_hash[:merchant_identifier]
  end

  merchant_identifiers.map do |identifier|
    self.get(identifier)
  end
end

Instance Method Details

#createObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/shipvine/inbound_shipment.rb', line 42

def create
  translated_payload = @attributes.deep_dup
  merchant_identifier = translated_payload.delete(:merchant_identifier)

  # `The /InboundShipment/ExpectedAt date is not in the expected format of yyyy-MM-dd (e.g., '2014-02-28')`
  if translated_payload[:expected_at] !=~ /[0-9]{4}-[0-2]{2}-[0-9]{2}/
    translated_payload[:expected_at] = DateTime.parse(translated_payload[:expected_at]).strftime('%Y-%m-%d')
  end

  preprocess_lines(translated_payload)
  (translated_payload)

  client.request(
    :put,
    '/inbound-shipments/' + ERB::Util.url_encode(Shipvine.merchant_code) + '/' + ERB::Util.url_encode(merchant_identifier),
    request_body('InboundShipment', translated_payload)
  )
end

#metadataObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/shipvine/inbound_shipment.rb', line 61

def 
   = self.attributes.dig(:inbound_shipment, :shipment_metadata, :metadata)

  if .nil? || .empty?
    return {}
  end

  # TODO cache the resulting hash here

   = Array.wrap()

  .inject({}) do |h, |
    h[[:name]] = [:value]
    h
  end
end