Search code examples
ruby-on-railsrubyarraysetsy

What's the best way to iterate through this structure in Rails?


I'm new to rails and having trouble figuring out how to iterate through this data. This is the output from a gem that integrates the etsy API. It looks like an array of sets of hashes, maybe?

output=[#<Etsy::Listing:0x5551ba8 @result={"listing_id"=>182661496, "title"=>"Edible Butterflies in Coral", "quantity"=>1, "price"=>"12.50", "ending_tsz"=>1405362199, "shipping_template_id"=>nil, "MainImage"=>{"url_75x75"=>"https://img1.etsystatic.com/036/0/8545731/il_75x75.576542775_ibud.jpg"}}, @token="token", @secret="secret">, 
#<Etsy::Listing:0x5551bc0 @result={"listing_id"=>182671909, "title"=>"Marshmallow Sampler Pack", "quantity"=>3, "price"=>"9.50", "ending_tsz"=>1405362468, "shipping_template_id"=>1680751676, "MainImage"=>{"url_75x75"=>"https://img1.etsystatic.com/021/0/8545731/il_75x75.576544537_n2zo.jpg"}}, @token="token", @secret="secret">, 
#<Etsy::Listing:0x5551bf0 @result={"listing_id"=>182663346, "title"=>"Gourmet popcorn and seasoning kit", "quantity"=>15, "price"=>"26.95", "ending_tsz"=>1405363087, "shipping_template_id"=>nil, "MainImage"=>{"url_75x75"=>"https://img0.etsystatic.com/026/0/8545731/il_75x75.576428850_r1mv.jpg"}}, @token="token", @secret">, 
#<Etsy::Listing:0x5551c08 @result={"listing_id"=>189414412, "title"=>"Sailor Tote Bag", "quantity"=>45, "price"=>"50.00", "ending_tsz"=>1410586221, "shipping_template_id"=>1024284528, "MainImage"=>{"url_75x75"=>"https://img0.etsystatic.com/039/0/8545731/il_75x75.576443100_slse.jpg"}}, @token="token", @secret="secret">]

Nothing I've tried seems to work. Treating it as an array works for some things, like: output.length correctly returns 4. But output[0] returns <Etsy::Listing:0x5483418> without any of the other data. All I actually need is what's contained in @result={}. Ideas? Thanks!


Solution

  • Looks like you missed to read this in doc:

    listings = output.map do |listing| 
      { 
        "title"    => listing.title, 
        "quantity" => listing.quantity, 
        "price"    => listing.price
      }
    end