Class: WSDirector::Protocols::Phoenix

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

Overview

Constant Summary

Constants included from Utils

Utils::MULTIPLIER_FORMAT

Instance Attribute Summary collapse

Attributes included from Utils

#scale

Instance Method Summary collapse

Methods inherited from Base

#debug, #handle_step, #receive_all, #sleep, #to_proc, #wait_all

Methods included from Utils

#parse_multiplier

Constructor Details

#initializePhoenix

Returns a new instance of Phoenix.



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

def initialize(...)
  super

  @refs_counter = 3
  @join_refs_counter = 3
  @topics_to_join_ref = {}
end

Instance Attribute Details

#join_refs_counterObject

Returns the value of attribute join_refs_counter.



9
10
11
# File 'lib/wsdirector/protocols/phoenix.rb', line 9

def join_refs_counter
  @join_refs_counter
end

#refs_counterObject

Returns the value of attribute refs_counter.



9
10
11
# File 'lib/wsdirector/protocols/phoenix.rb', line 9

def refs_counter
  @refs_counter
end

#topics_to_join_refObject (readonly)

Returns the value of attribute topics_to_join_ref.



8
9
10
# File 'lib/wsdirector/protocols/phoenix.rb', line 8

def topics_to_join_ref
  @topics_to_join_ref
end

Instance Method Details

#init_client(**options) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/wsdirector/protocols/phoenix.rb', line 19

def init_client(**options)
  options[:query] ||= {}
  # Make sure we use the v2 of the protocol
  options[:query][:vsn] = "2.0.0"

  super
end

#join(step) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/wsdirector/protocols/phoenix.rb', line 27

def join(step)
  topic = step.fetch("topic")

  join_ref = join_refs_counter
  self.join_refs_counter += 1

  ref = refs_counter
  self.refs_counter += 1

  log { "Joining #{topic} (##{ref})" }

  cmd = new_command(:phx_join, topic, join_ref:, ref:)

  client.send(cmd.to_json)

  receive({"data>" => new_command(:phx_reply, topic, join_ref:, ref:, payload: {"status" => "ok"})})

  log(:done) { "Joined #{topic} (##{ref})" }
end

#leave(step) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/wsdirector/protocols/phoenix.rb', line 47

def leave(step)
  topic = step.fetch("topic")
  join_ref = topics_to_join_ref.fetch(topic)

  ref = refs_counter
  self.refs_counter += 1

  cmd = new_command(:phx_leave, topic, join_ref:, ref:)
  client.send(cmd.to_json)

  receive({"data>" => new_command(:phx_reply, topic, join_ref:, ref:, payload: {"status" => "ok"})})

  log(nil) { "Left #{topic} (##{join_ref})" }
end

#receive(step) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/wsdirector/protocols/phoenix.rb', line 75

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

  topic = step.fetch("topic")
  event = step.fetch("event")

  key = step.key?("data") ? "data" : "data>"
  payload = step.fetch(key)

  cmd = new_command(event, topic, payload:)

  super({key => cmd})
end

#send(step) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/wsdirector/protocols/phoenix.rb', line 62

def send(step)
  return super unless step.key?("topic")

  ref = refs_counter
  self.refs_counter += 1

  topic = step.fetch("topic")
  event = step.fetch("event")
  payload = step["data"]

  super({"data" => new_command(event, topic, payload:, ref:).to_json})
end