Class: Demux::Transmission

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

Overview

Demux::Transmission represents a signal being sent to an app

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.for_app(app_relation) ⇒ Object


13
14
15
# File 'app/models/demux/transmission.rb', line 13

def for_app(app_relation)
  joins(:app).merge(app_relation)
end

.queue(signal_attributes) ⇒ Object


17
18
19
20
21
# File 'app/models/demux/transmission.rb', line 17

def queue(signal_attributes)
  create(signal_attributes.to_hash)
rescue ActiveRecord::RecordNotUnique
  # Unique index by status/uniqueness_hash
end

Instance Method Details

#save_receipt(receipt) ⇒ Object


38
39
40
41
42
43
44
45
# File 'app/models/demux/transmission.rb', line 38

def save_receipt(receipt)
  update(
    status: receipt.success? ? :success : :failure,
    response_code: receipt.http_code,
    response_body: receipt.response_body,
    request_headers: receipt.request_headers
  )
end

#signal_nameObject


47
48
49
# File 'app/models/demux/transmission.rb', line 47

def signal_name
  signal.signal_name
end

#signatureObject


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

def signature
  OpenSSL::HMAC.hexdigest("SHA256", app.secret, request_body)
end

#transmitObject


24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/models/demux/transmission.rb', line 24

def transmit
  return self unless attributes_required_to_transmit_present?

  update(
    request_url: app.signal_url,
    request_body: payload.to_json,
    status: :sending
  )

  save_receipt(Transmitter.new(self).transmit.receipt)

  self
end