class Loco::Sender

Public Class Methods

call(recipient_s, payload = {}) click to toggle source
# File lib/loco/sender.rb, line 6
def call(recipient_s, payload = {})
  payload = with_idempotency_key(payload)
  recipients = recipient_s.is_a?(Array) ? recipient_s : [recipient_s]
  new.(recipients, payload)
end
new() click to toggle source
# File lib/loco/sender.rb, line 23
def initialize
  @uuids = []
end

Private Class Methods

with_idempotency_key(payload) click to toggle source
# File lib/loco/sender.rb, line 14
def with_idempotency_key(payload)
  hash = payload.clone
  hash[:loco] ||= {}
  hash[:loco][:idempotency_key] ||= hash[:idempotency_key] || SecureRandom.hex
  hash.delete(:idempotency_key)
  hash
end

Public Instance Methods

call(recipients, payload) click to toggle source
# File lib/loco/sender.rb, line 27
def call(recipients, payload)
  recipients.each do |recipient|
    case recipient
    when String then broadcast_to(recipient, payload)
    when Hash then process_hash(recipient, payload)
    else find_and_broadcast_to(recipient, payload)
    end
  end
  payload[:loco][:idempotency_key]
end

Private Instance Methods

broadcast_to(uuid, payload) click to toggle source
# File lib/loco/sender.rb, line 54
def broadcast_to(uuid, payload)
  return if @uuids.include?(uuid)

  @uuids << uuid
  NotificationCenterChannel.broadcast_to(uuid, payload)
end
find_and_broadcast_to(recipient, payload) click to toggle source
# File lib/loco/sender.rb, line 48
def find_and_broadcast_to(recipient, payload)
  WsConnectionFinder.(recipient) do |uuid|
    broadcast_to(uuid, payload)
  end
end
process_hash(recipient, payload) click to toggle source
# File lib/loco/sender.rb, line 40
def process_hash(recipient, payload)
  if recipient.key?('token')
    find_and_broadcast_to(recipient['token'], payload)
  elsif recipient.key?('class')
    find_and_broadcast_to(recipient['class'].constantize, payload)
  end
end