Skip to content

Commit

Permalink
Merge pull request #22 from railslove/short-ref-issues
Browse files Browse the repository at this point in the history
fixes short refs in transactions swalloing delimiters
  • Loading branch information
bumi authored Jul 24, 2018
2 parents 6c4ec53 + 9f60869 commit 3a6bf3d
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/cmxl/field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def self.tag
#
def self.parse(line)
if line.match(/\A:(\d{2,2})(\w)?:(.*)\z/m)
tag, modifier, content = $1, $2, $3.gsub(/\r?\n\z/, '') # remove trailing line break to prevent empty field parsing
tag, modifier, content = $1, $2, $3.gsub(/\r/, '').gsub(/\n\z/, '') # remove trailing line break to prevent empty field parsing
Field.parsers[tag.to_s].new(content, modifier, tag)
else
raise LineFormatError, "Wrong line format: #{line.dump}" if Cmxl.config[:raise_line_format_errors]
Expand Down
4 changes: 2 additions & 2 deletions lib/cmxl/fields/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Cmxl
module Fields
class Transaction < Field
self.tag = 61
self.parser = %r{^(?<date>\d{6})(?<entry_date>\d{4})?(?<storno_flag>R?)(?<funds_code>[CD]{1})(?<currency_letter>[a-zA-Z])?(?<amount>\d{1,12},\d{0,2})(?<swift_code>(?:N|F).{3})(?<reference>NONREF|.{0,16})((?:\/\/)(?<bank_reference>[^\n]*))?((?:[\n])?(?<supplementary>.{,34}))$}
self.parser = %r{^(?<date>\d{6})(?<entry_date>\d{4})?(?<storno_flag>R?)(?<funds_code>[CD]{1})(?<currency_letter>[a-zA-Z])?(?<amount>\d{1,12},\d{0,2})(?<swift_code>(?:N|F).{3})(?<reference>NONREF|(.(?!\/\/)){,16}([^\/]){,1})((?:\/\/)(?<bank_reference>[^\n]{,16}))?((?:\n)(?<supplementary>.{,34}))?$}

attr_accessor :details

Expand Down Expand Up @@ -129,7 +129,7 @@ def to_h
'currency_letter' => currency_letter,
}.tap do |h|
h.merge!(details.to_h) if details
h.merge!(supplementary.to_h) unless supplementary.source.empty?
h.merge!(supplementary.to_h) if supplementary.source
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/cmxl/fields/transaction_supplementary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ class TransactionSupplementary < Field

class << self
def parse(line)
initial = $1 if line.match(initial_parser)
charges = $1 if line.match(charges_parser)
initial = $1 if line && line.match(initial_parser)
charges = $1 if line && line.match(charges_parser)
new(line, initial, charges)
end

Expand Down
13 changes: 13 additions & 0 deletions spec/fields/transaction_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
subject(:ocmt_transaction) { Cmxl::Fields::Transaction.parse(fixture_line(:statement_ocmt)) }
subject(:ocmt_cghs_transaction) { Cmxl::Fields::Transaction.parse(fixture_line(:statement_ocmt_chgs)) }
subject(:supplementary_transaction) { Cmxl::Fields::Transaction.parse(fixture_line(:statement_supplementary_plain)) }
subject(:complex_supplementary_transaction) { Cmxl::Fields::Transaction.parse(fixture_line(:statement_supplementary_complex)) }

let(:fixture) { fixture_line(:statement_line).split(/\n/) }

Expand Down Expand Up @@ -64,4 +65,16 @@

it { expect(supplementary_transaction.supplementary.source).to eql('Card Transaction') }
end

context 'statement with complex supplementary' do
it { expect(complex_supplementary_transaction.initial_amount_in_cents).to eql(nil) }
it { expect(complex_supplementary_transaction.initial_currency).to eql(nil) }

it { expect(complex_supplementary_transaction.charges_in_cents).to eql(nil) }
it { expect(complex_supplementary_transaction.charges_currency).to eql(nil) }

it { expect(complex_supplementary_transaction.reference).to eql('FOOBAR/123') }
it { expect(complex_supplementary_transaction.bank_reference).to eql('HERP-DERP-REF') }
it { expect(complex_supplementary_transaction.supplementary.source).to eql('random text / and stuff') }
end
end
2 changes: 2 additions & 0 deletions spec/fixtures/lines/statement_supplementary_complex.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:61:180627D79,NMSCFOOBAR/123//HERP-DERP-REF
random text / and stuff
2 changes: 1 addition & 1 deletion spec/fixtures/lines/statement_supplementary_plain.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
:61:0306280628D21,00FMSCNONREF//98327000090031789
:61:0306280628D21,00FMSCNONREF//983270000900317
Card Transaction

0 comments on commit 3a6bf3d

Please sign in to comment.