simple_hl7 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +20 -0
- data/lib/simple_hl7/composite.rb +1 -3
- data/lib/simple_hl7/message.rb +48 -6
- data/lib/simple_hl7/segment.rb +8 -0
- data/lib/simple_hl7/subcomponent.rb +7 -1
- data/lib/simple_hl7/version.rb +1 -1
- data/spec/simple_hl7/component_spec.rb +5 -0
- data/spec/simple_hl7/field_spec.rb +5 -0
- data/spec/simple_hl7/message_spec.rb +25 -0
- data/spec/simple_hl7/segment_spec.rb +7 -0
- data/spec/simple_hl7/subcomponent_spec.rb +18 -0
- metadata +2 -2
data/README.md
CHANGED
@@ -162,6 +162,18 @@ Outputs:
|
|
162
162
|
MSH|^~\&|
|
163
163
|
NTE|||Testing \T\ escaping notes
|
164
164
|
```
|
165
|
+
### Segment Separator
|
166
|
+
|
167
|
+
Some nonstandard HL7 implementations require a different segment separator
|
168
|
+
character than "\r". To make this change pass the `segment_separator` option to
|
169
|
+
the `Message` constructor.
|
170
|
+
|
171
|
+
```ruby
|
172
|
+
msg = SimpleHL7::Message.new(segment_separator: "\r\n")
|
173
|
+
msg.pid[3] = "123456"
|
174
|
+
msg.to_hl7
|
175
|
+
=> "MSH|^~\\&|\r\nPID|||123456"
|
176
|
+
```
|
165
177
|
|
166
178
|
### Parsing
|
167
179
|
|
@@ -179,6 +191,14 @@ msg.pid[5][1].to_s
|
|
179
191
|
=> "Doe"
|
180
192
|
```
|
181
193
|
|
194
|
+
If the string to be parsed contains a nonstandard segment separator then it can
|
195
|
+
be passed into the `parse` method as well:
|
196
|
+
|
197
|
+
```ruby
|
198
|
+
hl7_str = "MSH|^~\\&|||||||ADT^A04|12345678|D|2.5\r\nPID|||454545||Doe^John"
|
199
|
+
msg = SimpleHL7::Message.parse(hl7_str, segment_separator: "\r\n")
|
200
|
+
```
|
201
|
+
|
182
202
|
## Contributing
|
183
203
|
|
184
204
|
1. Fork it
|
data/lib/simple_hl7/composite.rb
CHANGED
@@ -10,9 +10,7 @@ module SimpleHL7
|
|
10
10
|
def initialize(value = nil)
|
11
11
|
@subcomposites = {}
|
12
12
|
cls = self.class
|
13
|
-
|
14
|
-
@subcomposites[cls.start_index] = cls.subcomposite_class.new(value)
|
15
|
-
end
|
13
|
+
@subcomposites[cls.start_index] = cls.subcomposite_class.new(value)
|
16
14
|
end
|
17
15
|
|
18
16
|
# Set the value of the specified subcomposite.
|
data/lib/simple_hl7/message.rb
CHANGED
@@ -1,8 +1,18 @@
|
|
1
1
|
module SimpleHL7
|
2
2
|
class Message
|
3
|
-
|
3
|
+
# Constructor
|
4
|
+
# @param options [Hash] Options for the Message, keys are symbols, accepted
|
5
|
+
# values are:
|
6
|
+
# default_msh [boolean] True to create a default MSH segment, false
|
7
|
+
# to create the message with no segments. Defaults to true.
|
8
|
+
# segment_separator [String] The string to place between segments when
|
9
|
+
# generating HL7, defaults to "\r".
|
10
|
+
def initialize(options = nil)
|
11
|
+
default_opts = {default_msh: true, segment_separator: "\r"}
|
12
|
+
opts = default_opts.merge(options || {})
|
13
|
+
@segment_separator = opts[:segment_separator]
|
4
14
|
@segments = []
|
5
|
-
@segments << MSHSegment.new if default_msh
|
15
|
+
@segments << MSHSegment.new if opts[:default_msh]
|
6
16
|
end
|
7
17
|
|
8
18
|
def method_missing(meth, *args, &block)
|
@@ -16,15 +26,29 @@ module SimpleHL7
|
|
16
26
|
end
|
17
27
|
end
|
18
28
|
|
29
|
+
# Generate a HL7 string from this message
|
30
|
+
#
|
31
|
+
# @return [String] The generated HL7 string
|
19
32
|
def to_hl7
|
20
33
|
separator_chars = get_named_segment('MSH').separator_chars
|
21
|
-
@segments.map {|s| s.to_hl7(separator_chars)}.join(
|
34
|
+
@segments.map {|s| s.to_hl7(separator_chars)}.join(@segment_separator)
|
22
35
|
end
|
23
36
|
|
37
|
+
# Get an array representation of the HL7 message. This can be useful
|
38
|
+
# to help debug problems.
|
39
|
+
#
|
40
|
+
# @return [Array] The HL7 message as an array where each subcomposites is
|
41
|
+
# also represented as an array.
|
24
42
|
def to_a
|
25
43
|
@segments.reduce([]) {|a, s| a << s.to_a}
|
26
44
|
end
|
27
45
|
|
46
|
+
# Get the specified segment
|
47
|
+
#
|
48
|
+
# @param name [String] The 3 letter segment name, case insensitive
|
49
|
+
# @param index [Integer] The segment number to get if there are multiple
|
50
|
+
# in the message. This index starts at 1 because that's what most HL7
|
51
|
+
# specs do.
|
28
52
|
def segment(name, index=1)
|
29
53
|
all = all_segments(name)
|
30
54
|
seg = nil
|
@@ -32,12 +56,20 @@ module SimpleHL7
|
|
32
56
|
seg
|
33
57
|
end
|
34
58
|
|
59
|
+
# Add a segment to the message
|
60
|
+
#
|
61
|
+
# @param name [String] The 3 letter segment name, case insensitive.
|
62
|
+
# @return [Segment] The segment that was just created
|
35
63
|
def add_segment(name)
|
36
64
|
segment = Segment.new(name)
|
37
65
|
@segments << segment
|
38
66
|
segment
|
39
67
|
end
|
40
68
|
|
69
|
+
# Appends a segment to the message
|
70
|
+
#
|
71
|
+
# @param segment [Segment] The segment to append.
|
72
|
+
# @return [Segment] The segment just appended
|
41
73
|
def append_segment(segment)
|
42
74
|
@segments << segment
|
43
75
|
segment
|
@@ -60,9 +92,19 @@ module SimpleHL7
|
|
60
92
|
segment
|
61
93
|
end
|
62
94
|
|
63
|
-
|
64
|
-
|
65
|
-
|
95
|
+
# Parses a HL7 string into a Message
|
96
|
+
#
|
97
|
+
# @param str [String] The string to parse.
|
98
|
+
# @param options [Hash] Options for parsing, keys are symbols, accepted
|
99
|
+
# values:
|
100
|
+
# segment_separator [String] The string to place between segments when
|
101
|
+
# generating HL7, defaults to "\r".
|
102
|
+
# @return [Message] The parsed HL7 Message
|
103
|
+
def self.parse(str, options = nil)
|
104
|
+
default_opts = {default_msh: true, segment_separator: "\r"}
|
105
|
+
opts = default_opts.merge(options || {})
|
106
|
+
msg = new(default_msh: false)
|
107
|
+
segment_strs = str.split(opts[:segment_separator])
|
66
108
|
msh = MSHSegment.parse(segment_strs[0])
|
67
109
|
msg.append_segment(msh)
|
68
110
|
segment_strs[1, segment_strs.length].each do |seg_str|
|
data/lib/simple_hl7/segment.rb
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
module SimpleHL7
|
2
|
-
class Subcomponent
|
2
|
+
class Subcomponent
|
3
|
+
attr_accessor :value
|
4
|
+
|
5
|
+
def initialize(value)
|
6
|
+
@value = value.nil? ? '' : value
|
7
|
+
end
|
8
|
+
|
3
9
|
def to_hl7(separator_chars)
|
4
10
|
hl7 = value
|
5
11
|
hl7.gsub!(separator_chars.escape, "\\E\\")
|
data/lib/simple_hl7/version.rb
CHANGED
@@ -9,6 +9,11 @@ module SimpleHL7
|
|
9
9
|
field[2] = "bar"
|
10
10
|
field.to_hl7(SeparatorCharacters.defaults).should == "foo&bar"
|
11
11
|
end
|
12
|
+
|
13
|
+
it "returns an empty string when initialized with a nil" do
|
14
|
+
field = Component.new(nil)
|
15
|
+
field.to_hl7(SeparatorCharacters.defaults).should == ''
|
16
|
+
end
|
12
17
|
end
|
13
18
|
|
14
19
|
describe "[]" do
|
@@ -10,6 +10,11 @@ module SimpleHL7
|
|
10
10
|
field.r(2)[1] = "bar"
|
11
11
|
field.to_hl7(SeparatorCharacters.defaults).should == 'foo^baz~bar'
|
12
12
|
end
|
13
|
+
|
14
|
+
it "generates an empty message for a nil value" do
|
15
|
+
field = Field.new(nil)
|
16
|
+
field.to_hl7(SeparatorCharacters.defaults).should == ''
|
17
|
+
end
|
13
18
|
end
|
14
19
|
end
|
15
20
|
end
|
@@ -10,6 +10,23 @@ module SimpleHL7
|
|
10
10
|
msg.pid[5][2] = "Test"
|
11
11
|
msg.to_hl7.should == "MSH|^~\\&||||accountid\rPID|||||User^Test"
|
12
12
|
end
|
13
|
+
|
14
|
+
it "generates an hl7 message with a nil field" do
|
15
|
+
msg = Message.new
|
16
|
+
msg.msh[6] = "accountid"
|
17
|
+
msg.pid[5] = "User"
|
18
|
+
msg.pid[5][2] = "Test"
|
19
|
+
msg.msh[5] = nil
|
20
|
+
msg.to_hl7.should == "MSH|^~\\&||||accountid\rPID|||||User^Test"
|
21
|
+
end
|
22
|
+
|
23
|
+
it "generates a message with a non-default segment separator" do
|
24
|
+
msg = Message.new(segment_separator: "\r\n")
|
25
|
+
msg.msh[6] = "accountid"
|
26
|
+
msg.pid[5] = "User"
|
27
|
+
msg.pid[5][2] = "Test"
|
28
|
+
msg.to_hl7.should == "MSH|^~\\&||||accountid\r\nPID|||||User^Test"
|
29
|
+
end
|
13
30
|
end
|
14
31
|
|
15
32
|
describe "#add_segment" do
|
@@ -32,6 +49,14 @@ module SimpleHL7
|
|
32
49
|
msg.pid[5][2].to_s.should == "Test"
|
33
50
|
msg.pid[5].r(2)[1].to_s.should == "Repeat"
|
34
51
|
end
|
52
|
+
|
53
|
+
it "properly parses a hl7 string with nonstandard separators" do
|
54
|
+
msg = Message.parse("MSH|^~\\&||||accountid\r\nPID|||||User^Test",
|
55
|
+
segment_separator: "\r\n")
|
56
|
+
msg.msh[6].to_s.should == "accountid"
|
57
|
+
msg.pid[5].to_s.should == "User"
|
58
|
+
msg.pid[5][2].to_s.should == "Test"
|
59
|
+
end
|
35
60
|
end
|
36
61
|
end
|
37
62
|
end
|
@@ -3,6 +3,13 @@ require "simple_hl7"
|
|
3
3
|
|
4
4
|
module SimpleHL7
|
5
5
|
describe Segment do
|
6
|
+
describe "#initialize" do
|
7
|
+
it "is not case sensitive to the segment name" do
|
8
|
+
seg = Segment.new('pId')
|
9
|
+
seg.name.should == 'PID'
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
6
13
|
describe "#to_hl7" do
|
7
14
|
it "generates a message using the specified separator chars" do
|
8
15
|
sep_chars = SeparatorCharacters.defaults
|
@@ -2,6 +2,17 @@ require "simple_hl7"
|
|
2
2
|
|
3
3
|
module SimpleHL7
|
4
4
|
describe Subcomponent do
|
5
|
+
describe "#initialize" do
|
6
|
+
it "changes a nil value to an empty string" do
|
7
|
+
subc = Subcomponent.new(nil)
|
8
|
+
subc.value.should == ''
|
9
|
+
end
|
10
|
+
|
11
|
+
it "initializes with a value" do
|
12
|
+
subc = Subcomponent.new('test')
|
13
|
+
subc.value.should == 'test'
|
14
|
+
end
|
15
|
+
end
|
5
16
|
describe "#to_hl7" do
|
6
17
|
it "returns the value" do
|
7
18
|
subf = Subcomponent.new('test')
|
@@ -18,6 +29,13 @@ module SimpleHL7
|
|
18
29
|
default_chars = SeparatorCharacters.defaults
|
19
30
|
subc.to_hl7(default_chars).should == "\\E\\\\T\\\\S\\\\R\\\\F\\"
|
20
31
|
end
|
32
|
+
|
33
|
+
context "when initialized with an empty string" do
|
34
|
+
it "returns an empty string" do
|
35
|
+
subc = Subcomponent.new(nil)
|
36
|
+
subc.to_hl7(SeparatorCharacters.defaults).should == ''
|
37
|
+
end
|
38
|
+
end
|
21
39
|
end
|
22
40
|
|
23
41
|
describe "#parse" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_hl7
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-02-
|
12
|
+
date: 2014-02-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: pry
|