Class: EShipper::Order

Inherits:
EShipperResponse show all
Defined in:
lib/eshipper/classes/order.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from EShipperResponse

decode

Class Method Details

.search(options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/eshipper/classes/order.rb', line 5

def self.search(options = {})
  orders = SearchReply.search(options).OrderSearchReply

  if orders == "\n" # No results
    []
  elsif orders.Order.is_a?(Array) # Multiple results
    orders.Order
  else # Single result
    [orders.Order]
  end
end

Instance Method Details

#fetchObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/eshipper/classes/order.rb', line 29

def fetch
  order_information_reply = OrderInformationReply.fetch(fetch_options)

  if order_information_reply.respond_to?(:OrderInformationReply)
    order_information_reply = order_information_reply.OrderInformationReply
    if order_information_reply.Order.is_a?(Array)
      # Sometimes there is a partial referenceCode match. In that case the API will return multiple Orders
      # However, the way they format the Orders, the properties of all orders are siblings.
      # In other words, the properties of an Order are NOT nested within the Order.
      # So the way the parser works is that it gives us arrays for each property.
      # TBD: Are properties of each Order ordered consistently?
      # TBD: Does the API always return the better matching result first???
      # Assume that the first result is the one we want!
      this_index = order_information_reply.Order.each_with_index.find do |_, index|
        order_information_reply.Order[index].id == self.Id
      end[1]
      order_information_reply.attributes.keys.each do |attribute_key|
        order_information_reply.send("#{attribute_key}=", order_information_reply.attributes[attribute_key][this_index])
      end
      throw("id mismatch") if order_information_reply.Order.id != self.Id
    end
    order_information_reply
  else
    # TODO: error handling
    throw(order_information_reply)
  end
end

#fetch_optionsObject



57
58
59
# File 'lib/eshipper/classes/order.rb', line 57

def fetch_options
  { order_id: self.Id, referenceCode: references.first&.Code }
end

#referencesObject



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/eshipper/classes/order.rb', line 17

def references
  if !respond_to?(:References)
    []
  elsif self.References.Reference == "\n"
    []
  elsif self.References.Reference.is_a?(Array)
    self.References.Reference
  else
    [self.References.Reference]
  end
end