module CSV2API::Utils

@author Jonah Ruiz <jonah@pixelhipsters.com> Utility module with helper methods

Public Class Methods

included(base) click to toggle source
# File lib/csv2api/utils.rb, line 7
def self.included(base)
  base.extend(self)
end

Public Instance Methods

csv_files(file_path) click to toggle source

Extracts the csv files from path @param file_path [IO] file path @return [Array<String>] path with csv files

# File lib/csv2api/utils.rb, line 15
def csv_files(file_path)
  Dir.glob("#{file_path}/*.csv")
end
file_names(file_path, extension: false) click to toggle source

Extracts the csv filename from collection @param file_path [IO] file path @param extension [TrueClass, FalseClass] return extension @return [Array<String>] csv filenames with or out .csv extension

# File lib/csv2api/utils.rb, line 23
def file_names(file_path, extension: false)
  csv_files(file_path).map do |file|
    file_name = file[/(\w*.csv)/]
    extension ? file_name : file_name[0..-5]
  end
end
generate_json(hash, pretty: false) click to toggle source

Converts a hash into a stringified json format @param hash [Hash] hash to converted into json format @param pretty [TrueClass, FalseClass] pretty json option @return [JSON<String>] stringified json

# File lib/csv2api/utils.rb, line 34
def generate_json(hash, pretty: false)
  pretty ? JSON.pretty_generate(hash) : JSON.generate(hash)
end
load_csv(path, filename) click to toggle source

Helper method for opening a csv file @param path [String] file path @param filename [String] file name in path to open @return [File] csv file

# File lib/csv2api/utils.rb, line 42
def load_csv(path, filename)
  File.read("#{path}/#{filename}.csv")
end
sanitize_column_headers(csv) click to toggle source

Sanitizes key names for column headers @param csv [Array<Hash>] csv data @return [Array<String>] column header names

# File lib/csv2api/utils.rb, line 49
def sanitize_column_headers(csv)
  csv.first.keys.map { |col| col.to_s.titleize }
end