0

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!

1 Answer 1

1

Based on the example Hash you have attached, I suspect you're having difficulties because your trying to enumerate over the object in @invoice["lines"] like you would an Array. The problem with this is that the object is a Hash and so enumeration is handled a little bit differently.

Since the keys invoiceitems, subscriptions, and prorations are always returned and also based on the assumption that each of these categories probably will look different on the generated invoice since they will have different attributes, you should just have 3 separate loops over the 3 values in the Hash. I've coded up an example of how this would work below:

<% @invoice["lines"]["invoiceitems"].each_with_index do |item, index| %>
  # display logic of an invoice item
<% end %>

<% @invoice["lines"]["prorations"].each_with_index do |proration, index| %>
  # display logic of a proration
<% end %>

<table>
  <tr>
    <th>#</th>
    <th>Quantity</th>
    <th>Start Period</th>
    <th>Amount</th>
  </tr>
  <% @invoice["lines"]["subscriptions"].each_with_index do |subscription, index| %>
  <tr>
    # display logic of a subscription
    <td><%= index %></td>
    <td><%= subscription["quantity"] %></td>
    <td>
      <%= DateTime.strptime("#{subscription["period"]["start"]}",'%s').strftime("%m/%d/%Y") %>
    </td>
  </tr>
  <% end %>
</table>

While I didn't do all the fields in subscriptions, this should be enough of an example to keep going.

Sign up to request clarification or add additional context in comments.

3 Comments

As an aside - I noted your used of DateTime.striptime. I had been using Time.at(subscription["period"]["start"]).strftime('%m/%d/%Y') - is one better than the other?
I'm not aware of any difference, just two ways of doing the same thing :)
OK - thanks again for such a complete answer. I was really banging my head against the table there!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.