uv-rays 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 026fc66184b7c4e5ef1f3495481ba5e469540fef
4
- data.tar.gz: f97d7dfacd1fa1afb821e75c4561f16a855bc397
3
+ metadata.gz: 144879cc69e227ca059cecbd2b4abf92d6fd0284
4
+ data.tar.gz: e57957308a1ba65488d48e8c451794161af23476
5
5
  SHA512:
6
- metadata.gz: 8bed0f3074a3000685e04f311c385fde102dc34494a16b7c5934418031514febe3c3f3e366c9885f874ebaf7dfeaacaa3309ec5719e761e18a95f1b51a463e39
7
- data.tar.gz: 148a330ff8f7a551116f1483b4a5a7453975b7af3b6553df7f2e71e97fd2982bc99982191a8486c9a30ddb79ed5c879e6a73032a7fe7440e5935df8eb0a76a2c
6
+ metadata.gz: 518a38b3f152899f36af5cd9488ffd1cd6ec7f983ca01cb8e909e8f831de9a7ee049fe8dbffe6e9b6bf2d2366e34907fb2ff5dd3f945b55de63a5ec6319510c7
7
+ data.tar.gz: 21751e286a6a2aa6e9dbd873c1712611dff769a52778fadaeb6d1b88600682c6be8d243f6995c64ae960578356cdc20b9a36e3b432704cf6bff59a0f8d2a4bae
data/LICENSE CHANGED
File without changes
data/README.md CHANGED
File without changes
data/Rakefile CHANGED
File without changes
data/lib/uv-rays.rb CHANGED
File without changes
File without changes
@@ -22,19 +22,22 @@ module UV
22
22
  def initialize(options)
23
23
  @delimiter = options[:delimiter]
24
24
  @indicator = options[:indicator]
25
+ @msg_length = options[:msg_length]
25
26
  @size_limit = options[:size_limit]
26
27
  @min_length = options[:min_length] || 1
27
28
  @verbose = options[:verbose] if @size_limit
28
29
  @encoding = options[:encoding]
29
30
 
30
- raise ArgumentError, 'no delimiter provided' unless @delimiter
31
-
32
- @input = ''
33
- if @encoding
34
- @input.force_encoding(@encoding)
35
- @delimiter.force_encoding(@encoding) if @delimiter.is_a?(String)
36
- @indicator.force_encoding(@encoding) if @indicator.is_a?(String)
31
+ if @delimiter
32
+ @extract_method = method(:delimiter_extract)
33
+ elsif @indicator && @msg_length
34
+ @length_index = @msg_length - 1
35
+ @extract_method = method(:length_extract)
36
+ else
37
+ raise ArgumentError, 'no delimiter provided'
37
38
  end
39
+
40
+ init_buffer
38
41
  end
39
42
 
40
43
  # Extract takes an arbitrary string of input data and returns an array of
@@ -50,6 +53,29 @@ module UV
50
53
  data.force_encoding(@encoding) if @encoding
51
54
  @input << data
52
55
 
56
+ @extract_method.call
57
+ end
58
+
59
+ # Flush the contents of the input buffer, i.e. return the input buffer even though
60
+ # a token has not yet been encountered.
61
+ #
62
+ # @return [String]
63
+ def flush
64
+ buffer = @input
65
+ reset
66
+ buffer
67
+ end
68
+
69
+ # @return [Boolean]
70
+ def empty?
71
+ @input.empty?
72
+ end
73
+
74
+
75
+ private
76
+
77
+
78
+ def delimiter_extract
53
79
  # Extract token-delimited entities from the input string with the split command.
54
80
  # There's a bit of craftiness here with the -1 parameter. Normally split would
55
81
  # behave no differently regardless of if the token lies at the very end of the
@@ -70,7 +96,38 @@ module UV
70
96
  @input = entities.pop
71
97
  end
72
98
 
73
- # Check to see if the buffer has exceeded capacity, if we're imposing a limit
99
+ check_buffer_limits
100
+
101
+ # Check min-length is met
102
+ entities.select! {|msg| msg.length >= @min_length}
103
+
104
+ return entities
105
+ end
106
+
107
+ def length_extract
108
+ messages = @input.split(@indicator, -1)
109
+ messages.shift # discard junk data
110
+
111
+ last = messages.pop || ''
112
+
113
+ # Select messages of the right size then remove junk data
114
+ messages.select! { |msg| msg.length >= @msg_length ? true : false }
115
+ messages.map! { |msg| msg[0..@length_index] }
116
+
117
+ if last.length >= @msg_length
118
+ messages << last[0..@length_index]
119
+ @input = last[@msg_length..-1]
120
+ else
121
+ @input = "#{@indicator}#{last}"
122
+ end
123
+
124
+ check_buffer_limits
125
+
126
+ return messages
127
+ end
128
+
129
+ # Check to see if the buffer has exceeded capacity, if we're imposing a limit
130
+ def check_buffer_limits
74
131
  if @size_limit && @input.size > @size_limit
75
132
  if @indicator && @indicator.respond_to?(:length) # check for regex
76
133
  # save enough of the buffer that if one character of the indicator were
@@ -83,32 +140,17 @@ module UV
83
140
  end
84
141
  raise 'input buffer exceeded limit' if @verbose
85
142
  end
86
-
87
- # Check min-length is met
88
- entities.select! {|msg| msg.length >= @min_length}
89
-
90
- return entities
91
- end
92
-
93
- # Flush the contents of the input buffer, i.e. return the input buffer even though
94
- # a token has not yet been encountered.
95
- #
96
- # @return [String]
97
- def flush
98
- buffer = @input
99
- reset
100
- buffer
101
143
  end
102
144
 
103
- # @return [Boolean]
104
- def empty?
105
- @input.empty?
145
+ def init_buffer
146
+ @input = ''
147
+ if @encoding
148
+ @input.force_encoding(@encoding)
149
+ @delimiter.force_encoding(@encoding) if @delimiter.is_a?(String)
150
+ @indicator.force_encoding(@encoding) if @indicator.is_a?(String)
151
+ end
106
152
  end
107
153
 
108
-
109
- private
110
-
111
-
112
154
  def reset
113
155
  @input = ''
114
156
  @input.force_encoding(@encoding) if @encoding
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -27,7 +27,7 @@ module UV
27
27
  class Scheduler
28
28
 
29
29
  def self.parse_in(o, quiet = false)
30
- # if o is an integer we are looking at seconds
30
+ # if o is an integer we are looking at ms
31
31
  o.is_a?(String) ? parse_duration(o, quiet) : o
32
32
  end
33
33
 
File without changes
@@ -1,3 +1,3 @@
1
1
  module UV
2
- VERSION = '1.0.4'
2
+ VERSION = '1.0.5'
3
3
  end
File without changes
@@ -173,6 +173,77 @@ describe UV::BufferedTokenizer do
173
173
  end
174
174
  end
175
175
 
176
+ describe 'indicator with length' do
177
+ before :each do
178
+ @buffer = UV::BufferedTokenizer.new({
179
+ indicator: "GO",
180
+ msg_length: 4 # length without the indicator
181
+ })
182
+ end
183
+
184
+ it "should not return anything when a complete message is not available" do
185
+ msg1 = "GO123"
186
+
187
+ result = @buffer.extract(msg1)
188
+ expect(result).to eq([])
189
+ end
190
+
191
+ it "should not return anything when the messages is empty" do
192
+ msg1 = ""
193
+
194
+ result = @buffer.extract(msg1)
195
+ expect(result).to eq([])
196
+ end
197
+
198
+ it "should tokenize messages where the data is a complete message" do
199
+ msg1 = "GO1234"
200
+
201
+ result = @buffer.extract(msg1)
202
+ expect(result).to eq(['1234'])
203
+ end
204
+
205
+ it "should discard data that is not relevant" do
206
+ msg1 = "1234-GO1234"
207
+
208
+ result = @buffer.extract(msg1)
209
+ expect(result).to eq(['1234'])
210
+ end
211
+
212
+ it "should return multiple complete messages" do
213
+ msg1 = "GO1234GOhome"
214
+
215
+ result = @buffer.extract(msg1)
216
+ expect(result).to eq(['1234', 'home'])
217
+ end
218
+
219
+ it "should discard data between multiple complete messages" do
220
+ msg1 = "1234-GO123412345-GOhome"
221
+
222
+ result = @buffer.extract(msg1)
223
+ expect(result).to eq(['1234', 'home'])
224
+ end
225
+
226
+ it "should tokenize messages where the indicator is split" do
227
+ msg1 = "GOtestG"
228
+ msg2 = "Owhoa"
229
+
230
+ result = @buffer.extract(msg1)
231
+ expect(result).to eq(['test'])
232
+ result = @buffer.extract(msg2)
233
+ expect(result).to eq(['whoa'])
234
+ end
235
+
236
+ it "should tokenize messages where the indicator is split and there is discard data" do
237
+ msg1 = "GOtest\n\r1234G"
238
+ msg2 = "Owhoa\n"
239
+
240
+ result = @buffer.extract(msg1)
241
+ expect(result).to eq(['test'])
242
+ result = @buffer.extract(msg2)
243
+ expect(result).to eq(['whoa'])
244
+ end
245
+ end
246
+
176
247
  describe 'buffer size limit with indicator' do
177
248
  before :each do
178
249
  @buffer = UV::BufferedTokenizer.new({
File without changes
File without changes
File without changes
File without changes
File without changes
data/uv-rays.gemspec CHANGED
File without changes
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uv-rays
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephen von Takach
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-17 00:00:00.000000000 Z
11
+ date: 2015-01-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: libuv
@@ -202,7 +202,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
202
202
  version: '0'
203
203
  requirements: []
204
204
  rubyforge_project:
205
- rubygems_version: 2.2.2
205
+ rubygems_version: 2.4.5
206
206
  signing_key:
207
207
  specification_version: 4
208
208
  summary: Abstractions for working with Libuv