在Rails中从外部API捕获错误

时间:2012-10-12 12:43:27

标签: ruby-on-rails ruby heroku twilio

我正在构建一个简单的网络应用程序,使用Twilio向手机发送短信。我想确保用户输入了一个完整的10位数电话号码,然后才允许发送邮件。

当我使用小于或大于10位的数字对其进行测试时,在heroku logs中,我看到Twilio::REST::RequestError (The 'To' number 1234567890 is not a valid phone number.)

我尝试使用begin / rescue包装,然后告诉render text: "Try again with a valid number."并尝试各种if语句以避免错误。< / p>

我对Ruby和Rails以及Twilio都很陌生,但我保证我已经找到了我找到的所有指南。任何帮助是极大的赞赏。我的UserController的完整代码如下:

    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

1 个答案:

答案 0 :(得分:3)

我将SMS发送逻辑提取到单独的模型/控制器中,并使用后台作业来处理提交。 UserController应该只处理用户创建/修改。

脚手架:

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

型号:

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

然后,控制器应该只提供SMS将要发送的信息:

# 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

对于后台处理,看看这些优秀的Railscasts :-)如果你必须处理很多消息和/或Twilio有很长的响应时间,你可能需要解决一些并发问题(不是使用该服务)。