sinatra - how to connect to Twilio API with ruby -
sorry basic question should easy answer!
using ruby , sinatra, trying connect, via api, details of calls. prescribed way twilio seems be:
@client = twilio::rest::client.new account_sid, auth_token # loop on calls , print out property each 1 @client.account.calls.list.each |call| puts call.sid puts call.from puts call.to which works fine , "puts" data in terminal. want print results on html page, changed line
@client.account.calls.list.each |call| to
@calls = @client.account.calls.list and removed last 3 lines of code block above, ie. "puts"
then, attempting print on index page included following:
<% @calls.each |call| %> <h4 style="color: #ff0000;"><%= params['msg'] %></h4> <ul> <li> <%= call.from %> </li> <li> <%= call.to %> </li> </ul> <% end %> the error message says:
undefined method `each' nil:nilclass so not connecting twilio seems though code same above connect , produce required results.
any ideas? gratefully received.
in sinatra, not use instance variables store connection objects , similar stuff. instead of @call, use set method enables user set such objects different variables.
the calls.list method, per code, defined in twilio::rest::listresource module. returns array , so, second part of code (in index.erb) correct.
the problem is, when start using instance variables storing connection object, gets reset in route , that's what's happening inside get .. end block.
change code to:
set :client, twilio::rest::client.new(account_sid, account_pass) # now, setting can accessed settings.client '/calls' @calls = settings.client.account.calls erb :index end # index.erb <% @calls.each |call| %> ... <% end %> this should work.
Comments
Post a Comment