Skip to content

Commit

Permalink
Merge pull request #27 from cloudlogistics/BUGS-6634
Browse files Browse the repository at this point in the history
BUGS-6634 Filter out X12 control characters
  • Loading branch information
scottkf authored May 14, 2018
2 parents 0a65831 + 93a2696 commit f1b2646
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
baldr (0.10.5)
baldr (0.10.7)
activesupport

GEM
Expand Down
20 changes: 19 additions & 1 deletion lib/baldr/renderer/x12.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ module Baldr::Renderer::X12

extend self

#TODO: There is an implicit out-of-band knowledge of which character is used
# for the component separator in envelope.rb ; this needs to be removed
# and the addition/construction of that character should be done here.
DEFAULT_SEPARATORS = {
element: '*',
segment: '~',
Expand All @@ -17,6 +20,21 @@ def draw(segments, params = {})

def draw_segment(segment, separators)
a = [segment.id] + segment.elements.reverse.drop_while{ |i| i.nil? }.reverse
#TODO: due to construction methods, if the last element in a segment is nil,
# it won't currently be printed, and if another element requires it,
# no error will be thrown.

#Due to the TODO item concerning the component separator, it is actively
#removed from the below regex
substitution_regex =
Regexp.new "[#{separators.reject{|k,_| k.to_s == "component" }.values.join}]+"

#This removes X12 control characters from any segement element that may have
#them, removing potential parse conflicts.
altered = a.map! { |value|
value.nil? ? nil :
value.gsub(substitution_regex,"")
}

["#{a.join(separators[:element])}#{separators[:segment]}"] + segment.children.map{ |l| draw_loop(l, separators) }
end
Expand All @@ -25,4 +43,4 @@ def draw_loop(loop, separators)
loop.segments.map { |s| draw_segment(s, separators) }
end

end
end
2 changes: 1 addition & 1 deletion lib/baldr/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Baldr
VERSION = '0.10.6'
VERSION = '0.10.7'
end
45 changes: 45 additions & 0 deletions spec/builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,51 @@ def segment_n1(parent)
output.should eq input
end

it 'should truncate special characters on draw' do
file = 'spec/support/edi_files/valid/204/1.EDI'
input = File.read(file)

#TODO: There is implicit knowledge of how the external file is constructed
# here, as evidenced by the construction & assumption of separators.
# Ideally, all of this should be in code.
separators = {
component: '>',
segment: "\n",
element: '*'
}

b = Baldr::Builder.new(
#standard_version_number: '',
sender_id: '4233372493',
sender_id_qualifier: 'ZZ',
receiver_id_qualifier: '02',
receiver_id: 'ODFL',
date_time: DateTime.parse('121109 1642'),
interchange_control_number: '000000002',
usage_indicator: 'P',
acknowledgment_requested: '1',
functional_groups_control_numbers: {
'SM' => '2'
}
)

GoodBuilderFromRealLife.new.build(b)

b.build_functional_groups
#Pull a value and substitute it for the relevant checked separators
value = b.envelope.transactions.first['S5'].first['G62'].first['G6204']
value = separators[:segment] + separators[:element] + value
b.envelope.transactions.first['S5'].first['G62'].first['G6204'] = value

b.prepare!
Baldr::Types.convert_before_render!(b.envelope, Baldr::Grammar::Envelope)
Baldr::Validator.validate!(b.envelope, Baldr::Grammar::Envelope)


output = Baldr::Renderer::X12.draw(b.envelope, {separators: separators})
output.should eq input
end

it 'should throw error if too long and truncate is disabled' do
b = Baldr::Builder.new(
#standard_version_number: '',
Expand Down

0 comments on commit f1b2646

Please sign in to comment.