ruby on rails - uninitialized Twilio::REST::LookupsClient -
i'm using ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-linux]
rails: rails 5.1.3
i have action in check phone number if valid or not , action send message. message action working error when try check number. action
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
the error uninitialised twilio::rest::lookupsclient
how can solve it?
twilio developer evangelist here.
it looks have installed latest version of twilio gem. in version there no longer twilio::rest::lookupsclient
rest clients part of twilio::rest::client
object. can make request specifically, rather having call on property of phone number object create.
check out documentation , examples of making calls lookup api ruby.
for right now, code should this:
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
note, replaced rescue exception
because it's bad idea. instead, can rescue twilio::rest::resterror
.
for more on using lookups api validate phone numbers in ruby, check out blog post on using active model validations , twilio lookups
Comments
Post a Comment