How do you get data from a hash of arrays?

Ok So I am using the calorieking api and it crack as my xml parser (it
comes with httparty which is my consumer gem). Now I need to be able to
list the food names. How can i get data from a hash that contains
arrays? Here is what the hash looks like when pp’ed.

#<HTTParty::Response:0x10368f160
@parsed_response={“searchresults”=>{“category”=>[{“name”=>“Average All
Brands”,
“foods”=>{“food”=>[{“id”=>“f985dcc7-ac8f-4ad5-bbe1-85e71469e023”,
“display”=>“Breast, meat only, roasted”},
{“id”=>“584917be-3ff3-4165-81c3-46ddb69ac35c”, “display”=>“Chicken Egg,
whole, hard-boiled”}, {“id”=>“0a067a58-8c3b-4a15-95d0-8cd54fa69401”,
“display”=>“Chicken Egg, egg white, cooked, no added fat”},
{“id”=>“7b8a89bb-9f43-481a-9e59-750de9c4b98b”, “display”=>“Breast, meat
only, raw”}, {“id”=>“76de8abd-cd6e-40ab-ae44-2824c9a4576a”,
“display”=>“Chicken Egg, whole, raw, fresh”},
{“id”=>“dfaf0fe2-0faa-42ae-82a4-b67ab1d3cbf4”, “display”=>“Chicken Egg,
egg white, hard-boiled”}, {“id”=>“b9d2a52d-e554-4641-b554-d11c3e4442e4”,
“display”=>“Chicken Egg, egg white, raw, fresh”},
{“id”=>“3f57dc04-2d73-4cb0-b7b8-73352e7299c7”, “display”=>“Wing, meat
only, fried, edible portion”},
{“id”=>“d981c191-2e80-4991-af90-b674f18daf35”, “display”=>“Thigh, meat
only, roasted”}, {“id”=>“42e5699f-79cd-436c-965b-90850b26a278”,
“display”=>“Breast Quarters, rotisserie, no skin or wing (Boston
Market)”}, {“id”=>“e04eeaad-801d-454d-b693-6ab306089e50”,
“display”=>“Breast, meat and skin, roasted”},
{“id”=>“06d4735c-5075-4d8a-bdd9-689471c9bbe6”, “display”=>“Leg, meat
only, roasted”}, {“id”=>“0915d043-9c44-470d-b1c1-9e9ac934628a”,
“display”=>“Chicken Egg, egg yolk, raw, fresh”},
{“id”=>“68556c9e-462a-4af1-89cf-04b79a04bdf1”, “display”=>“Wing w. meat
and skin, roasted”}, {“id”=>“64b4fc73-ec8e-411d-8659-0a990d80a04f”,
“display”=>“Breast, meat only, stewed”},
{“id”=>“b35f0582-d1e9-4dff-bf64-1495a1f92f51”, “display”=>“Breast, meat
& skin, fried, batter”}]}}, {“name”=>“McDonald’s”,
“foods”=>{“food”=>[{“id”=>“27dce0ec-8af9-46b0-ad40-ec59a501cdd1”,
“display”=>“Chicken McNuggets”},
{“id”=>“f1045302-9bb0-4018-9c1c-08f4a292c913”, “display”=>“Caesar w.
Grilled Chicken, no dressing”},
{“id”=>“4f6a1e64-3dcc-46b9-b981-933d56255fdd”, “display”=>“Snack Wraps,
Ranch w. Crispy Chicken”},
{“id”=>“67048351-9958-4f4c-b8e7-fb2663549a99”, “display”=>“Asian w.
Grilled Chicken, no dressing”},
{“id”=>“9a5f059e-6608-42c6-b809-02fb35c8949a”, “display”=>“Sandwiches,
Premium Grilled, Chicken Classic”},
{“id”=>“3c020ca7-dd6a-438d-be02-c4c4c78b07f6”, “display”=>“Snack Wraps,
Honey Mustard w.Grilled Chicken”},
{“id”=>“7d59f606-8f68-4ff6-855a-076c32d4d7ec”, “display”=>“Bacon Ranch
w. Grilled Chicken, no dressing”},
{“id”=>“6167852c-5cff-4342-8df9-696909773b6c”, “display”=>“Premium
Chicken Breast Filet, Grilled”},
{“id”=>“e8e84c38-1e97-4d45-be1f-ad4d600b8734”, “display”=>“Southwest w.
Grilled Chicken, no dressing”},
{“id”=>“02ed0335-2aa7-4e38-b489-44eb3cff08af”, “display”=>“Snack Wraps,
Ranch w. Grilled Chicken”},
{“id”=>“10eaa1d4-2ab3-49d2-97d1-a2c772424690”, “display”=>“Chicken
Selects, Premium Breast Strips”}]}}, {“name”=>“Burger King”,
“foods”=>{“food”=>[{“id”=>“de413f0f-dd04-4825-b92d-465439c6cbcc”,
“display”=>“Garden w. TenderGrill Chicken, no dressing or croutons”},
{“id”=>“5bd48435-bceb-473c-a97d-aea374774db4”, “display”=>“Sandwiches,
TenderGrill Chicken w. Honey Mustard Sauce”},
{“id”=>“24432f66-bd83-41f6-bb3c-59cd0076c747”, “display”=>“Sandwiches,
Original Chicken”}]}}]}}, @response=#<Net::HTTPOK 200 OK readbody=true>,
@headers={“expires”=>[“0”], “last-modified”=>[“Thu, 26 Aug 2010 11:58:33
GMT”], “connection”=>[“close”], “content-type”=>[“application/xml”],
“date”=>[“Thu, 26 Aug 2010 11:58:33 GMT”], “server”=>[“Apache”],
“cache-control”=>[“pre-check=0, post-check=0, max-age=0”],
“pragma”=>[“no-cache”], “transfer-encoding”=>[“chunked”]}>

On Aug 26, 1:32 pm, Zack N. [email protected] wrote:

Ok So I am using the calorieking api and it crack as my xml parser (it
comes with httparty which is my consumer gem). Now I need to be able to
list the food names. How can i get data from a hash that contains
arrays? Here is what the hash looks like when pp’ed.

I’m not entirely sure what you’re asking. Iterate over the hash and
the arrays it contains with something like each ?

Fred

Frederick C. wrote:

On Aug 26, 1:32�pm, Zack N. [email protected] wrote:

Ok So I am using the calorieking api and it crack as my xml parser (it
comes with httparty which is my consumer gem). Now I need to be able to
list the food names. How can i get data from a hash that contains
arrays? Here is what the hash looks like when pp’ed.

I’m not entirely sure what you’re asking. Iterate over the hash and
the arrays it contains with something like each ?

Fred

Well Ideally Id like to know how to get all the displays. Now Whether i
need to iterate through it or i can just use a command to get them, im
not sure. In a more broad sense i need to know how to get any element,
so perhaps a display or an id.

Many thanks,

jakx12

Just an example of how you’d output it in a view, I’m sure you can
adapt it from there to extract whatever values you want:

<% @parsed_response[0][‘searchresults’][‘category’].each do |cat| %>

<%=h cat['name']%>

Foods

<% cat['foods']['food'].each do |food| %>

<%=h food['id']%> - <%=h food['display']%>

<% end %> <% end %>

That code was the for the view, hence the HTML tags. You’d obviously
just use the iterating code without the output in the controller.

CU wrote:

That code was the for the view, hence the HTML tags. You’d obviously
just use the iterating code without the output in the controller.

Of course I did just that! But i still got the error…

CU wrote:

Just an example of how you’d output it in a view, I’m sure you can
adapt it from there to extract whatever values you want:

<% @parsed_response[0][‘searchresults’][‘category’].each do |cat| %>

<%=h cat['name']%>

Foods

<% cat['foods']['food'].each do |food| %>

<%=h food['id']%> - <%=h food['display']%>

<% end %> <% end %>

thanks, but when i do this in the controller i get this error:

You have a nil object when you didn’t expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]

But if i pp @parsed_response its not nill!

thanks

On Aug 26, 2:30 pm, Zack N. [email protected] wrote:

The error occurred while evaluating nil.[]

But if i pp @parsed_response its not nill!

That’s not necessarily the thing that is nil - for example if for a
certain category cat[‘foods’] didn’t exist then you’d get that error

Fred

Post the code you’re using in the controller?

Zack N. wrote:

Of course I did just that! But i still got the error…

Well then you are assuming that the wrong thing is nil… perhaps it
isn’t @parsed_response that is nil. Try working through the issue with
irb and seeing what your results are and work through how you’d navigate
the results.

All I can say is that it works just fine (accessing the calorieking api
and displaying the results), having never looked at their API or used
HTTParty before this a.m.

Model:

class Foodq < Object
include HTTParty
basic_auth “84294d7ae4454189bf2a3ebc37a0e421”, “”

def trial_search
self.class.get(“http://foodsearch1.webservices.calorieking.com/rest/search/chicken”)
end
end

Controller:

class FoodqsController < ApplicationController
def foods
@foodq = Foodq.new
@response = @foodq.trial_search
end
end

View (foods.html.haml - yeah, I use haml, but it’s pretty easy to read,
and sooooo much nicer to code):

%table{:border => “1”, :cellspacing => “1”, :cellpadding => “1”}

  • @response[‘searchresults’][‘category’].each do |cat|
    %tr
    %td{:colspan => “2”}= h(cat[‘name’])
    • cat[‘foods’][‘food’].each do |fd|
      %tr
      %td= h(fd[‘id’])
      %td= h(fd[‘display’])

Easy peasy…

YOU SAVIOUR! I’ll try it out tonight. Thanks sooo much!