class Centra::Rule::OrderMatcher

Match Centra orders with Rule sent emails

Attributes

data[R]

Public Class Methods

new(orders_data) click to toggle source

@param [OrdersData] orders_data @example Simple example # centra_orders & rule_orders types are Array<Order> data = {

'jane@example.com' => { centra_orders: [], rule_orders: [] }

}

MatchOrders.new(data)
# File lib/centra/rule/order_matcher.rb, line 14
def initialize(orders_data)
  @matched_orders = []
  @missing_order_emails = []
  @data = orders_data

  perform(orders_data.email_orders)
end

Public Instance Methods

matched() click to toggle source

@return [Hash] the matched orders @example

order_matcher.matched
# => { rule: #<Order:0x07f..>, centra: #<Order:0x09d..> }
# File lib/centra/rule/order_matcher.rb, line 26
def matched
  @matched_orders
end
missing() click to toggle source

@return [Array<Order>] the Centra order that has no match

# File lib/centra/rule/order_matcher.rb, line 31
def missing
  @missing_order_emails
end

Private Instance Methods

perform(email_orders) click to toggle source
# File lib/centra/rule/order_matcher.rb, line 37
def perform(email_orders)
  email_orders.each do |_email, data|
    data[:centra].each do |centra_order|
      matched_order = nil
      # Try to find a matching rule email
      data[:rule].each do |rule_order|
        if OrderCompare.new(centra_order) == rule_order
          matched_order = { rule: rule_order, centra: centra_order }
          break
        end
      end

      if matched_order
        @matched_orders << matched_order
      else
        @missing_order_emails << centra_order
      end
    end
  end
end