Class: WSDirector::Protocols::Base
- Inherits:
-
Object
- Object
- WSDirector::Protocols::Base
- Defined in:
- lib/wsdirector/protocols/base.rb
Overview
Base protocol describes basic actions
Direct Known Subclasses
Instance Method Summary collapse
- #handle_step(step) ⇒ Object
- #init_client(**options) ⇒ Object
-
#initialize(task) ⇒ Base
constructor
A new instance of Base.
- #receive(step) ⇒ Object
- #send(step) ⇒ Object
- #to_proc ⇒ Object
- #wait_all(_step) ⇒ Object
Constructor Details
#initialize(task) ⇒ Base
Returns a new instance of Base.
7 8 9 |
# File 'lib/wsdirector/protocols/base.rb', line 7 def initialize(task) @task = task end |
Instance Method Details
#handle_step(step) ⇒ Object
15 16 17 18 19 |
# File 'lib/wsdirector/protocols/base.rb', line 15 def handle_step(step) type = step.delete("type") raise Error, "Unknown step: #{type}" unless respond_to?(type) public_send(type, step) end |
#init_client(**options) ⇒ Object
11 12 13 |
# File 'lib/wsdirector/protocols/base.rb', line 11 def init_client(**) @client = build_client(**) end |
#receive(step) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/wsdirector/protocols/base.rb', line 21 def receive(step) expected = step.fetch("data") received = client.receive receive_matches?(expected, received) rescue ThreadError raise NoMessageError, "Expected to receive #{expected} but nothing has been received" end |
#send(step) ⇒ Object
29 30 31 32 33 |
# File 'lib/wsdirector/protocols/base.rb', line 29 def send(step) data = step.fetch("data") data = JSON.generate(data) if data.is_a?(Hash) client.send(data) end |
#to_proc ⇒ Object
39 40 41 |
# File 'lib/wsdirector/protocols/base.rb', line 39 def to_proc proc { |step| handle_step(step) } end |
#wait_all(_step) ⇒ Object
35 36 37 |
# File 'lib/wsdirector/protocols/base.rb', line 35 def wait_all(_step) task.global_holder.wait_all end |