class AppEarnings::Amazon::Parser

Converts a csv file to a hash.

Attributes

file_name[RW]

Public Class Methods

new(file_name) click to toggle source
# File lib/app_earnings/amazon/parser.rb, line 8
def initialize(file_name)
  @file_name = file_name
  @contents = File.read(@file_name)
  @header, @summary, @details = @contents.split(/Summary|Detail/)

  if @header =~ /Payment Report/
    @report_type = :payments
  else
    @report_type = :earnings
  end
end

Public Instance Methods

extract() click to toggle source
# File lib/app_earnings/amazon/parser.rb, line 20
def extract
  {
    report_type: @report_type,
    header: @header,
    summary: parse(@summary.strip),
    details: parse((@details || '').strip)
  }
end

Private Instance Methods

parse(content) click to toggle source
# File lib/app_earnings/amazon/parser.rb, line 31
def parse(content)
  return nil if content.nil?
  extracted_data = []
  options = { headers: true, header_converters: :symbol }

  CSV.parse(content, options) do |row|
    extracted_data << row.to_hash
  end
  extracted_data
end