Skip to content

Instantly share code, notes, and snippets.

@madzhuga
Created March 25, 2021 11:36
Show Gist options
  • Save madzhuga/c40805fb6b608b98ad0926ea34da0aa7 to your computer and use it in GitHub Desktop.
Save madzhuga/c40805fb6b608b98ad0926ea34da0aa7 to your computer and use it in GitHub Desktop.
example
class FinalResult
def results(filename)
@filename = filename
prepare
output
end
def prepare
CSV.foreach(file) do |row|
records << row_record(row) if row.length == 16
end
records.compact
end
def row_record(row)
{
amount: {
currency: headers[0],
subunits: (amount(row) * 100).to_i
},
bank_account_name: account_name(row),
bank_account_number: account_number(row),
bank_branch_code: branch_code(row),
bank_code: bank_code(row),
end_to_end_id: e2e(row)
}
end
def bank_code(row)
row[0]
end
def file
@file ||= File.open(filename, 'r')
end
def headers
@headers ||= CSV.parse_line(file)
end
def output
{
filename: File.basename(filename),
document: file,
failure_code: headers[1],
failure_message: headers[2],
records: records
}
end
def records
@records ||= []
end
def amount(row)
row[8].nil? || row[8] == '0' ? 0 : row[8].to_f
end
def account_number(row)
row[6]&.to_i || 'Bank account number missing'
end
def branch_code(row)
row[2] || 'Bank branch code missing'
end
def e2e(row)
row[10].nil? && row[11].nil? ? 'End to end id missing' : r[10] + r[11]
end
def account_name(row)
row[7].downcase.gsub(' ', '_')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment