Class: CommandTower::Messaging::Rendering::ChannelRenderer

Inherits:
Object
  • Object
show all
Defined in:
app/services/command_tower/messaging/rendering/channel_renderer.rb

Constant Summary collapse

EMAIL_CHANNEL =
"email"
SMS_CHANNEL =
"sms"
PUSHOVER_CHANNEL =
"pushover"
SUPPORTED_CHANNELS =
[EMAIL_CHANNEL, SMS_CHANNEL, PUSHOVER_CHANNEL].freeze
TEMPLATE_DIR =
CommandTower::Engine.root.join("app/views/command_tower/messaging/rendering")
DEFAULT_TITLE =
"Notification"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(communication:, channel_key:, recipient_address:) ⇒ ChannelRenderer

Returns a new instance of ChannelRenderer.



29
30
31
32
33
# File 'app/services/command_tower/messaging/rendering/channel_renderer.rb', line 29

def initialize(communication:, channel_key:, recipient_address:)
  @communication = communication
  @channel_key = channel_key.to_s
  @recipient_address = recipient_address
end

Class Method Details

.render(communication:, channel_key:, recipient_address:) ⇒ Object



17
18
19
20
21
22
23
# File 'app/services/command_tower/messaging/rendering/channel_renderer.rb', line 17

def self.render(communication:, channel_key:, recipient_address:)
  new(
    communication:,
    channel_key:,
    recipient_address:,
  ).render
end

.supported_channel?(channel_key) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'app/services/command_tower/messaging/rendering/channel_renderer.rb', line 25

def self.supported_channel?(channel_key)
  SUPPORTED_CHANNELS.include?(channel_key.to_s)
end

Instance Method Details

#renderObject

Raises:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/services/command_tower/messaging/rendering/channel_renderer.rb', line 35

def render
  address = @recipient_address.to_s.strip
  raise RenderError.new(code: "recipient_missing") if address.empty?

  unless self.class.supported_channel?(@channel_key)
    raise RenderError.new(code: "render_failed", error_class: "UnsupportedChannel")
  end

  begin
    case @channel_key
    when EMAIL_CHANNEL
      render_email(address)
    when SMS_CHANNEL
      render_sms(address)
    when PUSHOVER_CHANNEL
      render_pushover(address)
    else
      raise RenderError.new(code: "render_failed", error_class: "UnsupportedChannel")
    end
  rescue RenderError
    raise
  rescue StandardError => error
    raise RenderError.new(code: "render_failed", error_class: error.class.name)
  end
end