1

I've got the following feed from an external site which I've brought in to my rails3.2 app via httparty.

It would seem I'm having a retarted moment and cannot figure out how on earth to format / style it. Do I need to use nokogiri or is it simpler?

["radcheck", [{"attribute_name"=>"User-Password", "batch_user_id"=>nil, "id"=>22, "op"=>":=", "radcheck_serial"=>nil, "raduser_id"=>nil, "username"=>"simon", "value"=>"pass"}, {"attribute_name"=>"User-Password", "batch_user_id"=>nil, "id"=>23, "op"=>":=", "radcheck_serial"=>nil, "raduser_id"=>nil, "username"=>"silver", "value"=>"rain"}.....
5
  • What you're showing is a ruby data structure, no need for nokogiri. But what are you trying to accomplish, just render an html view for that data? How about a rails view or partial? Commented Feb 19, 2012 at 18:05
  • Think I'm experiencing writers block... Am just trying to format with plain old html.. Don't seem to be able to loop through the values as usual. Commented Feb 19, 2012 at 18:09
  • Btw. Am pretty sure that's not standard ror output considering it's pulling from xml feed and output is different. Commented Feb 19, 2012 at 19:21
  • Can you add more details / code on how you retrieve your feed object ? Commented Feb 20, 2012 at 8:28
  • According to HTTParty doc, your comment is not correct : HTTParty parse automatically XML into a standard ruby hash... Commented Feb 20, 2012 at 8:32

2 Answers 2

1

Given your sample data:

feed = ["radcheck", [
  {"attribute_name"=>"User-Password", "batch_user_id"=>nil, "id"=>22, "op"=>":=", "radcheck_serial"=>nil, "raduser_id"=>nil, "username"=>"simon", "value"=>"pass"},
  {"attribute_name"=>"User-Password", "batch_user_id"=>nil, "id"=>23, "op"=>":=", "radcheck_serial"=>nil, "raduser_id"=>nil, "username"=>"silver", "value"=>"rain"}
]]

if feed.is_a? Array && feed.size >= 2
  radcheck = feed.first  # do you need this first element?
  data = feed[1]
  # now you have an array in data, and can loop over each hash
  Array(data).each do |elem|
    puts elem["id"] # and so on
  end
end

This works in my console, so you should be able to adapt this for your templating mechanism.

Sign up to request clarification or add additional context in comments.

2 Comments

That makes more sense. However, in my console it gives me a TypeError: class or module required.
at which line does it give the error? And what type is the object it's throwing the error on?
1

Hum I think all you have to do is to loop over the 2nd element, which is an array of hash :

In controller :

@feed = ... # Retrieve your feed with httparty or other

In view :

<p>
First element: <%= @feed[0] #="radcheck", don't know the meaning of this %>
</p>
<p>
Attributes:
<ul>
<% @feed[1].each do |item| %>
  <li>
  Name: <%= item.attribute_name %><br />
  Batch User ID: <%= item.batch_user_id %><br />
  ID: <%= item.id %><br />
  OP: <%= item.op %><br />
  ... and so on ...
  </li>
<% end %>
</ul>
</p>

3 Comments

Thanks for response. That method gives me the following error: undefined method `each' for nil:NilClass
If @feed is exactly what you say (["radcheck, [{ ...},{...}..."), @feed[1] can't be nil and is an Array of Hash. This work in my console.
Got it working a slightly different way. Thanks for the help though - very helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.