未初始化 Twilio::REST::LookupsClient
uninitialized Twilio::REST::LookupsClient
我正在使用 ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-linux]
rails: Rails 5.1.3
我有检查 phone 号码是否有效的操作和发送消息的操作。
消息操作完美运行,但是当我尝试检查号码时出现错误。这是动作
def check_phone_number
account_sid = Rails.application.secrets.twilio_sid
auth_token = Rails.application.secrets.twilio_token
@lookup_client = Twilio::REST::LookupsClient.new(account_sid,
auth_token)
response = @lookup_client.phone_numbers.get("#
{params[:phone_number]}")
begin
response.phone_number
render json: {'response' => 'ok'}
rescue Exception => e
if e.code == 20404
render json: { 'error' => 'Invalid Number' }
else
render json: { 'error' => 'Invalid Number' }
end
end
end
错误是uninitialised Twilio::REST::LookupsClient
我该如何解决?
可能是第一个 :: 扔掉它。尝试:
@lookup_client = Twilio::REST::LookupsClient.new(account_sid,
auth_token)
而不是
@lookup_client = ::Twilio::Rest::Client.new account_sid, account_auth_token
示例:https://github.com/twilio/twilio-ruby/blob/master/examples/examples.rb
另外,参见 https://github.com/twilio/twilio-ruby/blob/master/lib/twilio-ruby/rest/client.rb - REST 看起来应该大写。
此处为 Twilio 开发人员布道师。
看来您已经安装了最新版本的 Twilio gem。在此版本中不再有 Twilio::REST::LookupsClient
所有 REST 客户端都是 Twilio::REST::Client
对象的一部分。您也可以专门提出请求,而不必调用您创建的 phone 数字对象的 属性。
查看 making calls to the Lookup API with Ruby 的文档和示例。
目前,您的代码应如下所示:
def check_phone_number
account_sid = Rails.application.secrets.twilio_sid
auth_token = Rails.application.secrets.twilio_token
@lookup_client = Twilio::REST::Client.new(account_sid, auth_token)
phone_number = @lookup_client.lookups.phone_numbers(params[:phone_number])
begin
response = phone_number.fetch
render json: {'response' => 'ok'}
rescue Twilio::REST::RestError => e
if e.code == 20404
render json: { 'error' => 'Invalid Number' }
else
render json: { 'error' => 'Invalid Number' }
end
end
end
注意,我还替换了你的 rescue Exception
因为 it's a bad idea。相反,您可以 rescue Twilio::REST::RestError
.
有关使用查找 API 来验证 Ruby 中的 phone 个数字的更多信息,请查看我在 using Active Model Validations and Twilio Lookups[=18= 上的博客 post ]
我正在使用 ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-linux]
rails: Rails 5.1.3
我有检查 phone 号码是否有效的操作和发送消息的操作。 消息操作完美运行,但是当我尝试检查号码时出现错误。这是动作
def check_phone_number
account_sid = Rails.application.secrets.twilio_sid
auth_token = Rails.application.secrets.twilio_token
@lookup_client = Twilio::REST::LookupsClient.new(account_sid,
auth_token)
response = @lookup_client.phone_numbers.get("#
{params[:phone_number]}")
begin
response.phone_number
render json: {'response' => 'ok'}
rescue Exception => e
if e.code == 20404
render json: { 'error' => 'Invalid Number' }
else
render json: { 'error' => 'Invalid Number' }
end
end
end
错误是uninitialised Twilio::REST::LookupsClient
我该如何解决?
可能是第一个 :: 扔掉它。尝试:
@lookup_client = Twilio::REST::LookupsClient.new(account_sid,
auth_token)
而不是
@lookup_client = ::Twilio::Rest::Client.new account_sid, account_auth_token
示例:https://github.com/twilio/twilio-ruby/blob/master/examples/examples.rb
另外,参见 https://github.com/twilio/twilio-ruby/blob/master/lib/twilio-ruby/rest/client.rb - REST 看起来应该大写。
此处为 Twilio 开发人员布道师。
看来您已经安装了最新版本的 Twilio gem。在此版本中不再有 Twilio::REST::LookupsClient
所有 REST 客户端都是 Twilio::REST::Client
对象的一部分。您也可以专门提出请求,而不必调用您创建的 phone 数字对象的 属性。
查看 making calls to the Lookup API with Ruby 的文档和示例。
目前,您的代码应如下所示:
def check_phone_number
account_sid = Rails.application.secrets.twilio_sid
auth_token = Rails.application.secrets.twilio_token
@lookup_client = Twilio::REST::Client.new(account_sid, auth_token)
phone_number = @lookup_client.lookups.phone_numbers(params[:phone_number])
begin
response = phone_number.fetch
render json: {'response' => 'ok'}
rescue Twilio::REST::RestError => e
if e.code == 20404
render json: { 'error' => 'Invalid Number' }
else
render json: { 'error' => 'Invalid Number' }
end
end
end
注意,我还替换了你的 rescue Exception
因为 it's a bad idea。相反,您可以 rescue Twilio::REST::RestError
.
有关使用查找 API 来验证 Ruby 中的 phone 个数字的更多信息,请查看我在 using Active Model Validations and Twilio Lookups[=18= 上的博客 post ]