Class: CommandTower::Messaging::Execution::Adapters::Pushover::Adapter

Inherits:
Object
  • Object
show all
Defined in:
app/services/command_tower/messaging/execution/adapters/pushover/adapter.rb

Overview

Execution adapter: decrypts credentials for the readiness-resolved endpoint and sends via Messaging::Pushover::Transport.

Constant Summary collapse

CREDENTIAL_REJECTION_CODES =
%i[
  invalid_user
  invalid_token
  invalid_credentials
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(configuration: nil, transport: nil, secret_reader: nil) ⇒ Adapter

Returns a new instance of Adapter.



17
18
19
20
21
# File 'app/services/command_tower/messaging/execution/adapters/pushover/adapter.rb', line 17

def initialize(configuration: nil, transport: nil, secret_reader: nil)
  @configuration = configuration || Configuration.new
  @transport = transport || Messaging::Pushover::Transport
  @secret_reader = secret_reader || Endpoints::SecretReader
end

Instance Method Details

#call(request:) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/services/command_tower/messaging/execution/adapters/pushover/adapter.rb', line 23

def call(request:)
  raise ArgumentError, "request must be an AdapterRequest" unless request.is_a?(AdapterRequest)

  rendered = request.rendered
  unless rendered.is_a?(Rendering::RenderedPushoverPayload)
    return AdapterResult.build(outcome: :terminal_failure, error_code: "render_failed")
  end

  unless @configuration.pushover_configured?
    return AdapterResult.build(outcome: :terminal_failure, error_code: "adapter_unconfigured")
  end

  endpoint_id = Integer(rendered.recipient_address, exception: false)
  if endpoint_id.nil? || endpoint_id <= 0
    return AdapterResult.build(outcome: :terminal_failure, error_code: "invalid_recipient")
  end

  delivery = Messaging::ChannelDelivery.includes(:communication).find_by(id: request.channel_delivery_id)
  owner_user_id = delivery&.communication&.user_id
  if owner_user_id.nil?
    return AdapterResult.build(outcome: :terminal_failure, error_code: "recipient_missing")
  end

  credentials = read_credentials(owner_user_id:, endpoint_id:)
  return credentials if credentials.is_a?(AdapterResult)

  result = @transport.send_message!(
    user_key: credentials.fetch(:user_key),
    application_token: credentials.fetch(:application_token),
    title: rendered.title,
    message: rendered.message,
  )
  map_result(result, owner_user_id:, endpoint_id:)
rescue StandardError
  AdapterResult.build(outcome: :retryable_failure, error_code: "pushover_transient")
end