WatersOfOblivion-heychell7 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/heychell7.rb +19 -3
- data/spec/heychell7_spec.rb +222 -8
- metadata +1 -1
data/lib/heychell7.rb
CHANGED
@@ -2,7 +2,7 @@ $:.unshift(File.dirname(__FILE__)) unless
|
|
2
2
|
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
3
|
|
4
4
|
module Heychell7
|
5
|
-
VERSION = '0.0.
|
5
|
+
VERSION = '0.0.3'
|
6
6
|
|
7
7
|
class ParseError < Exception; end
|
8
8
|
|
@@ -12,6 +12,21 @@ module Heychell7
|
|
12
12
|
@stack = []
|
13
13
|
end
|
14
14
|
|
15
|
+
class << self
|
16
|
+
def parse input = nil, &block
|
17
|
+
parser = Parser.new
|
18
|
+
return parser.parse_trigger &block if block_given?
|
19
|
+
return parser.parse_io input if input.is_a? IO
|
20
|
+
|
21
|
+
if input.respond_to?(:to_s) and !input.to_s.empty?
|
22
|
+
return parser.parse_file input if File.exist?(input)
|
23
|
+
return parser.parse_string input
|
24
|
+
end
|
25
|
+
|
26
|
+
raise ParseError.new "Unknown parser input"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
15
30
|
def parse_trigger &block
|
16
31
|
@trigger = block
|
17
32
|
Proc.new { |data|
|
@@ -27,7 +42,8 @@ module Heychell7
|
|
27
42
|
end
|
28
43
|
|
29
44
|
def parse_string str
|
30
|
-
|
45
|
+
str = "\x0b#{str}\x1c\r" unless str =~ /^\x0b.*\x1c\r$/
|
46
|
+
parse str
|
31
47
|
message
|
32
48
|
end
|
33
49
|
|
@@ -157,7 +173,7 @@ module Heychell7
|
|
157
173
|
end
|
158
174
|
|
159
175
|
def to_hl7
|
160
|
-
@data
|
176
|
+
@data.to_s
|
161
177
|
end
|
162
178
|
end
|
163
179
|
|
data/spec/heychell7_spec.rb
CHANGED
@@ -1,11 +1,225 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/spec_helper.rb'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
3
|
+
module Heychell7
|
4
|
+
describe Parser do
|
5
|
+
before do
|
6
|
+
@parser = Parser.new
|
7
|
+
end
|
8
|
+
|
9
|
+
describe :parse do
|
10
|
+
before do
|
11
|
+
@parser = mock "Parser"
|
12
|
+
Parser.stub! :new => @parser
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should delegate to parse_trigger if a block is given" do
|
16
|
+
@parser.should_receive(:parse_trigger)
|
17
|
+
Parser.parse do end
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should delegate to parse_io if an IO is given" do
|
21
|
+
io = mock "IO"
|
22
|
+
io.stub! :is_a? => true
|
23
|
+
@parser.should_receive(:parse_io).with io
|
24
|
+
Parser.parse io
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should delegate to parse_file if a existant file is given" do
|
28
|
+
File.should_receive(:exist?).with("filename").and_return true
|
29
|
+
@parser.should_receive(:parse_file).with("filename")
|
30
|
+
Parser.parse "filename"
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should delegate to parse if it can become a string" do
|
34
|
+
@parser.should_receive(:parse_string).with("the string")
|
35
|
+
Parser.parse "the string"
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should raise a ParserError if everything else is exhausted" do
|
39
|
+
lambda { Parser.parse nil }.should raise_error ParseError
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe :parse_trigger do
|
44
|
+
before do
|
45
|
+
@parser = Parser.new
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should return a block which calls parse" do
|
49
|
+
@parser.should_receive(:parse).with("foo")
|
50
|
+
@parser.parse_trigger.call "foo"
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should call the given block when parsing is complete" do
|
54
|
+
lambda {
|
55
|
+
@parser.parse_trigger do |msg|
|
56
|
+
fail if msg.is_a? Message
|
57
|
+
end.call "\x0b\x1c\r"
|
58
|
+
}.should raise_error RuntimeError
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe :parse_io do
|
63
|
+
before do
|
64
|
+
@io = mock "IO"
|
65
|
+
@io.stub!(:read).and_return "foo", "bar", ""
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should read until there is nothing else to read" do
|
69
|
+
@io.should_receive(:read).exactly(3).times
|
70
|
+
@parser.parse_io @io
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should pass what it reads to parse" do
|
74
|
+
@parser.should_receive(:parse).ordered.with "foo"
|
75
|
+
@parser.should_receive(:parse).ordered.with "bar"
|
76
|
+
@parser.should_not_receive(:parse).with ""
|
77
|
+
@parser.parse_io @io
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe :parse_file do
|
82
|
+
before do
|
83
|
+
@file = mock "file"
|
84
|
+
@file.stub! :read => ""
|
85
|
+
File.stub! :open => @file
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should open the file" do
|
89
|
+
File.should_receive(:open).with("filename", "r")
|
90
|
+
@parser.parse_file "filename"
|
91
|
+
end
|
92
|
+
|
93
|
+
it "should pass the opened file to parse_io" do
|
94
|
+
@parser.should_receive(:parse_io).with(@file)
|
95
|
+
@parser.parse_file "filename"
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
describe :parse_string do
|
100
|
+
it "should parse the string" do
|
101
|
+
@parser.should_receive(:parse).with("\x0bthe string\x1c\r")
|
102
|
+
@parser.parse_string "\x0bthe string\x1c\r"
|
103
|
+
end
|
104
|
+
|
105
|
+
it "should automatically add the MLLP bits" do
|
106
|
+
@parser.should_receive(:parse).with("\x0bthe string\x1c\r")
|
107
|
+
@parser.parse_string "the string"
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
it "should be able to parse HL7" do
|
112
|
+
example_hl7 = <<-EOF
|
113
|
+
one^two|three
|
114
|
+
four|five^six^seven|eight
|
115
|
+
EOF
|
116
|
+
|
117
|
+
Parser.parse(example_hl7).to_hl7.should == example_hl7
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
describe Message do
|
122
|
+
before do
|
123
|
+
@message = Message.new
|
124
|
+
end
|
125
|
+
|
126
|
+
it "should have a :segments attribute" do
|
127
|
+
@message.segments = [:foo, :bar]
|
128
|
+
@message.segments.should == [:foo, :bar]
|
129
|
+
end
|
130
|
+
|
131
|
+
it "should have no segments by default" do
|
132
|
+
@message.segments.should be_empty
|
133
|
+
end
|
134
|
+
|
135
|
+
it "should take the constructor arguments as segments" do
|
136
|
+
message = Message.new :foo, :bar
|
137
|
+
message.segments.should == [:foo, :bar]
|
138
|
+
end
|
139
|
+
|
140
|
+
it "should convert to HL7" do
|
141
|
+
@message.segments = [Segment.new(Field.new(Item.new("foo")), Field.new(Item.new("bar"))), Segment.new(Field.new(Item.new("baz")), Field.new(Item.new("bat")))]
|
142
|
+
@message.to_hl7.should == "foo|bar\nbaz|bat\n"
|
143
|
+
end
|
144
|
+
|
145
|
+
it "should convert to MLLP" do
|
146
|
+
@message.segments = [Segment.new(Field.new(Item.new("foo")), Field.new(Item.new("bar"))), Segment.new(Field.new(Item.new("baz")), Field.new(Item.new("bat")))]
|
147
|
+
@message.to_mllp.should == "\x0bfoo|bar\nbaz|bat\n\x1c\r"
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
describe Segment do
|
152
|
+
before do
|
153
|
+
@segment = Segment.new
|
154
|
+
end
|
155
|
+
|
156
|
+
it "should have a :fields attribute" do
|
157
|
+
@segment.fields = [:foo, :bar]
|
158
|
+
@segment.fields.should == [:foo, :bar]
|
159
|
+
end
|
160
|
+
|
161
|
+
it "should have no fields by default" do
|
162
|
+
@segment.fields.should be_empty
|
163
|
+
end
|
164
|
+
|
165
|
+
it "should take the constructor arguments as fields" do
|
166
|
+
segment = Segment.new :foo, :bar
|
167
|
+
segment.fields.should == [:foo, :bar]
|
168
|
+
end
|
169
|
+
|
170
|
+
it "should convert to HL7" do
|
171
|
+
@segment.fields = [Field.new(Item.new("foo"), Item.new("bar")), Field.new(Item.new("baz"))]
|
172
|
+
@segment.to_hl7.should == "foo^bar|baz"
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
describe Field do
|
177
|
+
before do
|
178
|
+
@field = Field.new
|
179
|
+
end
|
180
|
+
|
181
|
+
it "should have a :items attribute" do
|
182
|
+
@field.items = [:foo, :bar]
|
183
|
+
@field.items.should == [:foo, :bar]
|
184
|
+
end
|
185
|
+
|
186
|
+
it "should have no items by default" do
|
187
|
+
@field.items.should be_empty
|
188
|
+
end
|
189
|
+
|
190
|
+
it "should take the constructor arguments as items" do
|
191
|
+
field = Field.new :foo, :bar
|
192
|
+
field.items.should == [:foo, :bar]
|
193
|
+
end
|
194
|
+
|
195
|
+
it "should convert to HL7" do
|
196
|
+
@field.items = [Item.new("foo"), Item.new("bar")]
|
197
|
+
@field.to_hl7.should == "foo^bar"
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
describe Item do
|
202
|
+
before do
|
203
|
+
@item = Item.new
|
204
|
+
end
|
205
|
+
|
206
|
+
it "should have a :data attribute" do
|
207
|
+
@item.data = :foo
|
208
|
+
@item.data.should == :foo
|
209
|
+
end
|
210
|
+
|
211
|
+
it "should have no data by default" do
|
212
|
+
@item.data.should be_nil
|
213
|
+
end
|
214
|
+
|
215
|
+
it "should take the constructor arguments as data" do
|
216
|
+
item = Item.new :foo
|
217
|
+
item.data.should == :foo
|
218
|
+
end
|
219
|
+
|
220
|
+
it "should convert to HL7" do
|
221
|
+
@item.data = :foo
|
222
|
+
@item.to_hl7.should == "foo"
|
223
|
+
end
|
224
|
+
end
|
11
225
|
end
|