x12 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,363 @@
1
+ # This file is part of Ruby X12-Parser (x12-parser)
2
+ #
3
+ # Copyright (C) 2008 Chris Parker
4
+ # All rights reserved
5
+ #
6
+ # Based on Perl X12::Parser by
7
+ # Prasad Poruporuthan
8
+ # http://search.cpan.org/src/PRASAD/X12-0.09
9
+ #
10
+ # x12-parser is free software: you can redistribute it and/or modify
11
+ # it under the terms of the GNU Lesser General Public License as published by
12
+ # the Free Software Foundation, either version 3 of the License, or
13
+ # (at your option) any later version.
14
+ #
15
+ # x12-parser is distributed in the hope that it will be useful,
16
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
+ # GNU General Public License for more details.
19
+ #
20
+ # You should have received a copy of the GNU Lesser General Public License
21
+ # along with x12-parser. If not, see <http://www.gnu.org/licenses/>.
22
+
23
+ module X12
24
+
25
+ class Parser
26
+ attr_accessor :line_count
27
+ attr_accessor :current_level
28
+ attr_accessor :track_level
29
+ attr_accessor :segment_separator
30
+ attr_accessor :element_separator
31
+
32
+ attr_accessor :cf
33
+ attr_accessor :segment_list
34
+
35
+ attr_accessor :file_handle
36
+ attr_accessor :array_of_handles
37
+
38
+ attr_accessor :last_read_segment
39
+ attr_accessor :last_read_loop
40
+
41
+ def initialize
42
+ @line_count = 0
43
+ @current_level = nil
44
+ @track_level = nil
45
+ @element_separator = nil
46
+ @segment_separator = nil
47
+
48
+ @cf = nil
49
+ @segment_list = nil
50
+
51
+ @array_of_handles = []
52
+
53
+ @last_read_segment = nil
54
+ @last_read_loop = nil
55
+ end
56
+
57
+ def get_edi_type(file)
58
+
59
+ file_contents = nil
60
+
61
+ File.open(file, 'r') do |file|
62
+ file_contents = file.read
63
+ file_contents = file_contents.strip
64
+ end
65
+
66
+ self._set_separators(file_contents)
67
+ @segment_list = file_contents.split(@segment_separator)
68
+ @segment_list.each do |segment|
69
+
70
+ elements = segment.split(@element_separator)
71
+
72
+ if elements[0] == 'ST'
73
+ return elements[1]
74
+ end
75
+ end
76
+
77
+ return nil
78
+
79
+ end
80
+
81
+ def parse(options = {})
82
+ file = options[:file]
83
+ conf = options[:conf]
84
+
85
+ @cf = X12::CF.new
86
+ @cf.load(conf) if conf
87
+
88
+ p conf
89
+
90
+ @track_level = 1
91
+ level_one = @cf.get_level_one
92
+
93
+ @array_of_handles.unshift(level_one)
94
+
95
+ file_contents = nil
96
+ File.open(file, 'r') do |file|
97
+ file_contents = file.read
98
+ file_contents = file_contents.strip
99
+ end
100
+
101
+ self._set_separators(file_contents)
102
+ @segment_list = file_contents.split(@segment_separator)
103
+
104
+ file_contents
105
+
106
+ end
107
+
108
+ def _set_separators(file_contents)
109
+ isa = nil
110
+
111
+ begin
112
+ isa = file_contents[0, 106]
113
+
114
+ @segment_separator = isa[105].chr
115
+ @element_separator = isa[3].chr
116
+ rescue
117
+ nil
118
+ end
119
+
120
+ end
121
+
122
+ def _parse_loop_start
123
+
124
+ current_loop = nil
125
+ segment = nil
126
+ segment_list = nil
127
+
128
+ if @last_read_loop != nil
129
+ tmp = @last_read_loop
130
+ @last_read_loop = nil
131
+ return tmp
132
+ end
133
+
134
+ @segment_list[@line_count..-1].each do |segment|
135
+
136
+ @line_count += 1
137
+ segment_list = segment.split(@element_separator)
138
+
139
+ @last_read_segment = segment_list
140
+
141
+ @array_of_handles.each do |level_handle|
142
+ level_handle.each do |tree_index|
143
+
144
+ xloop = @cf.loop_trees[tree_index].loop
145
+
146
+ left = @cf.segmentstart[xloop][0].split(':')
147
+
148
+ if left[0] == segment_list[0]
149
+ if left[2] == ''
150
+ current_loop = @cf.loop_trees[tree_index].loop
151
+ @current_level = @cf.loop_trees[tree_index].level
152
+ diff = @track_level - @cf.loop_trees[tree_index].level
153
+ @track_level = @cf.loop_trees[tree_index].level
154
+
155
+ while diff > 0
156
+ @array_of_handles.shift
157
+ diff -= 1
158
+ end
159
+
160
+ next_level = @cf.get_next_level(tree_index)
161
+ if 'END' != next_level[0]
162
+ @track_level += 1
163
+ @array_of_handles.unshift(next_level)
164
+ return current_loop
165
+ end
166
+ return current_loop
167
+
168
+ else
169
+
170
+ qual = left[2].split(',')
171
+ if qual.include?(segment_list[left[1].to_i])
172
+ current_loop = @cf.loop_trees[tree_index].loop
173
+ @current_level = @cf.loop_trees[tree_index].level
174
+ diff = @track_level - @cf.loop_trees[tree_index].level
175
+ @track_level = @cf.loop_trees[tree_index].level
176
+
177
+ while diff > 0
178
+ @array_of_handles.shift
179
+ diff -=1
180
+ end
181
+
182
+ next_level = @cf.get_next_level(tree_index)
183
+ if 'END' != next_level[0]
184
+ @track_level += 1
185
+ @array_of_handles.unshift(next_level)
186
+ return current_loop;
187
+ end
188
+ return current_loop
189
+ end
190
+ end
191
+ end
192
+ end
193
+ end
194
+ end
195
+ end
196
+
197
+ def _parse_loop
198
+ segment = nil
199
+ segment_list = nil
200
+ xloop = []
201
+
202
+ if @last_read_segment != nil
203
+ xloop.push(@last_read_segment)
204
+ @last_read_segment = nil
205
+ end
206
+
207
+ @segment_list[@line_count..-1].each do |segment|
208
+
209
+ @line_count += 1
210
+
211
+ segment_list = segment.split(@element_separator)
212
+ @last_read_segment = segment_list
213
+
214
+ @array_of_handles.each do |level_handle|
215
+ level_handle.each do |tree_index|
216
+
217
+ tmp_loop = @cf.loop_trees[tree_index].loop
218
+ left = @cf.segmentstart[tmp_loop][0].split(':')
219
+
220
+ if left[0] == segment_list[0]
221
+
222
+ if left[2] == ''
223
+ @last_read_loop = @cf.loop_trees[tree_index].loop
224
+ @current_level = @cf.loop_trees[tree_index].level
225
+ diff = @track_level - @cf.loop_trees[tree_index].level
226
+ @track_level = @cf.loop_trees[tree_index].level
227
+
228
+ while diff > 0
229
+ @array_of_handles.shift
230
+ diff -= 1
231
+ end
232
+
233
+ next_level = @cf.get_next_level(tree_index)
234
+ if 'END' != next_level[0]
235
+ @track_level += 1
236
+ @array_of_handles.unshift(next_level)
237
+ return xloop
238
+ end
239
+
240
+ return xloop
241
+
242
+ else
243
+ qual = left[2].split(',')
244
+
245
+ if qual.include?(segment_list[left[1].to_i])
246
+
247
+ @last_read_loop = @cf.loop_trees[tree_index].loop
248
+ @current_level = @cf.loop_trees[tree_index].level
249
+ diff = @track_level - @cf.loop_trees[tree_index].level
250
+ @track_level = @cf.loop_trees[tree_index].level
251
+
252
+ while diff > 0
253
+ @array_of_handles.shift
254
+ diff -=1
255
+ end
256
+
257
+ next_level = @cf.get_next_level(tree_index)
258
+ if 'END' != next_level[0]
259
+ @track_level += 1
260
+ @array_of_handles.unshift(next_level)
261
+ return xloop
262
+ end
263
+ return xloop
264
+ end # if qual.include?
265
+ end # if left[2] == ''
266
+ end # if left[0] == segment_list[0]
267
+ end # level.handle.each
268
+ end # @array_of_handles.each
269
+ xloop.push(segment_list)
270
+ end # @segment_list[@line_count..-1].each
271
+ return xloop
272
+ end
273
+
274
+ def get_next_loop
275
+
276
+ xloop = nil
277
+
278
+ xloop = self._parse_loop_start
279
+
280
+ if 'IEA' == xloop
281
+ #self._set_separator
282
+ end
283
+
284
+ return false if not xloop or xloop.size == 0
285
+
286
+ return xloop
287
+ end
288
+
289
+ def get_next_pos_loop
290
+ loop = []
291
+
292
+ loop = self._parse_loop_start
293
+
294
+ return false if not loop or loop.size == 0
295
+
296
+ [ @line_count, loop ]
297
+
298
+ end
299
+
300
+ def get_next_pos_level_loop
301
+ loop = []
302
+
303
+ loop = self._parse_loop_start
304
+
305
+ return false if not loop or loop.size == 0
306
+
307
+ return [ @line_count, @current_level, loop ]
308
+
309
+ end
310
+
311
+ def get_loop_segments
312
+ xloop = []
313
+
314
+ xloop = self._parse_loop
315
+
316
+ [ @line_count, xloop ]
317
+
318
+ end
319
+
320
+ def print_tree
321
+
322
+ pad = nil
323
+ index = nil
324
+ segment = nil
325
+
326
+ while line = self.get_next_pos_level_loop
327
+
328
+ pos = line[0].to_i
329
+ level = line[1].to_i
330
+ loop = line[2]
331
+
332
+ if level != 0
333
+ pad = ' |' * level
334
+ print " #{pad}-- #{loop}\n"
335
+ tmp_level = level + 1
336
+ tmp_pad = ' |' * tmp_level
337
+ tmp_loop = self.get_loop_segments
338
+
339
+ tmp_loop.each do |segment|
340
+ index = sprintf("%+7s", pos)
341
+ pos = pos += 1
342
+ print "#{index}#{tmp_pad}-- #{segment}\n"
343
+ end
344
+ end
345
+ end
346
+ end
347
+
348
+ class EdiItem
349
+
350
+ attr_accessor :loop_xid
351
+ attr_accessor :xid
352
+ attr_accessor :elements
353
+ attr_accessor :children
354
+
355
+ def initialize
356
+ @loop_xid = ''
357
+ @xid = ''
358
+ @elements = []
359
+ @children = []
360
+ end
361
+ end
362
+ end
363
+ end
@@ -0,0 +1,5 @@
1
+ require 'test/unit'
2
+ require 'x12'
3
+
4
+ class Test832
5
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: x12
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Chris Parker
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-04-27 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: EDI x12 is a standard format for parsing and reading Invoices, Orders, etc. This library makes that easier.
17
+ email: chris.p@adelpo.com, mrcsparker@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ - LICENSE
25
+ - AUTHORS
26
+ - TODO
27
+ files:
28
+ - cf/277_004010X092.cf
29
+ - cf/810.cf
30
+ - cf/278_004010X094_Res.cf
31
+ - cf/270_004010X092.cf
32
+ - cf/835_004010X091.cf
33
+ - cf/832.cf
34
+ - cf/855.cf
35
+ - cf/276_004010X093.cf
36
+ - cf/997.cf
37
+ - cf/820_004010X061.cf
38
+ - cf/278_004010X094_Req.cf
39
+ - lib/x12
40
+ - lib/x12/cf.rb
41
+ - lib/x12/parser.rb
42
+ - lib/x12.rb
43
+ - test/test_832.rb
44
+ - Rakefile
45
+ - README
46
+ - LICENSE
47
+ - AUTHORS
48
+ - TODO
49
+ has_rdoc: true
50
+ homepage: http://rubyforge.org/projects/x12-parser/
51
+ post_install_message:
52
+ rdoc_options:
53
+ - --title
54
+ - x12 Documentation
55
+ - --main
56
+ - README
57
+ - -q
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: "0"
71
+ version:
72
+ requirements: []
73
+
74
+ rubyforge_project: x12-parser
75
+ rubygems_version: 0.9.5
76
+ signing_key:
77
+ specification_version: 2
78
+ summary: A generalized Ruby EDI x12 parsing engine.
79
+ test_files: []
80
+