Class: GunAccessorySupply::Order

Inherits:
Base
  • Object
show all
Defined in:
lib/gun_accessory_supply/order.rb

Overview

To submit an order:

See each method for a list of required options.

Instance Method Summary collapse

Methods inherited from Base

connect

Constructor Details

#initialize(options = {}) ⇒ Order

Returns a new instance of Order.

Options Hash (options):

  • :username (String)

    required



12
13
14
15
16
17
18
19
# File 'lib/gun_accessory_supply/order.rb', line 12

def initialize(options = {})
  requires!(options, :username, :password, :po_number, :ship_to_id)

  @po_number  = options[:po_number]
  @ship_to_id = options[:ship_to_id]
  @items      = []
  @options    = options
end

Instance Method Details

#add_item(item = {}) ⇒ Object



46
47
48
49
50
# File 'lib/gun_accessory_supply/order.rb', line 46

def add_item(item = {})
  requires!(item, :identifier, :upc, :qty)

  @items << item
end

#add_recipient(hash = {}) ⇒ Object



33
34
35
36
37
38
# File 'lib/gun_accessory_supply/order.rb', line 33

def add_recipient(hash = {})
  requires!(hash, :dealer_name, :shipping)
  requires!(hash[:shipping], :name, :address, :city, :state, :zip, :email)

  @recipient = hash
end

#filenameObject



52
53
54
55
56
57
58
# File 'lib/gun_accessory_supply/order.rb', line 52

def filename
  @filename ||= begin
    timestamp = Time.now.strftime('%Y%m%d%T').gsub(':', '')

    "GUN-ACCESSORY-SUPPLY-#{@po_number}-#{timestamp}.xml"
  end
end

#submit!Object



60
61
62
# File 'lib/gun_accessory_supply/order.rb', line 60

def submit!
  write_file("/in/#{filename}", self.to_xml)
end

#to_xmlObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/gun_accessory_supply/order.rb', line 64

def to_xml
  output = ""
  xml = Builder::XmlMarkup.new(target: output, indent: 2)

  xml.instruct!(:xml)

  xml.cXML(timestamp: Time.now) do
    xml.Header do
      xml.From do
        xml.Credential(domain: GunAccessorySupply.config.xml_domain) do
          xml.Identity GunAccessorySupply.config.xml_domain
          xml.SharedSecret GunAccessorySupply.config.xml_secret
        end
      end
      xml.To do
        xml.Credential(domain: GunAccessorySupply.config.xml_domain) do
          xml.Identity GunAccessorySupply.config.xml_domain
          xml.SharedSecret GunAccessorySupply.config.xml_secret
        end
      end
      xml.Sender do
        xml.Credential(domain: GunAccessorySupply.config.xml_domain) do
          xml.Identity GunAccessorySupply.config.xml_domain
          xml.SharedSecret GunAccessorySupply.config.xml_secret
        end
      end
    end

    xml.Request do
      xml.OrderRequest do
        xml.OrderRequestHeader(orderDate: Time.now, type: 'new', orderID: @po_number) do
          xml.ShipTo do
            xml.Address(addressID: @ship_to_id) do
              xml.Name @recipient[:dealer_name]
              xml.Email @recipient[:shipping][:email]
              xml.PostalAddress do
                xml.DeliverTo @recipient[:shipping][:name]
                xml.Street @recipient[:shipping][:address]
                xml.City @recipient[:shipping][:city]
                xml.State @recipient[:shipping][:state]
                xml.PostalCode @recipient[:shipping][:zip]
                xml.Country "US"
              end
            end
          end
          xml.BillTo do
            xml.Address do
              xml.Name @recipient[:dealer_name]
              xml.Email @recipient[:shipping][:email]
              xml.PostalAddress do
                xml.DeliverTo @recipient[:shipping][:name]
                xml.Street @recipient[:shipping][:address]
                xml.City @recipient[:shipping][:city]
                xml.State @recipient[:shipping][:state]
                xml.PostalCode @recipient[:shipping][:zip]
                xml.Country "US"
              end
            end
          end
        end

        @items.each do |item|
          xml.ItemOut(quantity: item[:qty]) do
            xml.ItemID do
              xml.SupplierPartID item[:identifier]
            end
            xml.ItemDetail do
              xml.Description item[:upc]
            end
          end
        end
      end
    end
  end

  output
end