Class: Demux::App

Inherits:
ApplicationRecord show all
Defined in:
app/models/demux/app.rb

Overview

Demux::App represents an external app that can be connected to an account in the parent application.

Constant Summary collapse

URL_REGEX =
%r{\A(http(s?)\://.+)?\z}i.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.listening_for(signal_name:, account_id:) ⇒ Object


20
21
22
23
24
25
26
27
28
29
# File 'app/models/demux/app.rb', line 20

def listening_for(signal_name:, account_id:)
  connections = Demux::Connection.listening_for(
    signal_name: signal_name,
    account_id: 
  )

  joins(:connections)
    .merge(connections)
    .where.not(signal_url: nil)
end

.transmission_requested_all(signal_attributes) ⇒ Object


43
44
45
46
47
# File 'app/models/demux/app.rb', line 43

def transmission_requested_all(signal_attributes)
  without_queued_transmissions_for(signal_attributes.hashed).each do |app|
    app.transmission_requested(signal_attributes)
  end
end

.without_queued_transmissions_for(signal_hash) ⇒ Object


31
32
33
34
35
36
37
38
39
40
41
# File 'app/models/demux/app.rb', line 31

def without_queued_transmissions_for(signal_hash)
  joins(
    <<~SQL
    LEFT OUTER JOIN demux_transmissions
    ON demux_transmissions.app_id = demux_apps.id
    AND demux_transmissions.status = 0
    AND demux_transmissions.uniqueness_hash = '#{signal_hash}'
    SQL
  )
    .where(demux_transmissions: { id: nil })
end

Instance Method Details

#signed_entry_url(data: {}, exp: 1.minute.from_now.to_i) ⇒ String

Return an entry url with JWT payload for authorization

Parameters:

  • data (Hash) (defaults to: {})

    data to sign and include in token payload

  • exp (Integer) (defaults to: 1.minute.from_now.to_i)

    expiration of token in seconds since the epoch

Returns:

  • (String)

    the entry url with signed token appended


61
62
63
64
# File 'app/models/demux/app.rb', line 61

def signed_entry_url(data: {}, exp: 1.minute.from_now.to_i)
  token = JWT.encode({ data: data, exp: exp }, secret, "HS256")
  "#{entry_url}?token=#{token}"
end

#transmission_requested(signal_attributes) ⇒ Object


50
51
52
# File 'app/models/demux/app.rb', line 50

def transmission_requested(signal_attributes)
  transmissions.queue(signal_attributes)
end