Created
July 19, 2022 19:33
-
-
Save cokara/3ab02b4f59a950868ee4e696d2b70807 to your computer and use it in GitHub Desktop.
Host File Ingestion spike
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HostConfig | |
def details | |
{ | |
format: 'xlsx', | |
mappings: { | |
project_name: { source_field: 'DEVELOPER' }, | |
date_reading_from: { source_field: 'GEN_PD_BEG_DATE' }, | |
date_reading_to: { source_field: 'GEN_PD_END_DATE' }, | |
kwh: { source_field: 'ALLOC_KWH' }, | |
utility_account_number: { source_field: 'SUBSCRIBER' }, | |
vnm_bill_credits: { source_field: 'ACTUAL CREDIT', mapper: :absolute_value}, | |
} | |
} | |
end | |
def absolute_value(val) | |
val.to_f.abs | |
end | |
end | |
class FileParser | |
attr_reader :file, :config, :csv, :items | |
def initialize(file:, config:) | |
@file = file | |
@config = config | |
end | |
def call | |
load_csv | |
validate_format | |
generate_items | |
@items.first(5) | |
end | |
def load_csv | |
# Remove BOM characters from the file | |
csv_str = file.sheet(0).to_csv.gsub!("\xEF\xBB\xBF".force_encoding("UTF-8"), '');nil | |
@csv = CSV.parse(csv_str, headers: true) | |
end | |
def validate_format | |
config.details[:mappings].each_pair do |attr, meta| | |
field_name = meta[:source_field] | |
raise "Missing Field: #{field_name}" unless csv.headers.include?(field_name) | |
end | |
puts "parsing complete" | |
end | |
def generate_items | |
items = [] | |
mappings = config.details[:mappings] | |
csv.each do |row| | |
items << [ | |
date_reading_to: row[mappings[:date_reading_to][:source_field]], | |
date_reading_from: row[mappings[:date_reading_from][:source_field]], | |
kwh: row[mappings[:kwh][:source_field]], | |
#subscriber_name: subscriber_name(row), | |
#utility_account: utility_account(row), | |
vnm_bill_credits: config.send(mappings[:vnm_bill_credits][:mapper], row[mappings[:vnm_bill_credits][:source_field]]) | |
] | |
end | |
@items = items | |
end | |
end | |
path = "/Users/charlesokara/Downloads/CS Credit Report - Summit Ridge April - Arcadia.xlsx" | |
f = Roo::Excelx.new(path, extension: :xlsx) | |
config = HostConfig.new | |
parser = FileParser.new(file: f, config: config) | |
parser.call |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment