class CSV2API::Parser

@author Jonah Ruiz <jonah@pixelhipsters.com> Contains methods for parsing CSV files

Attributes

file[R]

@!attribute [r] file @return [String] parsed csv file

Public Class Methods

new(file) click to toggle source

Passes file argument down to parser @param file [File, IO] file to parse @return [CSV2API::CSVParse] class instance of self

# File lib/csv2api/parser.rb, line 14
def initialize(file)
  @file = parse(file)
end

Public Instance Methods

all() click to toggle source

Returns an array of hashes from csv file @return [Array<Hash>] csv data

# File lib/csv2api/parser.rb, line 32
def all
  file.to_a.map { |row| empty?(row.to_hash) }.compact
end
empty?(hash) click to toggle source

Returns hash unless it's empty @param hash [Hash<Symbol>] @return [Hash<Symbol>] hash

# File lib/csv2api/parser.rb, line 39
def empty?(hash)
  hash unless hash.empty?
end
parse(csv_file) click to toggle source

Parses csv file @param csv_file [File, IO] csv file to parse @return [CSV] parsed csv data

# File lib/csv2api/parser.rb, line 21
def parse(csv_file)
  CSV::Converters[:blank_to_nil] = lambda do |field|
    field && field.empty? ? nil : field
  end

  CSV.new(csv_file,
          headers: true, header_converters: :symbol, converters: :all)
end