ruby - how can I generate a json return for all the objects in a twilio call -



ruby - how can I generate a json return for all the objects in a twilio call -

this similar question asked meaningfully different. using ruby, sinatra , twilio want phone call log records json object can print ona n html page. code is:

get '/call_logs' client = twilio::rest::client.new account_sid, auth_token calls = client.account.calls.list calls.each |call| @sid = call.sid puts @sid @from = call.from puts @from @to = call.to puts @to @price = call.price.to_s puts @price @duration = call.duration puts @duration end halt 200, {sid:@sid,from:@from,to:@to,price:@price,duration:@duration}.to_json end

this produces 1 homecoming is:

{ "sid":"cadd102cb19729424b1acdbe7b93a03400", "from":"+441903684027", "to":"+818088931617", "price":"-0.16000", "duration":"16" }

there are, in fact, multiple records getting one. if place

{ sid:@sid, from:@from, to:@to, price:@price, duration:@duration }.to_json

within "calls.each" loop error stuck. in terminal multiple records appear 1 json. so, question how can improve code records returned json?

i have tried , without "halt 200".

many help ...

your instance variables overwritten in each loop iteration. need keep/store each iteration in array , in end convert array json. please seek below mentioned code. work fine.

get '/call_logs' client = twilio::rest::client.new account_sid, auth_token calls = client.account.calls.list @result = [] calls.each |call| sc = {} sc[:sid] = call.sid puts sc[:sid] sc[:from] = call.from puts sc[:from] sc[:to] = call.to puts sc[:to] sc[:price] = call.price.to_s puts sc[:price] sc[:duration] = call.duration puts sc[:duration] @result << sc end halt 200, @result.to_json end

ruby json sinatra twilio

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -