Sha256: 251aec0b40a16b73f48777e4a326ac8d530da3799554e98fab16df94f19eefe0

Contents?: true

Size: 1.14 KB

Versions: 2

Compression:

Stored size: 1.14 KB

Contents

module AmazonOrder
  class Writer
    def initialize(file_glob_pattern, options = {})
      @file_glob_pattern = file_glob_pattern
      @output_dir = options.fetch(:output_dir, 'tmp')
    end

    def print_orders
      data['orders']
    end

    def print_produts
      data['products']
    end

    def generate_csv
      require 'csv'
      FileUtils.mkdir_p(@output_dir)
      %w[orders products].map do |resource|
        next if data[resource].blank?
        csv_file = "#{@output_dir}/#{resource}#{Time.current.strftime('%Y%m%d%H%M%S')}.csv"
        puts "    Writing #{csv_file}"
        CSV.open(csv_file, 'wb') do |csv|
          data[resource].each{|r| csv << r }
        end
        csv_file
      end
    end

    private

    def data
      @_data ||= begin
        data = {'orders' => [], 'products' => []}
        Dir.glob(@file_glob_pattern).each do |filepath|
          puts "    Parsing #{filepath}"
          parser = AmazonOrder::Parser.new(filepath)
          data['orders'] += parser.orders.map(&:values)
          data['products'] += parser.orders.map(&:products).flatten.map(&:values)
        end
        data
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
amazon_order-0.1.2 lib/amazon_order/writer.rb
amazon_order-0.1.1 lib/amazon_order/writer.rb