x12-lite 0.3.6 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/bin/x12 +15 -13
  3. data/lib/x12-lite.rb +59 -61
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0e175feff272628ec0164f5df6f42d417ba303d8ac500b068b382c5f07c3a610
4
- data.tar.gz: 88f3082cd0cf4e4222f511d79ac9514ec3a39c508ad707c3bc3a7cfa1bffab73
3
+ metadata.gz: 54a8c2fb07d59082d7803d063f97ac9252cbea9359a154233ca32c5890224c6e
4
+ data.tar.gz: af40d70fcb563fa5e463ebe4f48586357c804d964853ea383e8306e4ce649220
5
5
  SHA512:
6
- metadata.gz: 155c80204f474bc905c854fb99403431bdb5dc179c9121c33fb05a2c1c8e24253257ddd184cdfcb2fb1922fadf642b53e6ebbfa5cc8d9cdd407c15ea015dde32
7
- data.tar.gz: aee6f06c2b012a0955cc01966fcd06ead8efd499aa3dc1a62a097f0b911c0acc83ec045c13e97c899d9f3db6751a120c30ef9d093cb7de84e5fb19f7110b35cc
6
+ metadata.gz: 9d9f4ffd7f32426ec208d88b7a6b84e266b391ffb4e0a31b46a753e923199f6932c1afc2d5469c659e5870020e36ae469d22288952bc178031b0903b38ddf8da
7
+ data.tar.gz: fa9466f71e763b8c70641a5b5542938b7b527ca4924879e5f4635f1ba0a11aafcf9bf63134ab7628459926340fbc3a031f94357268da4cd98e8608f30e2bf41a
data/bin/x12 CHANGED
@@ -26,6 +26,7 @@ OptionParser.new.instance_eval do
26
26
  @banner = "usage: #{program_name} [options] <file> <file> ..."
27
27
 
28
28
  on "-a", "--after <date>" , "After (date as 'YYYYMMDD' or time as 'YYYYMMDD HHMMSS')"
29
+ on "--ansi" , "Use ANSI color codes to display values"
29
30
  on "-c", "--count" , "Count messages at the end"
30
31
  on "-d", "--dive" , "Dive into directories recursively"
31
32
  # on "--delim <char>" , "Delimiter to use"
@@ -36,10 +37,10 @@ OptionParser.new.instance_eval do
36
37
  on "-l", "--lower" , "Show segment names in lowercase"
37
38
  on "-m", "--message" , "Show message body"
38
39
  on "-p", "--path" , "Show path for each message"
39
- # on "-q", "--query <value>" , "Query a specific value"
40
+ on "-q", "--query <value>" , "Query a specific value"
40
41
  # on "-r", "--repeats" , "Show field repeats on their own line"
41
42
  on "-s", "--spacer" , "Show an empty line between messages"
42
- # on "-t", "--tsv" , "Tab-delimit output (tsv format)"
43
+ on "-t", "--tsv" , "Tab-delimit output (tsv format)"
43
44
  on "-v", "--version" , "Show version" do abort "v#{::X12::VERSION}"; end
44
45
  # on "-w", "--width <width>", "Width of segment names", Integer
45
46
 
@@ -54,13 +55,14 @@ require "time" if opts["after"]
54
55
  time = Time.parse(opts["after"]) if opts["after"]
55
56
  dive = opts["dive"]
56
57
  only = opts["fields-only"] and opts["fields"] = true
57
- # quer = opts["query"].split(',').map(&:strip) if opts["query"]
58
- # from = quer.delete("-") if quer.is_a?(Array)
59
- # delm = opts["tsv"] ? "\t" : (opts["delim"] || "|")
60
- # delm = {"\\n" => "\n", "\\t" => "\t"}[delm] || delm
58
+ quer = opts["query"].split(',').map(&:strip) if opts["query"]
59
+ from = quer.delete("-") if quer.is_a?(Array)
60
+ seps = opts["tsv"] ? "\t" : (opts["delim"] || "|")
61
+ seps = {"\\n" => "\n", "\\t" => "\t"}[seps] || seps
61
62
 
62
63
  args = []
63
64
  # args << :deep if opts["repeats"]
65
+ args << :ansi if opts["ansi"]
64
66
  args << :down if opts["lower"]
65
67
  args << :full if opts["message"] || opts.empty?
66
68
  args << :hide if !opts["fields"]
@@ -126,13 +128,13 @@ list.each do |file|
126
128
  abort "ERROR: malformed X12 file: \"#{file}\" (#{$!})" unless opts["ignore"]
127
129
  next
128
130
  end
129
- # if quer
130
- # hits = *x12.find(*quer)
131
- # hits.unshift file if opts["path"] || from
132
- # puts hits.join(delm)
133
- # puts if opts["path"]
134
- # next
135
- # end
131
+ if quer
132
+ hits = *x12.find(*quer)
133
+ hits.unshift file if opts["path"] || from
134
+ puts hits.join(seps)
135
+ puts if opts["path"]
136
+ next
137
+ end
136
138
  x12.show(*args)
137
139
  msgs += 1
138
140
  rescue => e
data/lib/x12-lite.rb CHANGED
@@ -45,7 +45,7 @@ end
45
45
  # ==[ X12 ]=====================================================================
46
46
 
47
47
  class X12
48
- VERSION="0.3.6"
48
+ VERSION="0.4.1"
49
49
 
50
50
  include Enumerable
51
51
 
@@ -59,6 +59,14 @@ class X12
59
59
  ! " # & ' ( ) * + , - . / : ; = ?
60
60
  end
61
61
 
62
+ REGEX = /^
63
+ (..[^-.(]?) # seg: eb
64
+ (?:\((\d*|[+!?*]?)\))? # num: eb(3)
65
+ [-.]?(\d+)? # fld: eb(3)-4
66
+ (?:\((\d*|[+!?*]?)\))? # rep: eb(3)-4(5)
67
+ [-.]?(\d+)?$ # com: eb(3)-4(5).6
68
+ /x
69
+
62
70
  # delimiter pos chr
63
71
  # ---------- --- ---
64
72
  # field 4 (*)
@@ -156,9 +164,10 @@ class X12
156
164
  full = opts.include?(:full) # show body at top
157
165
  deep = opts.include?(:deep) # dive into repeats
158
166
  down = opts.include?(:down) # show segments in lowercase
159
- list = opts.include?(:list) # give back a list or print it
167
+ list = opts.include?(:list) # puts output or return a list
160
168
  hide = opts.include?(:hide) # hide output
161
169
  only = opts.include?(:only) # only show first of each segment type
170
+ ansi = opts.include?(:ansi) # highlight data using ansi color codes
162
171
  left = opts.grep(Integer).first || 15 # left justify size
163
172
 
164
173
  out = full ? [to_s] : []
@@ -183,7 +192,7 @@ class X12
183
192
  end
184
193
  end
185
194
  tag = "#{seg}%s-#{j}" % [num > 1 && !only ? "(#{num})" : ""]
186
- out << (tag.ljust(left) + ansi(fld, "fff", "369"))
195
+ out << (tag.ljust(left) + (ansi ? ansi(fld, "fff", "369") : fld))
187
196
  end
188
197
  end
189
198
  end
@@ -224,8 +233,8 @@ class X12
224
233
  val = args[1]
225
234
 
226
235
  # Syntax: seg(num)-fld(rep).com
227
- pos =~ /^(..[^-.(]?)?(?:\((\d*|[+!?*]?)\))?[-.]?(\d+)?(?:\((\d*|[+!?*]?)\))?[-.]?(\d+)?$/
228
- seg = $1 or return ""; want = /^#{seg}[^#{Regexp.escape(@seg)}\r\n]*/i
236
+ pos =~ REGEX or raise "bad selector '#{pos}'"
237
+ seg = $1; want = /^#{seg}[^#{Regexp.escape(@seg)}\r\n]*/i
229
238
  num = $2 && $2.to_i; new_num = $2 == "+"; ask_num = $2 == "?"; all_num = $2 == "*"
230
239
  rep = $4 && $4.to_i; new_rep = $4 == "+"; ask_rep = $4 == "?"; all_rep = $4 == "*"
231
240
  fld = $3 && $3.to_i; len > 1 && fld == 0 and raise "zero index on field"
@@ -389,6 +398,51 @@ class X12
389
398
  end
390
399
  end
391
400
 
401
+ def find(*ask)
402
+ return if ask.empty?
403
+
404
+ str = to_s
405
+ say = []
406
+
407
+ ask.each do |pos|
408
+ say.push(nil) && next if pos.nil?
409
+ pos =~ REGEX or raise "bad selector '#{pos}'"
410
+ seg = $1; want = /^#{seg}[^#{Regexp.escape(@seg)}\r\n]*/i
411
+ num = $2 && $2.to_i; new_num = $2 == "+"; ask_num = $2 == "?"; all_num = $2 == "*"
412
+ rep = $4 && $4.to_i; new_rep = $4 == "+"; ask_rep = $4 == "?"; all_rep = $4 == "*"
413
+ fld = $3 && $3.to_i
414
+ com = $5 && $5.to_i
415
+
416
+ if all_num
417
+ raise "multi query allows only one selector" if ask.size > 1
418
+ return str.scan(want).inject([]) do |ary, out|
419
+ out = loop do
420
+ out = out.split(@fld)[fld ] or break if fld
421
+ break out.split(@rep).size if ask_rep
422
+ out = out.split(@rep)[rep - 1] or break if rep || (com && (rep ||= 1))
423
+ out = out.split(@com)[com - 1] or break if com
424
+ break out
425
+ end
426
+ ary << out if out
427
+ ary
428
+ end
429
+ end
430
+
431
+ say << loop do
432
+ out = ""
433
+ break str.scan( want).size if ask_num && !ask_rep
434
+ out = str.scan( want)[num - 1] or break "" if num ||= 1
435
+ out = out.split(@fld)[fld ] or break "" if fld
436
+ break out.split(@rep).size if ask_rep
437
+ out = out.split(@rep)[rep - 1] or break "" if rep || (com && (rep ||= 1))
438
+ out = out.split(@com)[com - 1] or break "" if com
439
+ break out
440
+ end
441
+ end
442
+
443
+ say.size > 1 ? say : say.first
444
+ end
445
+
392
446
  # def now(fmt="%Y%m%d%H%M%S")
393
447
  # Time.now.strftime(fmt)
394
448
  # end
@@ -544,62 +598,6 @@ class X12
544
598
  # end
545
599
  # end
546
600
  # end
547
- #
548
- # def find(*ask)
549
- # return if ask.empty?
550
- #
551
- # str = to_s
552
- # say = []
553
- #
554
- # seg = nil
555
- #
556
- # ask.each do |pos|
557
- # say.push(nil) && next if pos.nil?
558
- # pos = pos.to_s unless pos.is_a?(String)
559
- # pos =~ /^([A-Z]..)?(?:\((\d*|\?|\*)\))?[-.]?(\d+)?(?:\((\d*|\?|\*)\))?[-.]?(\d+)?[-.]?(\d+)?$/i or raise "bad query #{pos.inspect}"
560
- # seg = $1 if $1; want = /^#{seg}.*/i
561
- # num = $2 && $2.to_i; ask_num = $2 == "?"; all_num = $2 == "*"
562
- # rep = $4 && $4.to_i; ask_rep = $4 == "?"; all_rep = $4 == "*"
563
- # fld = $3 && $3.to_i
564
- # com = $5 && $5.to_i
565
- # sub = $6 && $6.to_i
566
- #
567
- # # p [seg, num, rep, fld, com, sub]
568
- #
569
- # msh = seg =~ /^MSH$/i # is this an MSH segment?
570
- # fld -= 1 if msh && fld # MSH fields are offset by one
571
- #
572
- # if all_num
573
- # raise "multi query allows only one selector" if ask.size > 1
574
- # return str.scan(want).inject([]) do |ary, out|
575
- # out = loop do
576
- # out = out.split(@fld)[fld ] or break if fld
577
- # break out.split(@rep).size if ask_rep
578
- # out = out.split(@rep)[rep - 1] or break if rep || (com && (rep ||= 1)) # default to first
579
- # out = out.split(@com)[com - 1] or break if com
580
- # out = out.split(@sub)[sub - 1] or break if sub
581
- # break out
582
- # end
583
- # ary << out if out
584
- # ary
585
- # end
586
- # end
587
- #
588
- # say << loop do
589
- # out = ""
590
- # break str.scan( want).size if ask_num && !ask_rep
591
- # out = str.scan( want)[num - 1] or break "" if num ||= 1 # default to first
592
- # out = out.split(@fld)[fld ] or break "" if fld
593
- # break out.split(@rep).size if ask_rep
594
- # out = out.split(@rep)[rep - 1] or break "" if rep || (com && (rep ||= 1)) # default to first
595
- # out = out.split(@com)[com - 1] or break "" if com
596
- # out = out.split(@sub)[sub - 1] or break "" if sub
597
- # break out
598
- # end
599
- # end
600
- #
601
- # say.size > 1 ? say : say.first
602
- # end
603
601
  end
604
602
 
605
603
  __END__
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: x12-lite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.6
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Shreeve
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-10-18 00:00:00.000000000 Z
11
+ date: 2024-10-21 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Ruby gem to parse and generate X.12 transactions
14
14
  email: steve.shreeve@gmail.com