ruby-hl7 1.2.3 → 1.3.0

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 (53) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +7 -0
  3. data/.rubocop.yml +127 -0
  4. data/.travis.yml +20 -0
  5. data/Gemfile +3 -0
  6. data/Gemfile.lock +58 -0
  7. data/NOTES.md +121 -0
  8. data/README.rdoc +5 -0
  9. data/Rakefile +77 -0
  10. data/VERSION +1 -0
  11. data/VERSION.yml +4 -0
  12. data/examples/proxy_server.rb +26 -0
  13. data/lib/ruby-hl7.rb +1 -1
  14. data/lib/segments/pid.rb +13 -1
  15. data/ruby-hl7.gemspec +38 -0
  16. data/spec/ail_segment_spec.rb +28 -0
  17. data/spec/aip_segment_spec.rb +31 -0
  18. data/spec/basic_parsing_spec.rb +319 -0
  19. data/spec/batch_parsing_spec.rb +52 -0
  20. data/spec/child_segment_spec.rb +66 -0
  21. data/spec/core_ext/date_time_spec.rb +43 -0
  22. data/spec/default_segment_spec.rb +31 -0
  23. data/spec/dg1_spec.rb +42 -0
  24. data/spec/dynamic_segment_def_spec.rb +37 -0
  25. data/spec/err_segment_spec.rb +26 -0
  26. data/spec/evn_segment_spec.rb +23 -0
  27. data/spec/fts_segment_spec.rb +19 -0
  28. data/spec/in1_segment_spec.rb +34 -0
  29. data/spec/message_spec.rb +53 -0
  30. data/spec/messages_spec.rb +24 -0
  31. data/spec/mfe_segment_spec.rb +28 -0
  32. data/spec/mfi_segment_spec.rb +28 -0
  33. data/spec/msa_segment_spec.rb +27 -0
  34. data/spec/msh_segment_spec.rb +28 -0
  35. data/spec/nk1_segment_spec.rb +26 -0
  36. data/spec/obr_segment_spec.rb +45 -0
  37. data/spec/obx_segment_spec.rb +68 -0
  38. data/spec/orc_segment_spec.rb +27 -0
  39. data/spec/pid_segment_spec.rb +78 -0
  40. data/spec/prd_segment_spec.rb +29 -0
  41. data/spec/pv1_segment_spec.rb +23 -0
  42. data/spec/rf1_segment_spec.rb +29 -0
  43. data/spec/sch_segment_spec.rb +32 -0
  44. data/spec/segment_field_spec.rb +110 -0
  45. data/spec/segment_generator_spec.rb +32 -0
  46. data/spec/segment_list_storage_spec.rb +47 -0
  47. data/spec/segment_spec.rb +38 -0
  48. data/spec/sft_segment_spec.rb +26 -0
  49. data/spec/spec_helper.rb +13 -0
  50. data/spec/speed_parsing_spec.rb +19 -0
  51. data/spec/spm_segment_spec.rb +26 -0
  52. metadata +117 -13
  53. data/lib/segments/zcf.rb +0 -22
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 1
3
+ :minor: 3
4
+ :patch: 0
@@ -0,0 +1,26 @@
1
+ # $Id$
2
+ # Ruby-HL7 Proxy Server Example
3
+ require 'rubygems'
4
+ require 'ruby-hl7'
5
+ require 'thread'
6
+ require 'socket'
7
+
8
+ PORT = 2402
9
+ target_ip = "127.0.0.1"
10
+ target_port = 5900
11
+
12
+ srv = TCPServer.new(PORT)
13
+ puts "proxy_server listening on port: %i" % PORT
14
+ puts "proxying for: %s:%i" % [ target_ip, target_port ]
15
+ while true
16
+ sok = srv.accept
17
+ Thread.new( sok ) do |my_socket|
18
+ raw_inp = my_socket.readlines
19
+ msg = HL7::Message.new( raw_input )
20
+ puts "forwarding message:\n%s" % msg.to_s
21
+ soc = TCPSocket.open( target_ip, target_port )
22
+ soc.write msg.to_mllp
23
+ soc.close
24
+ end
25
+ end
26
+
@@ -21,7 +21,7 @@ require 'stringio'
21
21
  require 'date'
22
22
 
23
23
  module HL7 # :nodoc:
24
- VERSION = '1.2.3'
24
+ VERSION = '1.3.0'
25
25
  def self.ParserConfig
26
26
  @parser_cfg ||= { :empty_segment_is_error => true }
27
27
  end
@@ -21,7 +21,7 @@ class HL7::Message::Segment::PID < HL7::Message::Segment
21
21
  add_field :patient_alias
22
22
  add_field :race
23
23
  add_field :address
24
- add_field :country_code
24
+ add_field :county_code
25
25
  add_field :phone_home
26
26
  add_field :phone_business
27
27
  add_field :primary_language
@@ -53,4 +53,16 @@ class HL7::Message::Segment::PID < HL7::Message::Segment
53
53
  add_field :strain
54
54
  add_field :production_class_code
55
55
  add_field :tribal_citizenship
56
+
57
+ def country_code
58
+ warn "DEPRECATION WARNING: PID-12 is defined as 'county_code'; "+
59
+ "the 'country_code' alias is retained for backwards compatibility only."
60
+ county_code
61
+ end
62
+
63
+ def country_code=(country_code)
64
+ warn "DEPRECATION WARNING: PID-12 is defined as 'county_code'; "+
65
+ "the 'country_code' alias is retained for backwards compatibility only."
66
+ self.county_code = country_code
67
+ end
56
68
  end
@@ -0,0 +1,38 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{ruby-hl7}
8
+ s.version = "1.3.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Mark Guzman"]
12
+ s.date = %q{2014-09-09}
13
+ s.description = %q{A simple library to parse and generate HL7 2.x messages}
14
+ s.email = %q{ruby-hl7@googlegroups.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+
20
+ s.files = `git ls-files`.split "\n"
21
+ s.test_files = `git ls-files -- {spec}/*`.split "\n"
22
+ s.homepage = %q{http://github.com/ruby-hl7/ruby-hl7}
23
+ s.rdoc_options = ["--charset=UTF-8"]
24
+ s.require_paths = ["lib"]
25
+ s.required_ruby_version = Gem::Requirement.new(">= 1.8.6")
26
+ s.rubyforge_project = %q{ruby-hl7}
27
+ s.rubygems_version = %q{1.4.2}
28
+ s.summary = %q{Ruby HL7 Library}
29
+ s.license = "MIT"
30
+
31
+ s.add_dependency 'rake', '~> 11.0'
32
+ s.add_dependency 'rdoc', '~> 3.12'
33
+
34
+ s.add_development_dependency 'bundler', '~> 1.17'
35
+ s.add_development_dependency 'simplecov', '~> 0.15'
36
+ s.add_development_dependency 'rspec', '~> 2.99'
37
+ s.add_development_dependency 'pry'
38
+ end
@@ -0,0 +1,28 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe HL7::Message::Segment::AIL do
5
+ context 'general' do
6
+ before :all do
7
+ @base_ail = 'AIL|1|A|OFFICE^^^OFFICE|^OFFICE^A4'
8
+ end
9
+
10
+ it 'creates an AIL segment' do
11
+ expect do
12
+ ail = HL7::Message::Segment::AIL.new( @base_ail )
13
+ expect(ail).not_to be_nil
14
+ expect(ail.to_s).to eq @base_ail
15
+ end.not_to raise_error
16
+ end
17
+
18
+ it 'allows access to an AIL segment' do
19
+ expect do
20
+ ail = HL7::Message::Segment::AIL.new( @base_ail )
21
+ expect(ail.set_id).to eq '1'
22
+ expect(ail.segment_action_code).to eq 'A'
23
+ expect(ail.location_resource_id).to eq 'OFFICE^^^OFFICE'
24
+ expect(ail.location_type).to eq '^OFFICE^A4'
25
+ end.not_to raise_error
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,31 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe HL7::Message::Segment::AIP do
5
+ context 'general' do
6
+ before :all do
7
+ @base_aip = 'AIP|1|U|JSB^ISON^Kathy^S|D^Doctor||20020108150000|||10|m^Minutes'
8
+ end
9
+
10
+ it 'creates an AIP segment' do
11
+ expect do
12
+ aip = HL7::Message::Segment::AIP.new( @base_aip )
13
+ expect(aip).not_to be_nil
14
+ expect(aip.to_s).to eq @base_aip
15
+ end.not_to raise_error
16
+ end
17
+
18
+ it 'allows access to an AIP segment' do
19
+ expect do
20
+ aip = HL7::Message::Segment::AIP.new( @base_aip )
21
+ expect(aip.set_id).to eq '1'
22
+ expect(aip.segment_action_code).to eq 'U'
23
+ expect(aip.personnel_resource_id).to eq 'JSB^ISON^Kathy^S'
24
+ expect(aip.resource_role).to eq 'D^Doctor'
25
+ expect(aip.start_date_time).to eq '20020108150000'
26
+ expect(aip.duration).to eq '10'
27
+ expect(aip.duration_units).to eq 'm^Minutes'
28
+ end.not_to raise_error
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,319 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe HL7::Message do
5
+ context 'basic parsing' do
6
+ before :all do
7
+ @simple_msh_txt = open( './test_data/test.hl7' ).readlines.first
8
+ @empty_txt = open( './test_data/empty.hl7' ).readlines.first
9
+ @empty_segments_txt = open( './test_data/empty_segments.hl7' ).readlines.first
10
+ @base_msh = "MSH|^~\\&|LAB1||DESTINATION||19910127105114||ORU^R03|LAB1003929"
11
+ @base_msh_alt_delims = "MSH$@~\\&|LAB1||DESTINATION||19910127105114||ORU^R03|LAB1003929"
12
+ end
13
+
14
+ it 'parses simple text' do
15
+ msg = HL7::Message.new
16
+ msg.parse @simple_msh_txt
17
+ expect(msg.to_hl7).to eq @simple_msh_txt
18
+ end
19
+
20
+ it 'parses delimiters properly' do
21
+ msg = HL7::Message.new( @base_msh )
22
+ expect(msg.element_delim).to eq "|"
23
+ expect(msg.item_delim).to eq "^"
24
+
25
+ msg = HL7::Message.new( @base_msh_alt_delims )
26
+ expect(msg.element_delim).to eq "$"
27
+ expect(msg.item_delim).to eq "@"
28
+ end
29
+
30
+ it 'parses via the constructor' do
31
+ msg = HL7::Message.new( @simple_msh_txt )
32
+ expect(msg.to_hl7).to eq @simple_msh_txt
33
+ end
34
+
35
+ it 'parses via the class method' do
36
+ msg = HL7::Message.parse( @simple_msh_txt )
37
+ expect(msg.to_hl7).to eq @simple_msh_txt
38
+ end
39
+
40
+ it 'only parses String and Enumerable data' do
41
+ expect { HL7::Message.parse :MSHthis_shouldnt_parse_at_all }.to raise_error(HL7::ParseError)
42
+ end
43
+
44
+ it 'parses empty strings' do
45
+ expect { HL7::Message.new @empty_txt }.not_to raise_error
46
+ end
47
+
48
+ it 'converts to strings' do
49
+ msg = HL7::Message.new
50
+ msg.parse @simple_msh_txt
51
+ orig = @simple_msh_txt.gsub( /\r/, "\n" )
52
+ expect(msg.to_s).to eq orig
53
+ end
54
+
55
+ it 'converts to a string and to HL7' do
56
+ msg = HL7::Message.new( @simple_msh_txt )
57
+ expect(msg.to_hl7).not_to eq(msg.to_s)
58
+ end
59
+
60
+ it 'allows access to segments by index' do
61
+ msg = HL7::Message.new
62
+ msg.parse @simple_msh_txt
63
+ expect(msg[0].to_s).to eq @base_msh
64
+ end
65
+
66
+ it 'allows access to segments by name' do
67
+ msg = HL7::Message.new
68
+ msg.parse @simple_msh_txt
69
+ expect(msg["MSH"].to_s).to eq @base_msh
70
+ end
71
+
72
+ it 'allows access to segments by symbol' do
73
+ msg = HL7::Message.new
74
+ msg.parse @simple_msh_txt
75
+ expect(msg[:MSH].to_s).to eq @base_msh
76
+ end
77
+
78
+ it 'inserts segments by index' do
79
+ msg = HL7::Message.new
80
+ msg.parse @simple_msh_txt
81
+ inp = HL7::Message::Segment::Default.new
82
+ msg[1] = inp
83
+ expect(msg[1]).to eq inp
84
+
85
+ expect { msg[2] = Class.new }.to raise_error(HL7::Exception)
86
+ end
87
+
88
+ it 'returns nil when accessing a missing segment' do
89
+ msg = HL7::Message.new
90
+ msg.parse @simple_msh_txt
91
+ expect { expect(msg[:does_not_exist]).to be_nil }.not_to raise_error
92
+ end
93
+
94
+ it 'inserts segments by name' do
95
+ msg = HL7::Message.new
96
+ msg.parse @simple_msh_txt
97
+ inp = HL7::Message::Segment::NTE.new
98
+ msg["NTE"] = inp
99
+ expect(msg["NTE"]).to eq inp
100
+ expect { msg["NTE"] = Class.new }.to raise_error(HL7::Exception)
101
+ end
102
+
103
+ it 'inserts segments by symbol' do
104
+ msg = HL7::Message.new
105
+ msg.parse @simple_msh_txt
106
+ inp = HL7::Message::Segment::NTE.new
107
+ msg[:NTE] = inp
108
+ expect(msg[:NTE]).to eq inp
109
+ expect { msg[:NTE] = Class.new }.to raise_error(HL7::Exception)
110
+ end
111
+
112
+ it 'allows access to segment elements' do
113
+ msg = HL7::Message.new
114
+ msg.parse @simple_msh_txt
115
+ expect(msg[:MSH].sending_app).to eq "LAB1"
116
+ end
117
+
118
+ it 'allows modification of segment elements' do
119
+ msg = HL7::Message.new
120
+ msg.parse @simple_msh_txt
121
+ msg[:MSH].sending_app = "TEST"
122
+ expect(msg[:MSH].sending_app).to eq "TEST"
123
+ end
124
+
125
+ it 'raises NoMethodError when accessing a missing element' do
126
+ msg = HL7::Message.new
127
+ msg.parse @simple_msh_txt
128
+ expect {msg[:MSH].does_not_really_exist_here}.to raise_error(NoMethodError)
129
+ end
130
+
131
+ it 'raises NoMethodError when modifying a missing element' do
132
+ msg = HL7::Message.new
133
+ msg.parse @simple_msh_txt
134
+ expect {msg[:MSH].does_not_really_exist_here="TEST"}.to raise_error(NoMethodError)
135
+ end
136
+
137
+ it 'permits elements to be accessed via numeric names' do
138
+ msg = HL7::Message.new( @simple_msh_txt )
139
+ expect(msg[:MSH].e2).to eq "LAB1"
140
+ expect(msg[:MSH].e3).to be_empty
141
+ end
142
+
143
+ it 'permits elements to be modified via numeric names' do
144
+ msg = HL7::Message.parse( @simple_msh_txt )
145
+ msg[:MSH].e2 = "TESTING1234"
146
+ expect(msg[:MSH].e2).to eq "TESTING1234"
147
+ end
148
+
149
+ it 'allows appending of segments' do
150
+ msg = HL7::Message.new
151
+ expect do
152
+ msg << HL7::Message::Segment::MSH.new
153
+ msg << HL7::Message::Segment::NTE.new
154
+ end.not_to raise_error
155
+
156
+ expect { msg << Class.new }.to raise_error(HL7::Exception)
157
+ end
158
+
159
+ it 'allows appending of an array of segments' do
160
+ msg = HL7::Message.new
161
+ expect do
162
+ msg << [HL7::Message::Segment::MSH.new, HL7::Message::Segment::NTE.new]
163
+ end.not_to raise_error
164
+
165
+ obx = HL7::Message::Segment::OBX.new
166
+ expect do
167
+ obx.children << [HL7::Message::Segment::NTE.new, HL7::Message::Segment::NTE.new]
168
+ end.not_to raise_error
169
+ end
170
+
171
+ it 'sorts segments' do
172
+ msg = HL7::Message.new
173
+ pv1 = HL7::Message::Segment::PV1.new
174
+ msg << pv1
175
+ msh = HL7::Message::Segment::MSH.new
176
+ msg << msh
177
+ nte = HL7::Message::Segment::NTE.new
178
+ msg << nte
179
+ nte2 = HL7::Message::Segment::NTE.new
180
+ msg << nte2
181
+ msh.sending_app = "TEST"
182
+
183
+ initial = msg.to_s
184
+ sorted = msg.sort
185
+ final = sorted.to_s
186
+ expect(initial).not_to eq(final)
187
+ end
188
+
189
+ it 'automatically assigns a set_id to a new segment' do
190
+ msg = HL7::Message.new
191
+ msh = HL7::Message::Segment::MSH.new
192
+ msg << msh
193
+ ntea = HL7::Message::Segment::NTE.new
194
+ ntea.comment = "first"
195
+ msg << ntea
196
+ nteb = HL7::Message::Segment::NTE.new
197
+ nteb.comment = "second"
198
+ msg << nteb
199
+ ntec = HL7::Message::Segment::NTE.new
200
+ ntec.comment = "third"
201
+ msg << ntec
202
+ expect(ntea.set_id).to eq "1"
203
+ expect(nteb.set_id).to eq "2"
204
+ expect(ntec.set_id).to eq "3"
205
+ end
206
+
207
+ it 'parses Enumerable data' do
208
+ test_file = open( './test_data/test.hl7' )
209
+ expect(test_file).not_to be_nil
210
+
211
+ msg = HL7::Message.new( test_file )
212
+ expect(msg.to_hl7).to eq @simple_msh_txt
213
+ end
214
+
215
+ it 'has a to_info method' do
216
+ msg = HL7::Message.new( @simple_msh_txt )
217
+ expect(msg[1].to_info).not_to be_nil
218
+ end
219
+
220
+ it 'parses a raw array' do
221
+ inp = "NTE|1|ME TOO"
222
+ nte = HL7::Message::Segment::NTE.new( inp.split( '|' ) )
223
+ expect(nte.to_s).to eq inp
224
+ end
225
+
226
+ it 'produces MLLP output' do
227
+ msg = HL7::Message.new( @simple_msh_txt )
228
+ expect = "\x0b%s\x1c\r" % msg.to_hl7
229
+ expect(msg.to_mllp).to eq expect
230
+ end
231
+
232
+ it 'parses MLLP input' do
233
+ raw = "\x0b%s\x1c\r" % @simple_msh_txt
234
+ msg = HL7::Message.parse( raw )
235
+ expect(msg).not_to be_nil
236
+ expect(msg.to_hl7).to eq @simple_msh_txt
237
+ expect(msg.to_mllp).to eq raw
238
+ end
239
+
240
+ it 'can parse its own MLLP output' do
241
+ msg = HL7::Message.parse( @simple_msh_txt )
242
+ expect(msg).not_to be_nil
243
+ expect do
244
+ post_mllp = HL7::Message.parse( msg.to_mllp )
245
+ expect(post_mllp).not_to be_nil
246
+ expect(msg.to_hl7).to eq post_mllp.to_hl7
247
+ end.not_to raise_error
248
+ end
249
+
250
+ it 'can access child elements' do
251
+ obr = HL7::Message::Segment::OBR.new
252
+ expect do
253
+ expect(obr.children).not_to be_nil
254
+ expect(obr.children.length).to be_zero
255
+ end.not_to raise_error
256
+ end
257
+
258
+ it 'can add child elements' do
259
+ obr = HL7::Message::Segment::OBR.new
260
+ expect do
261
+ expect(obr.children.length).to be_zero
262
+ (1..5).each do |x|
263
+ obr.children << HL7::Message::Segment::OBX.new
264
+ expect(obr.children.length).to eq x
265
+ end
266
+ end.not_to raise_error
267
+ end
268
+
269
+ it 'rejects invalid child segments' do
270
+ obr = HL7::Message::Segment::OBR.new
271
+ expect { obr.children << Class.new }.to raise_error(HL7::Exception)
272
+ end
273
+
274
+ it 'supports grouped, sequenced segments' do
275
+ #multible obr's with multiple obx's
276
+ msg = HL7::Message.parse( @simple_msh_txt )
277
+ orig_output = msg.to_hl7
278
+ (1..10).each do |obr_id|
279
+ obr = HL7::Message::Segment::OBR.new
280
+ msg << obr
281
+ (1..10).each do |obx_id|
282
+ obx = HL7::Message::Segment::OBX.new
283
+ obr.children << obx
284
+ end
285
+ end
286
+
287
+ expect(msg[:OBR]).not_to be_nil
288
+ expect(msg[:OBR].length).to eq 11
289
+ expect(msg[:OBX]).not_to be_nil
290
+ expect(msg[:OBX].length).to eq 102
291
+ expect(msg[:OBR][4].children[1].set_id).to eq "2"
292
+ expect(msg[:OBR][5].children[1].set_id).to eq "2"
293
+
294
+ final_output = msg.to_hl7
295
+ expect(orig_output).not_to eq(final_output)
296
+ end
297
+
298
+ it "returns each segment's index" do
299
+ msg = HL7::Message.parse( @simple_msh_txt )
300
+ expect(msg.index("PID")).to eq 1
301
+ expect(msg.index(:PID)).to eq 1
302
+ expect(msg.index("PV1")).to eq 2
303
+ expect(msg.index(:PV1)).to eq 2
304
+ expect(msg.index("TACOBELL")).to be_nil
305
+ expect(msg.index(nil)).to be_nil
306
+ expect(msg.index(1)).to be_nil
307
+ end
308
+
309
+ it 'validates the PID#admin_sex element' do
310
+ pid = HL7::Message::Segment::PID.new
311
+ expect { pid.admin_sex = "TEST" }.to raise_error(HL7::InvalidDataError)
312
+ expect { pid.admin_sex = "F" }.not_to raise_error
313
+ end
314
+
315
+ it 'can parse an empty segment' do
316
+ expect { HL7::Message.new @empty_segments_txt }.not_to raise_error
317
+ end
318
+ end
319
+ end