Sha256: ce57da3a9607ea65ec16752e58c8bc3b4aee9a03fc72b57c8234d616feb41054

Contents?: true

Size: 1.11 KB

Versions: 1

Compression:

Stored size: 1.11 KB

Contents

module AmazonOrder
  class Writer
    def initialize(base_dir, options = {})
      @base_dir = base_dir
      @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("#{@base_dir}/*html").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

1 entries across 1 versions & 1 rubygems

Version Path
amazon_order-0.1.0 lib/amazon_order/writer.rb