I'm working on rendering the results of a JSON based API and am struggling with how to properly iterate through the results. The gist of my API call:
@invoice = ActiveSupport::JSON.decode(api_response.to_json)
The resulting hash array is as follows:
{
"amount_due"=>4900, "attempt_count"=>0, "attempted"=>true, "closed"=>true,
"currency"=>"usd", "date"=>1350514040, "discount"=>nil, "ending_balance"=>0, "livemode"=>false,
"next_payment_attempt"=>nil, "object"=>"invoice", "paid"=>true, "period_end"=>1350514040, "period_start"=>1350514040, "starting_balance"=>0,
"subtotal"=>4900, "total"=>4900,
"lines"=>{
"invoiceitems"=>[],
"prorations"=>[],
"subscriptions"=>[
{"quantity"=>1,
"period"=>{"end"=>1353192440, "start"=>1350514040},
"plan"=>{"id"=>"2", "interval"=>"month", "trial_period_days"=>nil, "currency"=>"usd", "amount"=>4900, "name"=>"Basic"},
"amount"=>4900}
]
}}
I'm trying to loop through and display all the "lines" in order to render and invoice. Each of the "lines" can have 0 or many "invoiceitems", "prorations" and "subscriptions".
I've gotten this far, but can't figure our how to deal w/ any of the nesting.
<% @invoice["lines"].each_with_index do |line, index| %>
# not sure what the syntax is here ?
<% end %>
I'm currently working in the view, but will move most of this to a helper once I get it sorted.
Thanks!