ruby loop through data returned by API call

1.2k views Asked by At

I am working with ruby and sinatra. Using the Plivo api, I want to access the returned data, iterate through it and extract and print all the instances of some of the elements and print them out as HTML on my web page.

my ruby code is:

user_key = 'my_key'
 params = { 'account' => user_key }
 response = p.get_cdrs(params)
 obj = response.last.to_json

which gets the following returned in the Firebug console:

{
   "api_id":"c59f2008-5baf-11e3-bbb2-22000ac522d1",
   "meta":{
      "limit":20,
      "next":"/v1/Account/MAMTE4MTHJNJRKODBIMD/Call/?account=MAMTE4MTHJNJRKODBIMD&limit=20&offset=20",
      "offset":0,
      "previous":null,
      "total_count":114
   },
   "objects":[
      {
         "bill_duration":21,
         "billed_duration":60,
         "call_direction":"outbound",
         "call_duration":21,
         "call_uuid":"103a3bc6-5baa-11e3-a878-f30f0e86751e",
         "end_time":"2013-12-03 08:33:44+09:00",
         "from_number":"85258081345",
         "parent_call_uuid":"0313c23c-5baa-11e3-a673-f30f0e86751e",
         "resource_uri":"/v1/Account/MAMTE4MTHJNJRKODBIMD/Call/103a3bc6-5baa-11e3-a878-f30f0e86751e/",
         "to_number":"14157638148",
         "total_amount":"0.01200",
         "total_rate":"0.01200"
      },
      {
         "bill_duration":21,
         "billed_duration":60,
         "call_direction":"inbound",
         "call_duration":21,
         "call_uuid":"0313c23c-5baa-11e3-a673-f30f0e86751e",
         "end_time":"2013-12-03 08:33:44+09:00",
         "from_number":"sip:[email protected]",
         "parent_call_uuid":null,
         "resource_uri":"/v1/Account/MAMTE4MTHJNJRKODBIMD/Call/0313c23c-5baa-11e3-a673-f30f0e86751e/",
         "to_number":"14157638148",
         "total_amount":"0.00300",
         "total_rate":"0.00300"
      },
      {
         "bill_duration":358,
         "billed_duration":360,
         "call_direction":"inbound",
         "call_duration":358,
         "call_uuid":"0b07abb2-5b40-11e3-8fb1-057cf5362a5f",
         "end_time":"2013-12-02 19:59:58+09:00",
         "from_number":"441903773807",
         "parent_call_uuid":null,
         "resource_uri":"/v1/Account/MAMTE4MTHJNJRKODBIMD/Call/0b07abb2-5b40-11e3-8fb1-057cf5362a5f/",
         "to_number":"441903337012",
         "total_amount":"0.04800",
         "total_rate":"0.00800"
      },
      {
         "bill_duration":344,
         "billed_duration":360,
         "call_direction":"outbound",
         "call_duration":344,
         "call_uuid":"116d89f4-5b40-11e3-8fe5-057cf5362a5f",
         "end_time":"2013-12-02 19:59:57+09:00",
         "from_number":null,
         "parent_call_uuid":"0b07abb2-5b40-11e3-8fb1-057cf5362a5f",
         "resource_uri":"/v1/Account/MAMTE4MTHJNJRKODBIMD/Call/116d89f4-5b40-11e3-8fe5-057cf5362a5f/",
         "to_number":"sip:[email protected]",
         "total_amount":"0.01800",
         "total_rate":"0.00300"
      },
      {
         "bill_duration":27,
         "billed_duration":60,
         "call_direction":"outbound",
         "call_duration":27,
         "call_uuid":"1b026a9a-5b3e-11e3-898e-1b5c95282229",
         "end_time":"2013-12-02 19:40:46+09:00",
         "from_number":"85258081345",
         "parent_call_uuid":"19f8572c-5b3e-11e3-8930-1b5c95282229",
         "resource_uri":"/v1/Account/MAMTE4MTHJNJRKODBIMD/Call/1b026a9a-5b3e-11e3-898e-1b5c95282229/",
         "to_number":"441903773807",
         "total_amount":"0.01900",
         "total_rate":"0.01900"
      },
      {
         "bill_duration":27,
         "billed_duration":60,
         "call_direction":"inbound",
         "call_duration":27,
         "call_uuid":"19f8572c-5b3e-11e3-8930-1b5c95282229",
         "end_time":"2013-12-02 19:40:46+09:00",
         "from_number":"sip:[email protected]",
         "parent_call_uuid":null,
         "resource_uri":"/v1/Account/MAMTE4MTHJNJRKODBIMD/Call/19f8572c-5b3e-11e3-8930-1b5c95282229/",
         "to_number":"441903773807",
         "total_amount":"0.00300",
         "total_rate":"0.00300"
      },
      {
         "bill_duration":175,
         "billed_duration":180,
         "call_direction":"inbound",
         "call_duration":175,
         "call_uuid":"e59e46f6-5b3a-11e3-8488-1b5c95282229",
         "end_time":"2013-12-02 19:20:16+09:00",
         "from_number":"sip:[email protected]",
         "parent_call_uuid":null,
         "resource_uri":"/v1/Account/MAMTE4MTHJNJRKODBIMD/Call/e59e46f6-5b3a-11e3-8488-1b5c95282229/",
         "to_number":"441903773807",
         "total_amount":"0.00900",
         "total_rate":"0.00300"
      },
      {
         "bill_duration":175,
         "billed_duration":180,
         "call_direction":"outbound",
         "call_duration":175,
         "call_uuid":"e698b528-5b3a-11e3-84e1-1b5c95282229",
         "end_time":"2013-12-02 19:20:16+09:00",
         "from_number":"85258081345",
         "parent_call_uuid":"e59e46f6-5b3a-11e3-8488-1b5c95282229",
         "resource_uri":"/v1/Account/MAMTE4MTHJNJRKODBIMD/Call/e698b528-5b3a-11e3-84e1-1b5c95282229/",
         "to_number":"441903773807",
         "total_amount":"0.05700",
         "total_rate":"0.01900"
      },
      {
         "bill_duration":728,
         "billed_duration":780,
         "call_direction":"outbound",
         "call_duration":728,
         "call_uuid":"b421ca72-5b34-11e3-82c5-1b5c95282229",
         "end_time":"2013-12-02 18:45:04+09:00",
         "from_number":"85258081345",
         "parent_call_uuid":"b3210c64-5b34-11e3-8250-1b5c95282229",
         "resource_uri":"/v1/Account/MAMTE4MTHJNJRKODBIMD/Call/b421ca72-5b34-11e3-82c5-1b5c95282229/",
         "to_number":"85265001927",
         "total_amount":"0.33800",
         "total_rate":"0.02600"
      },
      {
         "bill_duration":728,
         "billed_duration":780,
         "call_direction":"inbound",
         "call_duration":728,
         "call_uuid":"b3210c64-5b34-11e3-8250-1b5c95282229",
         "end_time":"2013-12-02 18:45:04+09:00",
         "from_number":"sip:[email protected]",
         "parent_call_uuid":null,
         "resource_uri":"/v1/Account/MAMTE4MTHJNJRKODBIMD/Call/b3210c64-5b34-11e3-8250-1b5c95282229/",
         "to_number":"85265001927",
         "total_amount":"0.03900",
         "total_rate":"0.00300"
      },
      {
         "bill_duration":800,
         "billed_duration":840,
         "call_direction":"outbound",
         "call_duration":800,
         "call_uuid":"49c4bc6a-5b30-11e3-aa2e-f30f0e86751e",
         "end_time":"2013-12-02 18:14:41+09:00",
         "from_number":"85258081345",
         "parent_call_uuid":"43465b3c-5b30-11e3-a842-f30f0e86751e",
         "resource_uri":"/v1/Account/MAMTE4MTHJNJRKODBIMD/Call/49c4bc6a-5b30-11e3-aa2e-f30f0e86751e/",
         "to_number":"818042010550",
         "total_amount":"2.01600",
         "total_rate":"0.14400"
      },
      {
         "bill_duration":800,
         "billed_duration":840,
         "call_direction":"inbound",
         "call_duration":800,
         "call_uuid":"43465b3c-5b30-11e3-a842-f30f0e86751e",
         "end_time":"2013-12-02 18:14:41+09:00",
         "from_number":"sip:[email protected]",
         "parent_call_uuid":null,
         "resource_uri":"/v1/Account/MAMTE4MTHJNJRKODBIMD/Call/43465b3c-5b30-11e3-a842-f30f0e86751e/",
         "to_number":"818042010550",
         "total_amount":"0.04200",
         "total_rate":"0.00300"
      },
      {
         "bill_duration":2,
         "billed_duration":60,
         "call_direction":"outbound",
         "call_duration":2,
         "call_uuid":"7f1db12e-5a7b-11e3-9c03-c7a22946c980",
         "end_time":"2013-12-01 20:27:08+09:00",
         "from_number":"185258081345",
         "parent_call_uuid":"7e6225d0-5a7b-11e3-9bd4-c7a22946c980",
         "resource_uri":"/v1/Account/MAMTE4MTHJNJRKODBIMD/Call/7f1db12e-5a7b-11e3-9c03-c7a22946c980/",
         "to_number":"sip:[email protected]",
         "total_amount":"0.00300",
         "total_rate":"0.00300"
      },
      {
         "bill_duration":2,
         "billed_duration":60,
         "call_direction":"inbound",
         "call_duration":2,
         "call_uuid":"7e6225d0-5a7b-11e3-9bd4-c7a22946c980",
         "end_time":"2013-12-01 20:27:08+09:00",
         "from_number":"185258081345",
         "parent_call_uuid":null,
         "resource_uri":"/v1/Account/MAMTE4MTHJNJRKODBIMD/Call/7e6225d0-5a7b-11e3-9bd4-c7a22946c980/",
         "to_number":"14154292111",
         "total_amount":"0.00850",
         "total_rate":"0.00850"
      },
      {
         "bill_duration":1628,
         "billed_duration":1680,
         "call_direction":"outbound",
         "call_duration":1628,
         "call_uuid":"108c1c14-5a76-11e3-a749-1b5c95282229",
         "end_time":"2013-12-01 20:15:25+09:00",
         "from_number":"85258081345",
         "parent_call_uuid":"0f78d9c0-5a76-11e3-a6af-1b5c95282229",
         "resource_uri":"/v1/Account/MAMTE4MTHJNJRKODBIMD/Call/108c1c14-5a76-11e3-a749-1b5c95282229/",
         "to_number":"441903773807",
         "total_amount":"0.53200",
         "total_rate":"0.01900"
      },
      {
         "bill_duration":1628,
         "billed_duration":1680,
         "call_direction":"inbound",
         "call_duration":1628,
         "call_uuid":"0f78d9c0-5a76-11e3-a6af-1b5c95282229",
         "end_time":"2013-12-01 20:15:25+09:00",
         "from_number":"sip:[email protected]",
         "parent_call_uuid":null,
         "resource_uri":"/v1/Account/MAMTE4MTHJNJRKODBIMD/Call/0f78d9c0-5a76-11e3-a6af-1b5c95282229/",
         "to_number":"441903773807",
         "total_amount":"0.08400",
         "total_rate":"0.00300"
      },
      {
         "bill_duration":57,
         "billed_duration":60,
         "call_direction":"outbound",
         "call_duration":57,
         "call_uuid":"ce7f25f6-5a6f-11e3-ae45-f30f0e86751e",
         "end_time":"2013-12-01 19:04:37+09:00",
         "from_number":"85258081345",
         "parent_call_uuid":"c938e38e-5a6f-11e3-aca3-f30f0e86751e",
         "resource_uri":"/v1/Account/MAMTE4MTHJNJRKODBIMD/Call/ce7f25f6-5a6f-11e3-ae45-f30f0e86751e/",
         "to_number":"441903773807",
         "total_amount":"0.01900",
         "total_rate":"0.01900"
      },
      {
         "bill_duration":57,
         "billed_duration":60,
         "call_direction":"inbound",
         "call_duration":57,
         "call_uuid":"c938e38e-5a6f-11e3-aca3-f30f0e86751e",
         "end_time":"2013-12-01 19:04:37+09:00",
         "from_number":"sip:[email protected]",
         "parent_call_uuid":null,
         "resource_uri":"/v1/Account/MAMTE4MTHJNJRKODBIMD/Call/c938e38e-5a6f-11e3-aca3-f30f0e86751e/",
         "to_number":"441903773807",
         "total_amount":"0.00300",
         "total_rate":"0.00300"
      },
      {
         "bill_duration":126,
         "billed_duration":180,
         "call_direction":"outbound",
         "call_duration":126,
         "call_uuid":"24ed88a4-5a59-11e3-a462-fdc08976d83f",
         "end_time":"2013-12-01 16:23:39+09:00",
         "from_number":"185258081345",
         "parent_call_uuid":"2445b890-5a59-11e3-a437-fdc08976d83f",
         "resource_uri":"/v1/Account/MAMTE4MTHJNJRKODBIMD/Call/24ed88a4-5a59-11e3-a462-fdc08976d83f/",
         "to_number":"818088931617",
         "total_amount":"0.43200",
         "total_rate":"0.14400"
      },
      {
         "bill_duration":126,
         "billed_duration":180,
         "call_direction":"inbound",
         "call_duration":126,
         "call_uuid":"2445b890-5a59-11e3-a437-fdc08976d83f",
         "end_time":"2013-12-01 16:23:39+09:00",
         "from_number":"185258081345",
         "parent_call_uuid":null,
         "resource_uri":"/v1/Account/MAMTE4MTHJNJRKODBIMD/Call/2445b890-5a59-11e3-a437-fdc08976d83f/",
         "to_number":"14154292111",
         "total_amount":"0.02550",
         "total_rate":"0.00850"
      }
   ]
}

sorry it is so long.

Now, I want to access certain parts of the data and print out as HTML, for example, all the bill_durations and all the call_directions. How can I do this?

When I try:

obj.each do |row|
   bill_duration = ob["objects"][0]["bill_duration"]
  halt 200, { :bill_duration => bill_duration }
  end

the heroku error is:

NoMethodError - undefined method `each' for #<String:0x007f82e6448b38>

What's wrong here? Thanks guys!

OK, here is what I have now:

response.last['objects'].each do |row| 
 bill_duration = row["bill_duration"] 
 halt 200,  { :bill_duration => bill_duration }.to_json 
 end

this does not produce an error but it only returns one record despite the loop. I need to get all the instances of bill_duration.

2

There are 2 answers

1
user1903663 On BEST ANSWER

with assistance from Peter Alfvin, the sanswer is:

results =  response.last['objects'].each do |row| 
  bill_duration = row["bill_duration"]
  { :bill_duration => bill_duration }
  end
 halt 200,  results.to_json 
 end

then can be accessed and printed out, with jQuery, as follows:

$.getJSON("/callrecords", function(data) {
        $.each( data, function( key, value ) {
        var txt = '<p>' +this["bill_duration"]+ '</p>';
      $(".results").append(txt);
     });//end each
    });//end JSON

Brilliant, I have been struggling with this, on and off, for ages. Thank you Peter!!

4
Peter Alfvin On

Actually, .to_json generates a string, not a hash, which is why you're getting the error your getting. It's informally documented at http://www.ruby-doc.org/stdlib-2.0/libdoc/json/rdoc/JSON.html#module-JSON-label-Generating+JSON

You can turn that string back into an object with JSON.parse or you can operate on response.last directly. In either case, though, note that the object is going be a hash, not an array, so you'll have to select the objects element from it before you can start iterating through the objects array, as in:

response.last['objects'].each

or

JSON.parse(obj)['objects'].each