Module: Upkeep::Rails::ClientSubscription

Defined in:
lib/upkeep/rails/client_subscription.rb

Constant Summary collapse

CHANNEL =
"Upkeep::Rails::Cable::Channel"

Class Method Summary collapse

Class Method Details

.inject(html, identity:, subscription:) ⇒ Object



13
14
15
16
17
# File 'lib/upkeep/rails/client_subscription.rb', line 13

def inject(html, identity:, subscription:)
  marker = marker_for(identity: identity, subscription: subscription)
  insert_before_closing("body", html, marker) ||
    "#{html}#{marker}"
end

.insert_before_closing(tag, html, marker) ⇒ Object



37
38
39
40
41
42
# File 'lib/upkeep/rails/client_subscription.rb', line 37

def insert_before_closing(tag, html, marker)
  index = html.rindex(%(</#{tag}>)) || html.rindex(%(</#{tag.upcase}>))
  return unless index

  "#{html[0...index]}#{marker}#{html[index..]}"
end

.marker_for(identity:, subscription:) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/upkeep/rails/client_subscription.rb', line 19

def marker_for(identity:, subscription:)
  payload = JSON.generate(
    channel: CHANNEL,
    subscription_id: subscription.id,
    activation_token: ActivationToken.generate(subscription),
    stream_name: identity.stream_name
  ).gsub("</", '<\/')

  id = "upkeep-subscription-source-#{subscription.id}"

  [
    %(<upkeep-subscription-source id="#{CGI.escapeHTML(id)}" ),
    %(data-upkeep-subscription data-turbo-temporary>),
    CGI.escapeHTML(payload),
    %(</upkeep-subscription-source>)
  ].join
end