Class: RsrGroup::Order
Overview
To submit an order:
-
Instantiate a new Order, passing in ‘:merchant_number`, `:username`, `:password`, `:sequence_number`, and `:identifier`
-
Call #add_recipient
-
Call #add_item for each item on the order
-
Call #submit! to send the order
Defined Under Namespace
Classes: ResponseStruct
Constant Summary
Constants inherited from Base
Base::DEFAULT_CATALOG_FILENAME, Base::DEFAULT_DIR, Base::KEYDEALER_CATALOG_FILENAME, Base::KEYDEALER_DIR, Base::MAP_FILENAME, Base::QTY_FILENAME
Instance Method Summary collapse
- #add_item(item = {}) ⇒ Object
- #add_recipient(shipping_info, ffl_options = {}) ⇒ Object
- #filename ⇒ Object
-
#initialize(options = {}) ⇒ Order
constructor
A new instance of Order.
- #submit! ⇒ Object
- #to_txt ⇒ Object
Methods inherited from Base
Constructor Details
#initialize(options = {}) ⇒ Order
Returns a new instance of Order.
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/rsr_group/order.rb', line 27 def initialize( = {}) requires!(, :merchant_number, :sequence_number, :username, :password, :identifier) @credentials = .select { |k, v| [:username, :password].include?(k) } @identifier = [:identifier] @merchant_number = "%05d" % [:merchant_number] # Leading zeros are required @sequence_number = "%04d" % [:sequence_number] # Leading zeros are required = Time.now.strftime("%Y%m%d") @items = [] end |
Instance Method Details
#add_item(item = {}) ⇒ Object
68 69 70 71 72 |
# File 'lib/rsr_group/order.rb', line 68 def add_item(item = {}) requires!(item, :rsr_stock_number, :quantity, :shipping_carrier, :shipping_method) @items << OrderDetail.new(item.merge(order_identifier: @identifier)) end |
#add_recipient(shipping_info, ffl_options = {}) ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/rsr_group/order.rb', line 53 def add_recipient(shipping_info, = {}) requires!(shipping_info, :shipping_name, :address_one, :city, :state, :zip) @recipient = OrderRecipient.new(shipping_info.merge(order_identifier: @identifier)) if && .any? @ffl = OrderFFL.new(.merge(order_identifier: @identifier)) end end |
#filename ⇒ Object
74 75 76 77 |
# File 'lib/rsr_group/order.rb', line 74 def filename name = ["EORD", @merchant_number, , @sequence_number].join("-") [name, ".txt"].join end |
#submit! ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/rsr_group/order.rb', line 95 def submit! connect(@credentials) do |ftp| ftp.chdir(RsrGroup.config.submission_dir) io = StringIO.new(to_txt) begin ftp.storlines("STOR " + filename, io) success = ftp.nlst.include?(filename) @response = ResponseStruct.new(success, nil, modified: ftp.mtime(filename), size: ftp.size(filename)) ensure io.close end ftp.close end @response || ResponseStruct.new(false) rescue Net::FTPPermError => e return ResponseStruct.new(false, e..chomp) end |
#to_txt ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/rsr_group/order.rb', line 79 def to_txt raise "Recipient is required!" unless @recipient raise "Items are required!" unless @items.length > 0 txt = header + "\n" txt += @recipient.to_single_line + "\n" if @ffl txt += (@ffl.to_single_line + "\n") end @items.each do |item| txt += (item.to_single_line + "\n") end txt += order_trailer + "\n" txt += end |