class Cmxl::Statement

Attributes

collection[RW]
fields[RW]
lines[RW]
source[RW]

Public Class Methods

new(source) click to toggle source

Public: Initiate a new Statement and parse a provided single statement string It directly parses the source and initiates file and transaction objects.

Example:

Cmxl::Statement.new(single_statement_string)

# File lib/cmxl/statement.rb, line 12
def initialize(source)
  self.source = source
  self.fields = []
  self.lines = []
  strip_headers! if Cmxl.config[:strip_headers]
  parse!
end

Public Instance Methods

account_identification() click to toggle source
# File lib/cmxl/statement.rb, line 66
def account_identification
  field(25)
end
available_balance() click to toggle source
# File lib/cmxl/statement.rb, line 86
def available_balance
  field(64)
end
closing_balance() click to toggle source
# File lib/cmxl/statement.rb, line 78
def closing_balance
  field(62, 'F')
end
closing_or_intermediary_balance() click to toggle source
# File lib/cmxl/statement.rb, line 82
def closing_or_intermediary_balance
  field(62)
end
field(tag, modifier = nil) click to toggle source

Internal: Field accessor returns a field object by a given tag

Example: field(20) field(61,'F')

# File lib/cmxl/statement.rb, line 151
def field(tag, modifier = nil)
  fields.detect { |field| field.tag == tag.to_s && (modifier.nil? || field.modifier == modifier) }
end
generation_date() click to toggle source

Get generation date from field 20. If generation date is not provided in field 20, method will fall back to field 13 if present.

# File lib/cmxl/statement.rb, line 62
def generation_date
  field(20).date || (field(13).nil? ? nil : field(13).date)
end
mt940_hash() click to toggle source
# File lib/cmxl/statement.rb, line 110
def mt940_hash
  {
    'reference' => reference,
    'sha' => sha,
    'generation_date' => generation_date,
    'account_identification' => account_identification.to_h,
    'opening_balance' => opening_balance.to_h,
    'closing_balance' => closing_balance.to_h,
    'available_balance' => available_balance.to_h,
    'transactions' => transactions.map(&:to_h),
    'fields' => fields.map(&:to_h)
  }
end
mt942?() click to toggle source
# File lib/cmxl/statement.rb, line 102
def mt942?
  fields.any? { |field| field.is_a? Fields::FloorLimitIndicator }
end
mt942_hash() click to toggle source
# File lib/cmxl/statement.rb, line 124
def mt942_hash
  {
    'reference' => reference,
    'sha' => sha,
    'generation_date' => generation_date,
    'account_identification' => account_identification.to_h,
    'debit_summary' => vmk_debit_summary.to_h,
    'credit_summary' => vmk_credit_summary.to_h,
    'transactions' => transactions.map(&:to_h),
    'fields' => fields.map(&:to_h)
  }
end
opening_balance() click to toggle source
# File lib/cmxl/statement.rb, line 70
def opening_balance
  field(60, 'F')
end
opening_or_intermediary_balance() click to toggle source
# File lib/cmxl/statement.rb, line 74
def opening_or_intermediary_balance
  field(60)
end
parse!() click to toggle source

Internal: Parse a single MT940 statement and extract the line data

# File lib/cmxl/statement.rb, line 26
def parse!
  self.fields = []

  lines = source.split(/(^:[0-9A-Z]{2,3}:)/m).reject(&:empty?).each_slice(2).map(&:join)

  lines.map do |line|
    if line =~ /\A:86:/
      if field = fields.last
        field.add_meta_data(line)
      end
    else
      field = Field.parse(line)
      fields << field unless field.nil?
    end
  end
end
reference() click to toggle source
# File lib/cmxl/statement.rb, line 57
def reference
  field(20).reference
end
sha() click to toggle source

Public: SHA2 of the provided source This is an experiment of trying to identify statements. The MT940 itself might not provide a unique identifier

Returns the SHA2 of the source

# File lib/cmxl/statement.rb, line 53
def sha
  Digest::SHA2.new.update(source).to_s
end
strip_headers!() click to toggle source
# File lib/cmxl/statement.rb, line 43
def strip_headers!
  source.gsub!(/\A.+?(?=^:)/m, '') # beginning: strip every line in the beginning that does not start with a :
  source.gsub!(/^[^:]+\z/, '') # end: strip every line in the end that does not start with a :
  source.strip!
end
to_h() click to toggle source
# File lib/cmxl/statement.rb, line 106
def to_h
  mt942? ? mt942_hash : mt940_hash
end
to_hash() click to toggle source
# File lib/cmxl/statement.rb, line 137
def to_hash
  to_h
end
to_json(*args) click to toggle source
# File lib/cmxl/statement.rb, line 141
def to_json(*args)
  to_h.to_json(*args)
end
transactions() click to toggle source
# File lib/cmxl/statement.rb, line 20
def transactions
  fields.select { |field| field.is_a?(Fields::Transaction) }
end
vmk_credit_summary() click to toggle source
# File lib/cmxl/statement.rb, line 94
def vmk_credit_summary
  field(90, 'C')
end
vmk_debit_summary() click to toggle source
# File lib/cmxl/statement.rb, line 98
def vmk_debit_summary
  field(90, 'D')
end