Sha256: f1da50241766202414eb185f0835f963a9c5f36cb00d2d1f40a09183f9d88ebe

Contents?: true

Size: 1.11 KB

Versions: 6

Compression:

Stored size: 1.11 KB

Contents

module Workarea
  module Emarsys
    class SyncOrders
      include Sidekiq::Worker

      def perform
        # get the orders based on the time span set in config
        orders = Workarea::Order.emarsys_exportable

        return if orders.empty?

        Dir.mktmpdir do |dir|
          file = "order_export.csv"
          path = "#{dir}/#{file}"
          File.new(path, 'w+')

          CSV.open(path, "wb") do |csv|
            csv << headers
            orders.each do |order|
              order.items.each do |item|
                csv << order_line(item)
              end
            end
          end
          gateway.send_file(path)
        end
      end

      private
        def order_line(item)
          [
            item.order.id,
            item.order.email,
            item.product_id,
            item.total_price.to_s,
            item.quantity,
            item.order.placed_at.utc.iso8601
          ]
        end

        def headers
          ["order", "email", "item", "price", "quantity", "timestamp"]
        end

        def gateway
          Emarsys.sales_data_gateway
        end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
workarea-emarsys-1.0.4 app/workers/workarea/emarsys/sync_orders.rb
workarea-emarsys-2.0.1 app/workers/workarea/emarsys/sync_orders.rb
workarea-emarsys-2.0.0 app/workers/workarea/emarsys/sync_orders.rb
workarea-emarsys-1.1.0 app/workers/workarea/emarsys/sync_orders.rb
workarea-emarsys-1.0.3 app/workers/workarea/emarsys/sync_orders.rb
workarea-emarsys-1.0.2 app/workers/workarea/emarsys/sync_orders.rb