-1

I would like the code to pull information from the cart api and store it in a hash with simple keys, then iterate over the hash to make new html elements as needed per item. Every item that I need to pull has the following: id, Name, and Price.

products.each do |product|
  @product_card = [
      {
          :id => "#{product['product_id']}",
          :name => "#{product['name']}",
          :price => "#{product['price']}"
      }
  ]

I want to store the information in a hash so I can iterate over the info on the fly like so:

<% @product_card.each do |Need help here, Need help here| %>
<html p><% :id %></html p>
<html p><% :name %></html p>
<html p><% :price %></html p>

I stay hopeful for any feedback you can offer.

sawa
  • 165,429
  • 45
  • 277
  • 381
  • 1
    This seems like what you are looking for: http://stackoverflow.com/questions/6423484/how-do-i-convert-hash-keys-to-method-names – Vincent Jan 19 '16 at 19:48
  • It's not very clear from this what the problem is - what does this produce and how does it differ from your expectations ? – Frederick Cheung Jan 19 '16 at 19:54
  • @FrederickCheung yeah, I was trying to express what I want but I can see how it can be unclear. what I would like is to store item information in a hash and call it via hash.name hash.price. – Justin Garr Jan 19 '16 at 19:57
  • Make it something the reader can actually do something with, for example what data does this api return? What are the contents of @product_card at the end? – Frederick Cheung Jan 19 '16 at 19:59
  • @FrederickCheung I just made my edit, after thinking about this over night. – Justin Garr Jan 20 '16 at 17:06
  • @FrederickCheung I am sure the question has been on stackOver but the link you gave is simply not what I wish to do. Thanks for the help any ways. – Justin Garr Jan 20 '16 at 17:08

2 Answers2

0

If you do something like

@product_card = {}
products.each do |product|
  @product_card[product['name']] = client.call('call' ,session_id ,"product.info#{product['id']}")
end

then at the end @product_card is hash where the keys are names and the values are whatever this api returns, apparently hashes, so when you do

<% @product_card.each do |key, data | %>

key will be the name and data the return value from the api (these are arbitrary names). Assuming the api does indeed return hashes you'd want

<%= data['price'] %>

and so on, inside the each loop

Frederick Cheung
  • 83,189
  • 8
  • 152
  • 174
  • when I do @product_card[product['name']] = product['product_id'] this does not return any value. <%= data['name'] %> I know the hash must have the data do to the loop making product cards. I just need that data. One more thing how would I add more "keys" to the hash above @product_card – Justin Garr Jan 20 '16 at 18:14
  • Then perhaps the data isn't as you expected it (symbol vs string keys is a common one) inspect the return value from the api and change accordingly. You add keys to a hash by doing hash[key] = value – Frederick Cheung Jan 20 '16 at 18:17
  • So I would need something like product_a[:name = 'foo', :id = 'foo1', :price=19.99] – Justin Garr Jan 20 '16 at 18:24
  • No, you'd need 3 separate assignments. You might also want try a basic ruby syntax guide as it would cover a lot of this sort of thing. – Frederick Cheung Jan 20 '16 at 18:27
  • you're 100% correct, the main issue here is my lack of understanding the syntax and the way it works. I can not help but dive in rails. Thanks again for the information! – Justin Garr Jan 20 '16 at 21:50
0

I'd suggest using a Struct. Adds a bit of overhead, but makes your intention crystal clear.

ProductCard = Struct.new(:id, :name, :price)

@product_cards = products.map do |product|
  ProductCard.new("#{product['product_id']}", "#{product['name']}", "#{product['price']}")
end

This gives you an array of ProductCards which you can iterate over and call .id, .name, .price.

Alternative using OpenStruct which does not require declaring a ProductCard constant:

@product_cards = products.map do |product|
  OpenStruct.new(id: "#{product['product_id']}", name: "#{product['name']}", price: "#{product['price']}")
end
fylooi
  • 3,840
  • 14
  • 24