Sha256: e77651e53e1f40d0e50aaf3162b9b320d73f9cb51f6d3ff4fa0ec108b4ff9825

Contents?: true

Size: 1.59 KB

Versions: 9

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true
# SMS notification service for Messages and Notifications sent to Participants.
class MessageSmsNotification
  attr_reader :body, :phone_number

  BLACKLIST_MESSAGE = "The message From/To pair violates a blacklist rule."

  def initialize(body:, phone_number:)
    @body = body
    @phone_number = phone_number
  end

  def deliver
    if environment.staging? || environment.production?
      sms_client.account.messages.create message
    else
      message
    end
  rescue Twilio::REST::RequestError => e
    if e.message == BLACKLIST_MESSAGE
      record_delivery_error!(e.message)
    else
      capture_message(e.message)
    end
  end

  private

  def capture_message(message)
    Raven
      .capture_message message, extra: {
        phone_number: phone_number
      } if defined?(Raven)
  end

  def config
    Rails.application.config
  end

  def environment
    Rails.env
  end

  def status_callback_url
    config.try(:status_callback_url)
  end

  def from_telephone_number
    config.try(:twilio_account_telephone_number)
  end

  def message
    params = {
      to: "+1#{phone_number}",
      from: from_telephone_number,
      body: body
    }

    params[:status_callback] = status_callback_url if status_callback_url

    params
  end

  def record_delivery_error!(message)
    ThinkFeelDoEngine::SmsDeliveryError
      .create!(
        body: body,
        error_message: message,
        phone_number: phone_number
      )
  end

  def sms_client
    @sms_client ||= Twilio::REST::Client.new(
      config.twilio_account_sid,
      config.twilio_auth_token
    )
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
think_feel_do_engine-3.22.9 app/models/message_sms_notification.rb
think_feel_do_engine-3.22.8 app/models/message_sms_notification.rb
think_feel_do_engine-3.22.7 app/models/message_sms_notification.rb
think_feel_do_engine-3.22.6 app/models/message_sms_notification.rb
think_feel_do_engine-3.22.5 app/models/message_sms_notification.rb
think_feel_do_engine-3.22.4 app/models/message_sms_notification.rb
think_feel_do_engine-3.22.2 app/models/message_sms_notification.rb
think_feel_do_engine-3.22.1 app/models/message_sms_notification.rb
think_feel_do_engine-3.22.0 app/models/message_sms_notification.rb