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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6cd1e4839ca1350e3e953be00f1428c1fbf31b84
4
- data.tar.gz: e344a1a7e9537edd9eeb1f925518880b9f431e17
3
+ metadata.gz: f69ccd216fe6e46fe9472a0975e31de3c15a9c14
4
+ data.tar.gz: 91fdac926fc0e44432c67da5e27a072e7e027ab8
5
5
  SHA512:
6
- metadata.gz: 863a4003723aade726d6a753a15dacb6d5666f7dd5b5453048ed8eb9557f48dfb940d530431d3cc38e358de4fcdd3fd73793bb4f1af8264e85919d12409e062e
7
- data.tar.gz: dd9b0fa2281be1a3441d6069c408b1b896e665dd93f5c3c57114ed9ce9fd2756295361cf7afccba14d299e4a019772bfaccbe1c336951ca7f545f535d7c59ca4
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 |messages|
154
- messages.each { |message| puts message }
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
 
@@ -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
- @callbacks[id].call attributes[:result] if @callbacks[id]
57
+ result = Result.new(attributes, @callbacks[id])
58
+ result.publish
32
59
  end
33
60
  end
@@ -1,3 +1,3 @@
1
1
  module Metybur
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
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 |product|
325
+ meteor.get_product(27) do
325
326
  done.call
326
- expect(product[:price]).to eq 99
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 |product|
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 |product|
354
+ meteor.get_product(27) do
353
355
  done.call
354
- expect(product[:price]).to eq 99
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.2.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 00:00:00.000000000 Z
11
+ date: 2015-05-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faye-websocket