Ruby - Iterating over a Nested Hash

405 views
Skip to first unread message

Scott Bradford

unread,
Nov 13, 2014, 8:31:08 AM11/13/14
to rubyonra...@googlegroups.com
I am trying to figure out how to output a global variable to my view
page to EACH DO loop the contents of my xml file.


This is how my controller is configured:

** CONTROLLER CLASS **
require 'net/http'
require 'httparty'
require 'rexml/document'
require 'uri'
require 'remixr'
include REXML

#user inputs from form
def result
$zip_code = params[:zip_code]
$range = params[:range]

#Modifiable URL parameters
$base_url = 'http://api.remix.bestbuy.com'
$uri = URI.parse($base_url)
$stores_call = '/v1/stores'
$apiKey = '?apiKey='YOUR API KEY'
$area_para = "(area(#{$zip_code},#{$range}))"
$store_para = '&show=storeID,name'
$xml_url =
"#{$base_url}#{$stores_call}#{$area_para}#{$apiKey}#{$store_para}"

# Data call and parsing into xml hash array
http = Net::HTTP.new($uri.host, $uri.port)
request = Net::HTTP::Get.new($xml_url)
response = http.request(request)

$xml = response.body
$doc = Document.new $xml

** VIEW PAGE **
<%=
$doc.each do |item|
puts "{storeID}=#{item}"
end
%>

The $doc object is a nested hash with store locations that each have
other characteristics. Currently the parameters only pull the StoreID
and name. So the loop will need to pull each StoreID, then name, then
repeat loop. I am going to be building the loop around an HTML table,
but that would be extra credit if someone wants to take it on.

Thank you so much!

--
Posted via http://www.ruby-forum.com/.

Vivek Sampara

unread,
Nov 13, 2014, 8:58:13 AM11/13/14
to rubyonra...@googlegroups.com
If you want to iterate over each nested hash - it totally depends on the depth of the hash and the kind of view you're trying to build using it. If you dont have knowledge about the other keys that come in the hash, use this pattern. 

h = { "a" => 100, "b" => 200 }
h.each {|key, value| puts "#{key} is #{value}" }

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/d56fc1207bc5c99a10e284d859307c9f%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

Scott Bradford

unread,
Nov 13, 2014, 9:43:25 AM11/13/14
to rubyonra...@googlegroups.com
I am trying to build it nearly like the PHP example above.

I'll build the header row for a table and then run the loop to fill the
cells and then put in a closer for the table.

Here is a sample of the data:

<store>
<storeId>2970</storeId>
<name>Best Buy Mobile - Brunswick Square Mall </name>
</store>
<store>
<storeId>598</storeId>
<name>East Brunswick</name>
</store>
<store>
<storeId>2980</storeId>
<name>Best Buy Mobile - Menlo Park Mall </name>
</store>

I will be adding in more portions of the hash to the end product, but
want to understand how to iterate two items first. Does this help?

Vivek Sampara

unread,
Nov 13, 2014, 10:46:56 AM11/13/14
to rubyonra...@googlegroups.com
You will have to use nokogiri gem to parse it and iterate. 

More details here. 


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.

Scott Bradford

unread,
Nov 14, 2014, 5:33:20 AM11/14/14
to rubyonra...@googlegroups.com
I couldnt' figure it out. Everytime I ran it through the Nokogiri gem,
the headers were still there. It was like nothing changed to the data.

Colin Law

unread,
Nov 14, 2014, 6:22:12 AM11/14/14
to rubyonra...@googlegroups.com
On 14 November 2014 10:32, Scott Bradford <li...@ruby-forum.com> wrote:
> I couldnt' figure it out. Everytime I ran it through the Nokogiri gem,
> the headers were still there. It was like nothing changed to the data.

I don't understand, I thought you wanted to parse the xml document and
extract data for display, why would you expect it to change the data?

See the tutorials at http://www.nokogiri.org/ for how to do that.

Colin
Reply all
Reply to author
Forward
0 new messages