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



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

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

.insert_before_closing(tag, html, marker) ⇒ Object



29
30
31
32
33
34
# File 'lib/upkeep/rails/client_subscription.rb', line 29

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
# File 'lib/upkeep/rails/client_subscription.rb', line 19

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

  %(<script type="application/json" data-upkeep-subscription>#{payload}</script>)
end