cman77
cman77

Reputation: 1783

rendering JSON based API response rails

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!

Upvotes: 0

Views: 285

Answers (1)

adimitri
adimitri

Reputation: 1296

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.

Upvotes: 1

Related Questions