class AnySMS::Backend::AWS

AnySMS backend class to send sms using amazon web services

Public Class Methods

new(access_key:, secret_access_key:, region: "us-east-1", default_sender_id: nil) click to toggle source

To use class, you need access key from AWS. Go to README for instructions on how to obtain them.

@param access_key [String] AWS access key @param secret_access_key [String] AWS secret access key @param region [String] AWS region. Full list: goo.gl/Ys5XMi @param default_sender_id [String] Default SenderID, !CHANGES account settings!

# File lib/any_sms/backend/aws.rb, line 14
def initialize(access_key:, secret_access_key:, region: "us-east-1",
               default_sender_id: nil)
  @access_key = access_key
  @secret_access_key = secret_access_key
  @region = region
  @default_sender_id = default_sender_id
end

Public Instance Methods

send_sms(phone, text, _args = {}) click to toggle source

Sends sms using amazon web services

@phone [String] Phone number in E.164 format @text [String] Sms text

# File lib/any_sms/backend/aws.rb, line 26
def send_sms(phone, text, _args = {})
  resp = sns_client.publish(phone_number: phone, message: text)

  if resp.error.nil? && resp.message_id
    respond_with_status :success
  else
    respond_with_status :sending_failure, meta: { error: resp.error }
  end
rescue StandardError => e
  respond_with_status :runtime_error, meta: { error: e }
end

Protected Instance Methods

sns_client() click to toggle source
# File lib/any_sms/backend/aws.rb, line 40
def sns_client
  @client ||= begin
    client = Aws::SNS::Client.new(sns_options)

    unless @default_sender_id.nil?
      client.set_sms_attributes(attributes: { "DefaultSenderID" => @default_sender_id })
    end

    client
  end
end
sns_options() click to toggle source
# File lib/any_sms/backend/aws.rb, line 52
def sns_options
  {
    access_key_id: @access_key,
    secret_access_key: @secret_access_key,
    region: @region
  }
end