Module: Turbo::Train

Extended by:
Streams::ActionHelper, Broadcasts
Defined in:
lib/turbo/train/train.rb,
lib/turbo/train/config.rb,
lib/turbo/train/engine.rb,
lib/turbo/train/version.rb,
lib/turbo/train/base_server.rb,
lib/turbo/train/test_helper.rb,
lib/turbo/train/test_server.rb,
lib/turbo/train/fanout_server.rb,
lib/turbo/train/mercure_server.rb,
lib/turbo/train/anycable_server.rb,
app/jobs/turbo/train/broadcast_job.rb,
app/jobs/turbo/train/action_broadcast_job.rb,
app/jobs/turbo/train/test/application_job.rb,
app/models/turbo/train/test/application_record.rb,
app/mailers/turbo/train/test/application_mailer.rb,
app/controllers/turbo/train/test/application_controller.rb

Defined Under Namespace

Modules: Broadcastable, Broadcasts, StreamsHelper, Test, TestHelper Classes: ActionBroadcastJob, AnycableConfiguration, AnycableServer, BaseServer, BroadcastJob, Configuration, Engine, FanoutConfiguration, FanoutServer, MercureConfiguration, MercureServer, TestServer

Constant Summary collapse

ALGORITHM =
"HS256"
VERSION =
"0.4.0"

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Broadcasts

broadcast, broadcast_action_later_to, broadcast_action_to, broadcast_after_later_to, broadcast_after_to, broadcast_append_later_to, broadcast_append_to, broadcast_before_later_to, broadcast_before_to, broadcast_prepend_later_to, broadcast_prepend_to, broadcast_remove_later_to, broadcast_remove_to, broadcast_render_later_to, broadcast_render_to, broadcast_replace_later_to, broadcast_replace_to, broadcast_update_later_to, broadcast_update_to

Class Attribute Details

.configurationObject

Returns the value of attribute configuration.



97
98
99
# File 'lib/turbo/train/config.rb', line 97

def configuration
  @configuration
end

Class Method Details

.anycable_serverObject

Raises:

  • (ArgumentError)


46
47
48
49
50
# File 'lib/turbo/train/train.rb', line 46

def anycable_server
  raise ArgumentError, "Anycable configuration is missing" unless configuration.anycable

  @anycable_server ||= AnycableServer.new(configuration)
end

.configure {|configuration| ... } ⇒ Object

Yields:



99
100
101
102
# File 'lib/turbo/train/config.rb', line 99

def configure
  self.configuration ||= Configuration.new
  yield(configuration)
end

.encode(payload) ⇒ Object



12
13
14
15
# File 'lib/turbo/train/train.rb', line 12

def encode(payload)
  structured_payload = { mercure: { payload: payload } }
  JWT.encode structured_payload, configuration.subscriber_key, ALGORITHM
end

.fanout_serverObject

Raises:

  • (ArgumentError)


40
41
42
43
44
# File 'lib/turbo/train/train.rb', line 40

def fanout_server
  raise ArgumentError, "Fanout configuration is missing" unless configuration.fanout

  @fanout_server ||= FanoutServer.new(configuration)
end

.mercure_serverObject

Raises:

  • (ArgumentError)


34
35
36
37
38
# File 'lib/turbo/train/train.rb', line 34

def mercure_server
  raise ArgumentError, "Mercure configuration is missing" unless configuration.mercure

  @mercure_server ||= MercureServer.new(configuration)
end

.server(server = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/turbo/train/train.rb', line 21

def server(server = nil)
  @server ||= case server || configuration.default_server
  when :mercure
    mercure_server
  when :fanout
    fanout_server
  when :anycable
    anycable_server
  else
    raise ArgumentError, "Unknown server: #{server}"
  end
end

.signed_stream_name(streamables) ⇒ Object



17
18
19
# File 'lib/turbo/train/train.rb', line 17

def signed_stream_name(streamables)
  Turbo.signed_stream_verifier.generate stream_name_from(streamables)
end

.stream_name_from(streamables) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/turbo/train/train.rb', line 52

def stream_name_from(streamables)
  if streamables.is_a?(Array)
    streamables.map  { |streamable| stream_name_from(streamable) }.join(":")
  else
    streamables.then { |streamable| streamable.try(:to_gid_param) || streamable.to_param }
  end
end