Class: Hushed::Documents::Request::ShipmentOrder

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Document
Defined in:
lib/hushed/documents/request/shipment_order.rb

Defined Under Namespace

Classes: MissingClientError, MissingOrderError

Constant Summary collapse

NAMESPACE =
"http://schemas.quietlogistics.com/V2/ShipmentOrder.xsd"
DATEFORMAT =
"%Y%m%d_%H%M%S"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Document

#warehouse

Constructor Details

#initialize(options = {}) ⇒ ShipmentOrder

Returns a new instance of ShipmentOrder.



19
20
21
22
23
24
# File 'lib/hushed/documents/request/shipment_order.rb', line 19

def initialize(options = {})
  @order = options[:order] || raise(MissingOrderError.new("order cannot be missing"))
  @client = options[:client] || raise(MissingClientError.new("client cannot be missing"))
  @carrier = options[:carrier]
  @service_level = options[:service_level]
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



15
16
17
# File 'lib/hushed/documents/request/shipment_order.rb', line 15

def client
  @client
end

#orderObject (readonly)

Returns the value of attribute order.



15
16
17
# File 'lib/hushed/documents/request/shipment_order.rb', line 15

def order
  @order
end

Instance Method Details

#address_attributes(address) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/hushed/documents/request/shipment_order.rb', line 86

def address_attributes(address)
  {
    Company: address.company, Contact: address.name, Address1: address.address1, Address2: address.address2,
    City: address.city, State: address.province_code, PostalCode: address.zip, Country: address.country_code,
    Email: @order.email
  }
end

#attach_order_details(xml, line_items) ⇒ Object



48
49
50
51
52
# File 'lib/hushed/documents/request/shipment_order.rb', line 48

def attach_order_details(xml, line_items)
  line_items.each.with_index(1) do |line_item, index|
    xml.OrderDetails(line_item_attributes(line_item, index))
  end
end

#attach_order_header(xml, header_attributes, order) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/hushed/documents/request/shipment_order.rb', line 38

def attach_order_header(xml, header_attributes, order)
  xml.OrderHeader(order_header_attributes) do
    xml.Comments order.note
    xml.ShipMode(shipping_attributes(order.shipping_lines.first))
    xml.ShipTo(address_attributes(order.shipping_address))
    xml.BillTo(address_attributes(order.billing_address))
    xml.DeclaredValue order.total_price
  end
end

#dateObject



70
71
72
# File 'lib/hushed/documents/request/shipment_order.rb', line 70

def date
  @order.created_at.utc
end

#document_numberObject



62
63
64
# File 'lib/hushed/documents/request/shipment_order.rb', line 62

def document_number
  @order.id
end

#filenameObject



74
75
76
# File 'lib/hushed/documents/request/shipment_order.rb', line 74

def filename
  "#{business_unit}_#{type}_#{document_number}_#{date.strftime(DATEFORMAT)}.xml"
end

#line_item_attributes(line_item, line_number) ⇒ Object



94
95
96
97
98
99
# File 'lib/hushed/documents/request/shipment_order.rb', line 94

def line_item_attributes(line_item, line_number)
  {
    ItemNumber: line_item.id, Line: line_number, QuantityOrdered: line_item.quantity,
    QuantityToShip: line_item.quantity, UOM: line_item.unit_of_measure, Price: line_item.price
  }
end

#message_idObject



58
59
60
# File 'lib/hushed/documents/request/shipment_order.rb', line 58

def message_id
  document_number
end

#order_header_attributesObject



78
79
80
# File 'lib/hushed/documents/request/shipment_order.rb', line 78

def order_header_attributes
  {OrderNumber: document_number, OrderType: order_type, OrderDate: date.iso8601, ShipDate: date.iso8601}
end

#order_typeObject



66
67
68
# File 'lib/hushed/documents/request/shipment_order.rb', line 66

def order_type
  'SO'
end

#shipping_attributes(shipping_line) ⇒ Object



82
83
84
# File 'lib/hushed/documents/request/shipment_order.rb', line 82

def shipping_attributes(shipping_line)
  {Carrier: @carrier || shipping_line.carrier, ServiceLevel: @service_level || shipping_line.service_level}
end

#to_xmlObject



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/hushed/documents/request/shipment_order.rb', line 26

def to_xml
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.ShipOrderDocument(xmlns: NAMESPACE) do
      xml.ClientID client_id
      xml.BusinessUnit business_unit
      attach_order_header(xml, order_header_attributes, @order)
      attach_order_details(xml, @order.line_items)
    end
  end
  builder.to_xml
end

#typeObject



54
55
56
# File 'lib/hushed/documents/request/shipment_order.rb', line 54

def type
  'ShipmentOrder'
end