This library implements the Bai2 standard, as per its official specification.
Add this line to your application's Gemfile:
gem 'bai2'
And then execute:
$ bundle
Or install it yourself as:
$ gem install bai2
BaiFile
is the main class in gem.
# Parse a file:
file = Bai2::BaiFile.parse('file.bai2')
# Parse data:
file = Bai2::BaiFile.new(string_data)
puts file.sender, file.receiver
# e.g. filter for groups relevant to your organization, iterate:
file.groups.filter {|g| g.destination == YourOrgId }.each do |group|
# groups have accounts
group.accounts.each do |account|
puts account.customer, account.currency_code
# summaries are arrays of hashes
puts account.summaries.inspect
# accounts have transactions
# e.g. print all debits
account.transactions.filter(&:debit?).each do |debit|
# transactions have string amounts, too
puts debit.amount
# transaction types are represented by an informative hash:
puts debit.type
# => {
# code: 451,
# transaction: :debit,
# scope: :detail,
# description: "ACH Debit Received",
# }
puts debit.text
end
# e.g. print sum of all credits
sum = account.transactions \
.filter(&:credit?) \
.map(&:amount) \
.map {|a| BigDecimal(a) } \
.reduce(&:+)
puts sum.inspect
end
end
In lib/bai2/integrity.rb
, we perform integrity checks mandated by the Bai2
standard. In our experience, the spec and bank’s real implementations differ on
how sums are calculated. It’s hard to tell if this an industry-wide trend, or
just an SVB quirk. I would love to hear how other banks do this. GitHub Issues
with more information on this would be greatly appreciated.
# Check sum vs. summary + transaction sums
actual_sum = self.transactions.map(&:amount).reduce(0, &:+) \
#+ self.summaries.map {|s| s[:amount] }.reduce(0, &:+)
# TODO: ^ there seems to be a disconnect between what the spec defines
# as the formula for the checksum and what SVB implements...
- Fork it ( https://github.com/venturehacks/bai2/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request