I am trying to figure out how to bring a global variable ($doc) to my
view page and then run it through an EACH loop to PRINT or PUT the
contents of my xml file on the view page.
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.
I was able to get the table output working in PHP.
Here is the PHP version that I have yet to successfully migrate over to
Ruby on Rails:
foreach($xmlcont as $url)
{
echo “”;
echo "{$url->storeId} {$url->name} {$url->city}