Skip to content

Commit

Permalink
Lg/fix failure when parsing empty details (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
lauragilgz authored Dec 30, 2024
1 parent dd3c13e commit be3411e
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion cfonb.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Gem::Specification.new do |s|
s.name = 'cfonb'
s.version = '1.1.0'
s.version = '1.1.1'
s.required_ruby_version = '>= 3.2'
s.summary = 'CFONB parser'
s.description = 'An easy to use CFONB format parser'
Expand Down
2 changes: 1 addition & 1 deletion lib/cfonb/operation_details.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def self.register(code, klass)
end

def self.for(line)
return unless line.respond_to?(:detail_code)
return unless line.respond_to?(:detail_code) && line.detail_code != ''

@details[line.detail_code] || Unknown
end
Expand Down
2 changes: 1 addition & 1 deletion lib/cfonb/operation_details/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def self.inherited(base)
base.singleton_class.prepend(
Module.new do
def apply(details, line)
code = :"@#{line.detail_code}"
code = :"@#{line.detail_code.gsub(' ', '_')}"
details.instance_variable_set(code, instance_value(details, line, code))

super
Expand Down
2 changes: 1 addition & 1 deletion lib/cfonb/operation_details/unknown.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Unknown < Base

def self.apply(details, line)
details.unknown ||= {}
code = line.detail_code
code = line.detail_code.gsub(' ', '_')

details.unknown[code] =
if details.unknown[code] && line.detail.is_a?(String)
Expand Down
19 changes: 15 additions & 4 deletions spec/cfonb/operation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -276,23 +276,34 @@
context 'with an unknown detail' do
let(:detail) do
OpenStruct.new(
body: '0530004411001871EUR2 0001016255614090823 AAAEUR200000000000740',
detail_code: 'AAA',
body: '0530004411001871EUR2 0001016255614090823 A AEUR200000000000740',
detail_code: 'A A',
detail: 'EUR200000000000740',
)
end

it 'adds the detail to the unknown details hash' do
operation.merge_detail(detail)

expect(operation.details.unknown).to eq({ 'AAA' => 'EUR200000000000740' })
expect(operation.details.unknown).to eq({ 'A_A' => 'EUR200000000000740' })
end

it 'updates the current details in case of duplicated codes' do
operation.merge_detail(detail)
operation.merge_detail(detail)

expect(operation.details.unknown).to eq({ 'AAA' => "EUR200000000000740\nEUR200000000000740" })
expect(operation.details.unknown).to eq({ 'A_A' => "EUR200000000000740\nEUR200000000000740" })
end

it 'does not add any detail if they are empty' do
detail = OpenStruct.new(
body: '0530004411001871EUR2 0001016255614090823',
detail_code: '',
detail: 'EUR200000000000740',
)
operation.merge_detail(detail)

expect(operation.details.unknown).to be_nil
end
end
end
Expand Down
1 change: 1 addition & 0 deletions spec/cfonb/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
'AAA' => "INTERNETA AAA\nINTERNETA ABB",
'BBB' => 'INTERNETE BBB',
'CCC' => 'INTERNETI CCC',
'N_Y' => 'EXAMPLE WITH EMPTY SPACE',
},
)

Expand Down
2 changes: 2 additions & 0 deletions spec/files/example.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
0515589916200000EUR2 98765432100B1160519 AAAINTERNETA ABB
0515589916200000EUR2 98765432100B1160519 BBBINTERNETE BBB
0515589916200000EUR2 98765432100B1160519 CCCINTERNETI CCC
0515589916200000EUR2 98765432100B1160519
0515589916200000EUR2 98765432100B1160519 N YEXAMPLE WITH EMPTY SPACE

0415589916200000EUR2 98765432100B1160519 160519VIR SEPA DEMONSTRATION 0000000000000000000107}REFERENCE
0515589916200000EUR2 98765432100B1160519 NPYELEC ERDF
Expand Down

0 comments on commit be3411e

Please sign in to comment.