Class: WSDirector::Protocols::ActionCable

Inherits:
Base
  • Object
show all
Defined in:
lib/wsdirector/protocols/action_cable.rb

Overview

ActionCable protocol

Constant Summary collapse

WELCOME_MSG =
{type: "welcome"}.to_json
PING_IGNORE =
/['"]type['"]:\s*['"]ping['"]/

Constants included from Utils

Utils::MULTIPLIER_FORMAT

Instance Method Summary collapse

Methods inherited from Base

#debug, #handle_step, #initialize, #send, #sleep, #to_proc, #wait_all

Methods included from Utils

#parse_multiplier

Constructor Details

This class inherits a constructor from WSDirector::Protocols::Base

Instance Method Details

#init_client(**options) ⇒ Object

Add ping ignore and make sure that we receive Welcome message



11
12
13
14
15
16
17
# File 'lib/wsdirector/protocols/action_cable.rb', line 11

def init_client(**options)
  options[:ignore] ||= [PING_IGNORE]

  super(**options)

  receive("data" => WELCOME_MSG)
end

#perform(step) ⇒ Object

Raises:



34
35
36
37
38
39
40
41
42
43
# File 'lib/wsdirector/protocols/action_cable.rb', line 34

def perform(step)
  identifier = extract_identifier(step)
  action = step.delete("action")

  raise Error, "Action is missing" unless action

  data = step.fetch("data", {}).merge(action: action).to_json

  client.send({command: "message", data: data, identifier: identifier}.to_json)
end

#receive(step) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/wsdirector/protocols/action_cable.rb', line 45

def receive(step)
  return super unless step.key?("channel")

  identifier = extract_identifier(step)
  message = step.fetch("data", {})
  super("data" => {"identifier" => identifier, "message" => message})
end

#receive_all(step) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/wsdirector/protocols/action_cable.rb', line 53

def receive_all(step)
  messages = step["messages"]

  return super if messages.nil? || messages.empty?

  messages.each do |msg|
    next unless msg.key?("channel")
    identifier = extract_identifier(msg)
    msg["data"] = {"identifier" => identifier, "message" => msg["data"]}
  end

  super
end

#subscribe(step) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/wsdirector/protocols/action_cable.rb', line 19

def subscribe(step)
  identifier = extract_identifier(step)

  client.send({command: "subscribe", identifier: identifier}.to_json)

  begin
    receive(
      "data" => {"type" => "confirm_subscription", "identifier" => identifier}
    )
  rescue UnmatchedExpectationError => e
    raise unless /reject_subscription/.match?(e.message)
    raise UnmatchedExpectationError, "Subscription rejected to #{identifier}"
  end
end