metybur 0.2.0 → 0.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.
- checksums.yaml +4 -4
- data/README.md +28 -2
- data/lib/metybur/method.rb +28 -1
- data/lib/metybur/version.rb +1 -1
- data/spec/metybur_spec.rb +69 -5
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f69ccd216fe6e46fe9472a0975e31de3c15a9c14
|
4
|
+
data.tar.gz: 91fdac926fc0e44432c67da5e27a072e7e027ab8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 020974269a114c7ed5edba2b2d8a7168931d7dd61ea81064a8a1df0a18464a564b703eb9fcae536e8d3ec1526ee2affab60cab47e1f1b7443e11cc41cdb127b2
|
7
|
+
data.tar.gz: 9682e9bcd748d3aa069fab63548672a522dda50eae8dafe4beadea581641b95b095a7f35e6bae4720b176d2bea1357594eec7a818fd8ce68338d692d857d1122
|
data/README.md
CHANGED
@@ -150,8 +150,34 @@ messages.each { |message| puts message }
|
|
150
150
|
Instead, pass a block to the method. The block will get called once the result arrives.
|
151
151
|
|
152
152
|
```ruby
|
153
|
-
meteor.chat_messages(in_room: 'General') do
|
154
|
-
|
153
|
+
meteor.chat_messages(in_room: 'General') do
|
154
|
+
result.each { |message| puts message }
|
155
|
+
end
|
156
|
+
```
|
157
|
+
|
158
|
+
#### Errors
|
159
|
+
|
160
|
+
Your meteor methods might throw errors, or the method you call might not even exist. You can rescue these errors right in the method's result block:
|
161
|
+
|
162
|
+
```ruby
|
163
|
+
meteor.chat_messages(in_room: 'General') do
|
164
|
+
begin
|
165
|
+
result.each { |message| puts message }
|
166
|
+
rescue Metybur::MethodError => e
|
167
|
+
puts "An error ocurred: #{e}"
|
168
|
+
end
|
169
|
+
end
|
170
|
+
```
|
171
|
+
|
172
|
+
Note that the error is raised by the `result`. If your method doesn't return a result, you can still handle the error:
|
173
|
+
|
174
|
+
```ruby
|
175
|
+
meteor.post_chat_message('Hey there!', in_room: 'General') do
|
176
|
+
begin
|
177
|
+
raise_errors
|
178
|
+
rescue Metybur::MethodError => e
|
179
|
+
puts "An error ocurred: #{e}"
|
180
|
+
end
|
155
181
|
end
|
156
182
|
```
|
157
183
|
|
data/lib/metybur/method.rb
CHANGED
@@ -1,4 +1,30 @@
|
|
1
|
+
Metybur::MethodError = Class.new(Exception)
|
2
|
+
|
1
3
|
class Metybur::Method
|
4
|
+
class Result
|
5
|
+
def initialize(attributes, callback)
|
6
|
+
@attributes = attributes
|
7
|
+
@callback = callback
|
8
|
+
end
|
9
|
+
|
10
|
+
def publish
|
11
|
+
instance_eval(&@callback) if @callback
|
12
|
+
end
|
13
|
+
|
14
|
+
def result
|
15
|
+
error = @attributes[:error]
|
16
|
+
if error
|
17
|
+
fail(
|
18
|
+
Metybur::MethodError,
|
19
|
+
"error: #{error[:error]}, reason: #{error[:reason]}, details: #{error[:details]}"
|
20
|
+
)
|
21
|
+
else
|
22
|
+
@attributes[:result]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
alias_method :raise_errors, :result
|
26
|
+
end
|
27
|
+
|
2
28
|
def initialize(name, websocket)
|
3
29
|
require 'securerandom'
|
4
30
|
|
@@ -28,6 +54,7 @@ class Metybur::Method
|
|
28
54
|
|
29
55
|
def handle_message(attributes)
|
30
56
|
id = attributes[:id]
|
31
|
-
|
57
|
+
result = Result.new(attributes, @callbacks[id])
|
58
|
+
result.publish
|
32
59
|
end
|
33
60
|
end
|
data/lib/metybur/version.rb
CHANGED
data/spec/metybur_spec.rb
CHANGED
@@ -319,11 +319,12 @@ describe Metybur do
|
|
319
319
|
|
320
320
|
it 'passes the result to a block' do
|
321
321
|
meteor = Metybur.connect(url)
|
322
|
+
expectation = proc { |result| expect(result[:price]).to eq 99 }
|
322
323
|
|
323
324
|
wait_for_callback do |done|
|
324
|
-
meteor.get_product(27) do
|
325
|
+
meteor.get_product(27) do
|
325
326
|
done.call
|
326
|
-
|
327
|
+
expectation.call(result)
|
327
328
|
end
|
328
329
|
|
329
330
|
id = last_sent_message[:id]
|
@@ -338,7 +339,7 @@ describe Metybur do
|
|
338
339
|
it "doesn't trigger the callback for ping messages" do
|
339
340
|
meteor = Metybur.connect(url)
|
340
341
|
|
341
|
-
meteor.get_product(27) do
|
342
|
+
meteor.get_product(27) do
|
342
343
|
fail
|
343
344
|
end
|
344
345
|
|
@@ -347,11 +348,12 @@ describe Metybur do
|
|
347
348
|
|
348
349
|
it 'triggers the callback with the right result' do
|
349
350
|
meteor = Metybur.connect(url)
|
351
|
+
expectation = proc { |result| expect(result[:price]).to eq 99 }
|
350
352
|
|
351
353
|
wait_for_callback do |done|
|
352
|
-
meteor.get_product(27) do
|
354
|
+
meteor.get_product(27) do
|
353
355
|
done.call
|
354
|
-
|
356
|
+
expectation.call(result)
|
355
357
|
end
|
356
358
|
|
357
359
|
id = last_sent_message[:id]
|
@@ -367,5 +369,67 @@ describe Metybur do
|
|
367
369
|
}.to_json)
|
368
370
|
end
|
369
371
|
end
|
372
|
+
|
373
|
+
it 'raises an error if an exception occurs in the method' do
|
374
|
+
meteor = Metybur.connect(url)
|
375
|
+
error = FFaker::Lorem.word
|
376
|
+
reason = FFaker::Lorem.sentence
|
377
|
+
details = FFaker::Lorem.paragraph
|
378
|
+
expectation = proc { |e| expect(e.message).to eq "error: #{error}, reason: #{reason}, details: #{details}" }
|
379
|
+
|
380
|
+
wait_for_callback do |done|
|
381
|
+
meteor.get_product(27) do
|
382
|
+
begin
|
383
|
+
done.call
|
384
|
+
result
|
385
|
+
fail("result didn't trigger error")
|
386
|
+
rescue Metybur::MethodError => e
|
387
|
+
expectation.call(e)
|
388
|
+
end
|
389
|
+
end
|
390
|
+
|
391
|
+
id = last_sent_message[:id]
|
392
|
+
websocket.receive({
|
393
|
+
msg: 'result',
|
394
|
+
id: id,
|
395
|
+
error: {
|
396
|
+
error: error,
|
397
|
+
reason: reason,
|
398
|
+
details: details
|
399
|
+
}
|
400
|
+
}.to_json)
|
401
|
+
end
|
402
|
+
end
|
403
|
+
|
404
|
+
it 'raises an error if an exception occurs in the method' do
|
405
|
+
meteor = Metybur.connect(url)
|
406
|
+
error = FFaker::Lorem.word
|
407
|
+
reason = FFaker::Lorem.sentence
|
408
|
+
details = FFaker::Lorem.paragraph
|
409
|
+
expectation = proc { |e| expect(e.message).to eq "error: #{error}, reason: #{reason}, details: #{details}" }
|
410
|
+
|
411
|
+
wait_for_callback do |done|
|
412
|
+
meteor.get_product(27) do
|
413
|
+
begin
|
414
|
+
done.call
|
415
|
+
raise_errors
|
416
|
+
fail("result didn't trigger error")
|
417
|
+
rescue Metybur::MethodError => e
|
418
|
+
expectation.call(e)
|
419
|
+
end
|
420
|
+
end
|
421
|
+
|
422
|
+
id = last_sent_message[:id]
|
423
|
+
websocket.receive({
|
424
|
+
msg: 'result',
|
425
|
+
id: id,
|
426
|
+
error: {
|
427
|
+
error: error,
|
428
|
+
reason: reason,
|
429
|
+
details: details
|
430
|
+
}
|
431
|
+
}.to_json)
|
432
|
+
end
|
433
|
+
end
|
370
434
|
end
|
371
435
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: metybur
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Clemens Helm
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-05-
|
11
|
+
date: 2015-05-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faye-websocket
|