Class: ActivityNotification::OptionalTarget::AmazonSNS

Inherits:
Base
  • Object
show all
Defined in:
lib/activity_notification/optional_targets/amazon_sns.rb

Overview

Optional target implementation for mobile push notification or SMS using Amazon SNS.

Instance Method Summary collapse

Methods inherited from Base

#initialize, #to_optional_target_name

Constructor Details

This class inherits a constructor from ActivityNotification::OptionalTarget::Base

Instance Method Details

#initialize_target(options = {}) ⇒ Object

Initialize method to prepare Aws::SNS::Client

Parameters:

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

    Options for initializing

Options Hash (options):

  • :topic_arn (String, Proc, Symbol) — default: nil

    :topic_arn option for Aws::SNS::Client#publish, it resolved by target instance like email_allowed?

  • :target_arn (String, Proc, Symbol) — default: nil

    :target_arn option for Aws::SNS::Client#publish, it resolved by target instance like email_allowed?

  • :phone_number (String, Proc, Symbol) — default: nil

    :phone_number option for Aws::SNS::Client#publish, it resolved by target instance like email_allowed?

  • others (Hash)

    Other options to be set Aws::SNS::Client.new



17
18
19
20
21
22
# File 'lib/activity_notification/optional_targets/amazon_sns.rb', line 17

def initialize_target(options = {})
  @topic_arn    = options.delete(:topic_arn)
  @target_arn   = options.delete(:target_arn)
  @phone_number = options.delete(:phone_number)
  @sns_client = Aws::SNS::Client.new(options)
end

#notify(notification, options = {}) ⇒ Object

Publishes notification message to Amazon SNS

Parameters:

  • notification (Notification)

    Notification instance

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

    Options for publishing

Options Hash (options):

  • :topic_arn (String, Proc, Symbol) — default: nil

    :topic_arn option for Aws::SNS::Client#publish, it resolved by target instance like email_allowed?

  • :target_arn (String, Proc, Symbol) — default: nil

    :target_arn option for Aws::SNS::Client#publish, it resolved by target instance like email_allowed?

  • :phone_number (String, Proc, Symbol) — default: nil

    :phone_number option for Aws::SNS::Client#publish, it resolved by target instance like email_allowed?

  • :partial_root (String) — default: "activity_notification/optional_targets/#{target}/#{optional_target_name}", "activity_notification/optional_targets/#{target}/base", "activity_notification/optional_targets/default/#{optional_target_name}", "activity_notification/optional_targets/default/base"

    Partial template name

  • :partial (String) — default: self.key.tr('.', '/')

    Root path of partial template

  • :layout (String) — default: nil

    Layout template name

  • :layout_root (String) — default: 'layouts'

    Root path of layout template

  • :fallback (String, Symbol) — default: :default

    Fallback template to use when MissingTemplate is raised. Set :text to use i18n text as fallback.

  • others (Hash)

    Parameters to be set as locals



36
37
38
39
40
41
42
43
# File 'lib/activity_notification/optional_targets/amazon_sns.rb', line 36

def notify(notification, options = {})
  @sns_client.publish(
    topic_arn:    notification.target.resolve_value(options.delete(:topic_arn) || @topic_arn),
    target_arn:   notification.target.resolve_value(options.delete(:target_arn) || @target_arn),
    phone_number: notification.target.resolve_value(options.delete(:phone_number) || @phone_number),
    message: render_notification_message(notification, options)
  )
end