Class: AmazonOrder::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/amazon_order/writer.rb

Instance Method Summary collapse

Constructor Details

#initialize(file_glob_pattern, options = {}) ⇒ Writer

Returns a new instance of Writer.



3
4
5
6
# File 'lib/amazon_order/writer.rb', line 3

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

Instance Method Details

#generate_csvObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/amazon_order/writer.rb', line 16

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|
      csv << attributes_for(resource)
      data[resource].each { |r| csv << r }
    end
    csv_file
  end
end


8
9
10
# File 'lib/amazon_order/writer.rb', line 8

def print_orders
  data['orders']
end


12
13
14
# File 'lib/amazon_order/writer.rb', line 12

def print_products
  data['products']
end