Class: Rawbotz::OrderProcessor
- Inherits:
-
Object
- Object
- Rawbotz::OrderProcessor
- Defined in:
- lib/rawbotz/processors/order_processor.rb
Instance Method Summary collapse
-
#check_against_cart ⇒ Object
Returns diff -> perfect: [], …
-
#initialize(order, logger = Logger.new("/dev/null")) ⇒ OrderProcessor
constructor
A new instance of OrderProcessor.
-
#process! ⇒ Object
Yield items, if block given.
Constructor Details
#initialize(order, logger = Logger.new("/dev/null")) ⇒ OrderProcessor
Returns a new instance of OrderProcessor.
5 6 7 8 9 10 |
# File 'lib/rawbotz/processors/order_processor.rb', line 5 def initialize(order, logger=Logger.new("/dev/null")) @order = order @logger = logger @form_token = YAML::load_file( Rawbotz::conf_file_path)["remote_shop"]["form_token"] end |
Instance Method Details
#check_against_cart ⇒ Object
Returns diff -> perfect: [], …
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/rawbotz/processors/order_processor.rb', line 44 def check_against_cart diff = {perfect: [], under_avail: [], extra: [], modified: [], miss: [], error: []}.to_h # have logger mech = Rawbotz::new_mech mech.login cart_c = mech.get_cart_content cart = cart_c.map{|i| [i[0],i[1]]}.to_h @order.order_items.processible.find_each do |item| remote_name = item.local_product.try(:remote_product).try(:name) qty = cart.delete remote_name # missing case: more than wanted if !qty.nil? # Is in cart if qty.to_i == item.num_wished && qty.to_i == item.num_ordered diff[:perfect] << [item, qty.to_i] elsif qty.to_i == item.num_ordered diff[:under_avail] << [item, qty.to_i] else # + information qty.to_i diff[:modified] << [item, qty.to_i] end else diff[:miss] << [item, nil] # probably not available end end cart.each do |name, qty| diff[:extra] << [name, qty] end @order.order_items.where(state: "error").find_each do |item| diff[:error] << [item.local_product.name] end diff end |
#process! ⇒ Object
Yield items, if block given
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/rawbotz/processors/order_processor.rb', line 13 def process! mech = Rawbotz::new_mech mech.login @order.order_items.processible.find_each do |item| if item.remote_product_id.present? && item.num_wished.present? log_product_handling item begin ordered_qty = mech.add_to_cart! item.remote_product_id, item.num_wished, @form_token rescue Exception => e STDERR.puts e..to_s STDERR.puts e.backtrace ordered_qty = nil item.update(state: "error") end item.update(num_ordered: ordered_qty.to_i) log_result item yield item if block_given? else @logger.warn("Cannot order this item (no rem. prod/num_wished)") @logger.warn(item.attributes) end end @order.update(ordered_at: DateTime.now) end |