Class: X12::Parser
- Inherits:
-
Object
- Object
- X12::Parser
- Defined in:
- lib/x12/parser.rb
Defined Under Namespace
Classes: EdiItem
Instance Attribute Summary collapse
-
#array_of_handles ⇒ Object
Returns the value of attribute array_of_handles.
-
#cf ⇒ Object
Returns the value of attribute cf.
-
#current_level ⇒ Object
Returns the value of attribute current_level.
-
#element_separator ⇒ Object
Returns the value of attribute element_separator.
-
#file_handle ⇒ Object
Returns the value of attribute file_handle.
-
#last_read_loop ⇒ Object
Returns the value of attribute last_read_loop.
-
#last_read_segment ⇒ Object
Returns the value of attribute last_read_segment.
-
#line_count ⇒ Object
Returns the value of attribute line_count.
-
#segment_list ⇒ Object
Returns the value of attribute segment_list.
-
#segment_separator ⇒ Object
Returns the value of attribute segment_separator.
-
#track_level ⇒ Object
Returns the value of attribute track_level.
Instance Method Summary collapse
- #_parse_loop ⇒ Object
- #_parse_loop_start ⇒ Object
- #_set_separators(file_contents) ⇒ Object
- #get_edi_type(file) ⇒ Object
- #get_loop_segments ⇒ Object
- #get_next_loop ⇒ Object
- #get_next_pos_level_loop ⇒ Object
- #get_next_pos_loop ⇒ Object
-
#initialize ⇒ Parser
constructor
A new instance of Parser.
- #parse(options = {}) ⇒ Object
- #print_tree ⇒ Object
Constructor Details
#initialize ⇒ Parser
Returns a new instance of Parser.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/x12/parser.rb', line 41 def initialize @line_count = 0 @current_level = nil @track_level = nil @element_separator = nil @segment_separator = nil @cf = nil @segment_list = nil @array_of_handles = [] @last_read_segment = nil @last_read_loop = nil end |
Instance Attribute Details
#array_of_handles ⇒ Object
Returns the value of attribute array_of_handles.
36 37 38 |
# File 'lib/x12/parser.rb', line 36 def array_of_handles @array_of_handles end |
#cf ⇒ Object
Returns the value of attribute cf.
32 33 34 |
# File 'lib/x12/parser.rb', line 32 def cf @cf end |
#current_level ⇒ Object
Returns the value of attribute current_level.
27 28 29 |
# File 'lib/x12/parser.rb', line 27 def current_level @current_level end |
#element_separator ⇒ Object
Returns the value of attribute element_separator.
30 31 32 |
# File 'lib/x12/parser.rb', line 30 def element_separator @element_separator end |
#file_handle ⇒ Object
Returns the value of attribute file_handle.
35 36 37 |
# File 'lib/x12/parser.rb', line 35 def file_handle @file_handle end |
#last_read_loop ⇒ Object
Returns the value of attribute last_read_loop.
39 40 41 |
# File 'lib/x12/parser.rb', line 39 def last_read_loop @last_read_loop end |
#last_read_segment ⇒ Object
Returns the value of attribute last_read_segment.
38 39 40 |
# File 'lib/x12/parser.rb', line 38 def last_read_segment @last_read_segment end |
#line_count ⇒ Object
Returns the value of attribute line_count.
26 27 28 |
# File 'lib/x12/parser.rb', line 26 def line_count @line_count end |
#segment_list ⇒ Object
Returns the value of attribute segment_list.
33 34 35 |
# File 'lib/x12/parser.rb', line 33 def segment_list @segment_list end |
#segment_separator ⇒ Object
Returns the value of attribute segment_separator.
29 30 31 |
# File 'lib/x12/parser.rb', line 29 def segment_separator @segment_separator end |
#track_level ⇒ Object
Returns the value of attribute track_level.
28 29 30 |
# File 'lib/x12/parser.rb', line 28 def track_level @track_level end |
Instance Method Details
#_parse_loop ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/x12/parser.rb', line 197 def _parse_loop segment = nil segment_list = nil xloop = [] if @last_read_segment != nil xloop.push(@last_read_segment) @last_read_segment = nil end @segment_list[@line_count..-1].each do |segment| @line_count += 1 segment_list = segment.split(@element_separator) @last_read_segment = segment_list @array_of_handles.each do |level_handle| level_handle.each do |tree_index| tmp_loop = @cf.loop_trees[tree_index].loop left = @cf.segmentstart[tmp_loop][0].split(':') if left[0] == segment_list[0] if left[2] == '' @last_read_loop = @cf.loop_trees[tree_index].loop @current_level = @cf.loop_trees[tree_index].level diff = @track_level - @cf.loop_trees[tree_index].level @track_level = @cf.loop_trees[tree_index].level while diff > 0 @array_of_handles.shift diff -= 1 end next_level = @cf.get_next_level(tree_index) if 'END' != next_level[0] @track_level += 1 @array_of_handles.unshift(next_level) return xloop end return xloop else qual = left[2].split(',') if qual.include?(segment_list[left[1].to_i]) @last_read_loop = @cf.loop_trees[tree_index].loop @current_level = @cf.loop_trees[tree_index].level diff = @track_level - @cf.loop_trees[tree_index].level @track_level = @cf.loop_trees[tree_index].level while diff > 0 @array_of_handles.shift diff -=1 end next_level = @cf.get_next_level(tree_index) if 'END' != next_level[0] @track_level += 1 @array_of_handles.unshift(next_level) return xloop end return xloop end # if qual.include? end # if left[2] == '' end # if left[0] == segment_list[0] end # level.handle.each end # @array_of_handles.each xloop.push(segment_list) end # @segment_list[@line_count..-1].each return xloop end |
#_parse_loop_start ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/x12/parser.rb', line 122 def _parse_loop_start current_loop = nil segment = nil segment_list = nil if @last_read_loop != nil tmp = @last_read_loop @last_read_loop = nil return tmp end @segment_list[@line_count..-1].each do |segment| @line_count += 1 segment_list = segment.split(@element_separator) @last_read_segment = segment_list @array_of_handles.each do |level_handle| level_handle.each do |tree_index| xloop = @cf.loop_trees[tree_index].loop left = @cf.segmentstart[xloop][0].split(':') if left[0] == segment_list[0] if left[2] == '' current_loop = @cf.loop_trees[tree_index].loop @current_level = @cf.loop_trees[tree_index].level diff = @track_level - @cf.loop_trees[tree_index].level @track_level = @cf.loop_trees[tree_index].level while diff > 0 @array_of_handles.shift diff -= 1 end next_level = @cf.get_next_level(tree_index) if 'END' != next_level[0] @track_level += 1 @array_of_handles.unshift(next_level) return current_loop end return current_loop else qual = left[2].split(',') if qual.include?(segment_list[left[1].to_i]) current_loop = @cf.loop_trees[tree_index].loop @current_level = @cf.loop_trees[tree_index].level diff = @track_level - @cf.loop_trees[tree_index].level @track_level = @cf.loop_trees[tree_index].level while diff > 0 @array_of_handles.shift diff -=1 end next_level = @cf.get_next_level(tree_index) if 'END' != next_level[0] @track_level += 1 @array_of_handles.unshift(next_level) return current_loop; end return current_loop end end end end end end end |
#_set_separators(file_contents) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/x12/parser.rb', line 108 def _set_separators(file_contents) isa = nil begin isa = file_contents[0, 106] @segment_separator = isa[105].chr @element_separator = isa[3].chr rescue nil end end |
#get_edi_type(file) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/x12/parser.rb', line 57 def get_edi_type(file) file_contents = nil File.open(file, 'r') do |file| file_contents = file.read file_contents = file_contents.strip end self._set_separators(file_contents) @segment_list = file_contents.split(@segment_separator) @segment_list.each do |segment| elements = segment.split(@element_separator) if elements[0] == 'ST' return elements[1] end end return nil end |
#get_loop_segments ⇒ Object
311 312 313 314 315 316 317 318 |
# File 'lib/x12/parser.rb', line 311 def get_loop_segments xloop = [] xloop = self._parse_loop [ @line_count, xloop ] end |
#get_next_loop ⇒ Object
274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/x12/parser.rb', line 274 def get_next_loop xloop = nil xloop = self._parse_loop_start if 'IEA' == xloop #self._set_separator end return false if not xloop or xloop.size == 0 return xloop end |
#get_next_pos_level_loop ⇒ Object
300 301 302 303 304 305 306 307 308 309 |
# File 'lib/x12/parser.rb', line 300 def get_next_pos_level_loop loop = [] loop = self._parse_loop_start return false if not loop or loop.size == 0 return [ @line_count, @current_level, loop ] end |
#get_next_pos_loop ⇒ Object
289 290 291 292 293 294 295 296 297 298 |
# File 'lib/x12/parser.rb', line 289 def get_next_pos_loop loop = [] loop = self._parse_loop_start return false if not loop or loop.size == 0 [ @line_count, loop ] end |
#parse(options = {}) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/x12/parser.rb', line 81 def parse( = {}) file = [:file] conf = [:conf] @cf = X12::CF.new @cf.load(conf) if conf p conf @track_level = 1 level_one = @cf.get_level_one @array_of_handles.unshift(level_one) file_contents = nil File.open(file, 'r') do |file| file_contents = file.read file_contents = file_contents.strip end self._set_separators(file_contents) @segment_list = file_contents.split(@segment_separator) file_contents end |
#print_tree ⇒ Object
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
# File 'lib/x12/parser.rb', line 320 def print_tree pad = nil index = nil segment = nil while line = self.get_next_pos_level_loop pos = line[0].to_i level = line[1].to_i loop = line[2] if level != 0 pad = ' |' * level print " #{pad}-- #{loop}\n" tmp_level = level + 1 tmp_pad = ' |' * tmp_level tmp_loop = self.get_loop_segments tmp_loop.each do |segment| index = sprintf("%+7s", pos) pos = pos += 1 print "#{index}#{tmp_pad}-- #{segment}\n" end end end end |