Module: CSV2API::Utils
Overview
Utility module with helper methods
Class Method Summary collapse
-
.csv_files(file_path) ⇒ Array<String>
Extracts the csv files from path.
-
.file_names(file_path, extension: false) ⇒ Array<String>
Extracts the csv filename from collection.
-
.generate_json(hash, pretty: false) ⇒ JSON<String>
Converts a hash into a stringified json format.
- .included(base) ⇒ Object
-
.load_csv(path, filename) ⇒ File
Helper method for opening a csv file.
-
.sanitize_column_headers(csv) ⇒ Array<String>
Sanitizes key names for column headers.
Class Method Details
.csv_files(file_path) ⇒ Array<String>
Extracts the csv files from path
15 16 17 |
# File 'lib/csv2api/utils.rb', line 15 def csv_files(file_path) Dir.glob("#{file_path}/*.csv") end |
.file_names(file_path, extension: false) ⇒ Array<String>
Extracts the csv filename from collection
23 24 25 26 27 28 |
# 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) ⇒ JSON<String>
Converts a hash into a stringified json format
34 35 36 |
# File 'lib/csv2api/utils.rb', line 34 def generate_json(hash, pretty: false) pretty ? JSON.pretty_generate(hash) : JSON.generate(hash) end |
.included(base) ⇒ Object
7 8 9 |
# File 'lib/csv2api/utils.rb', line 7 def self.included(base) base.extend(self) end |
.load_csv(path, filename) ⇒ File
Helper method for opening a csv file
42 43 44 |
# File 'lib/csv2api/utils.rb', line 42 def load_csv(path, filename) File.read("#{path}/#{filename}.csv") end |
.sanitize_column_headers(csv) ⇒ Array<String>
Sanitizes key names for column headers
49 50 51 |
# File 'lib/csv2api/utils.rb', line 49 def sanitize_column_headers(csv) csv.first.keys.map { |col| col.to_s.titleize } end |