ohayon
ohayon

Reputation: 391

Catch Error from external API in Rails

I am building a simple web app that sends SMS messages to cell phones using Twilio. I want to ensure that the user has entered a full 10 digit phone number before it will allow a message to attempt to be sent.

When I test it with a less-than or greater-than 10 digit number, in heroku logs, I see Twilio::REST::RequestError (The 'To' number 1234567890 is not a valid phone number.).

I have tried to use a begin/rescue wrapper and am telling it to render text: "Try again with a valid number." and tried a variety of if statements to try to avoid the error.

I am pretty new to Ruby and Rails and Twilio, but I promise i have been through every guide I have found. Any help is greatly appreciated. Full code of my UserController below:

    require 'twilio-ruby'
    class UsersController < ApplicationController

    def new
      @user = User.new
    end

    def create
      @user = User.new(params[:user])

      account_sid = '...'
      auth_token = '...'

      if @user.save
         render text: "Wasn't that fun? Hit the back button in your browser to give it another go!"
    begin
       client = Twilio::REST::Client.new account_sid, auth_token
           client.account.sms.messages.create(
           from: '+16035093259',
           to: @user.phone,
           body: @user.message
           )
        rescue Twilio::REST::RequestError
           render text: "Try again with a valid number."        
        end
     else
       render :new
     end
  end

end

Upvotes: 1

Views: 1848

Answers (1)

DMKE
DMKE

Reputation: 4603

I'd extract the SMS sending logic into a separate model/controller and use a background job to process the submitting. The UserController should only handle, well, user creation/modification.

Scaffolding:

$ rails g model sms_job user:references message:text phone submitted_at:datetime
$ rake db:migrate

Model:

class SmsJob < AR::Base
  attr_accessible :user_id, :message, :phone

  belongs_to :user
  validates_presence_of :message, :phone, :user_id
  validates :phone,
      length: { min: 10 },
      format: { with: /\+?\d+/ }

  scope :unsubmitted, where(submitted_at: nil)

  TWILIO = {
    from_no:      '...',
    account_sid:  '...',
    auth_token:   '...'
  }

  # find a way to call this method separately from user request
  def self.process!
    unsubmitted.find_each do |job|
      begin
        client = Twilio::REST::Client.new TWILIO[:account_sid], TWILIO[:auth_token]
        client.account.sms.messages.create(
          from: TWILIO[:from_no],
          to:   job.phone,
          body: job.message
        )
        job.submitted_at = Time.zone.now
        job.save
      rescue Twilio::REST::RequestError
        # maybe set update a tries counter
        # or delete job record
        # or just ignore this error
      end
    end
  end
end

The controller then should just provide the information that the SMS is going to be send:

# don't forget the 'resources :sms_jobs' in your routes.rb
class SmsJobsController < ApplicationController
  # index, update, destroy only for only admin?

  def new
    @sms_job = SmsJobs.new
  end

  def create
    @sms_job = current_user.sms_jobs.build params[:sms_job]
    if @sms_job.save
      redirect_to root_url, notice: "Your message is being send!"
    else
      render :new
    end
  end
end

For the background processing, have a look at these excellent Railscasts :-) You probably need to workaround some concurrency problems if you have to process many messages and/or Twilio has a long response time (didn't use that service yet).

Upvotes: 3

Related Questions