2014-10-31 4 views
0

레일을 처음 사용하고이 데이터를 반복하는 방법을 파악하는 데 어려움이 있습니다. etsy API를 통합 한 보석의 출력입니다. 아마도 해시 세트의 배열처럼 보일 것입니다.Rails에서이 구조를 반복하는 가장 좋은 방법은 무엇입니까?

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">] 

아무 것도 시도하지 않았습니다. 배열로 취급하는 것은 다음과 같이 몇 가지 경우에 작동합니다. output.length가 을 올바르게 반환합니다. 그러나 출력 [0]은 < Etsy :: returns : 0x5483418 > 다른 데이터를 반환하지 않습니다. 실제로 필요한 것은 @ result = {}에 포함 된 것입니다. 아이디어? 감사!

+0

'output.first.result'을 사용해 보셨습니까? – Surya

답변

0

가 보이는 :

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

이것이 내가 필요한 것입니다 - 감사합니다! – wickedpixel

+0

@wickedpixel 미래에 다른 사람들을 도울 수 있도록 답변을 받아 들였는지 확인하십시오. – Surya

+0

빠른 후속 조치 - MainImage에서 키/값 쌍을 얻으려면 어떻게해야합니까? 나는'listing.MainImage.each_pair {| k, v | puts "# {k.inspect} : # {v}"}'하지만 결과는'NoMethodError undefined method 'MainImage'' – wickedpixel

0

당신이 쓴 :

그것은 해시 세트의 배열처럼 보이는, 어쩌면?

개체 배열입니다. 각 개체는 Etsy::Listing 인스턴스입니다. 각 목록과 뭔가를하려는 경우

> listing = output.first 
#=> <Etsy::Listing:0x5483418> 
> listing.result 
#=> {"listing_id"=>182661496, …} 

, 당신은이 작업을 수행 할 수 있습니다 : 당신은 레일 콘솔에서이 작업을 수행 할 수 있어야합니다 당신이 this in doc을 읽을 수 놓친처럼

> output.each {|listing| puts listing.result.title } 
Edible Butterflies in Coral 
Marshmallow Sampler Pack 
Gourmet popcorn and seasoning kit 
Sailor Tote Bag 
> 
+0

완벽한, 감사합니다! – wickedpixel

+0

@wickedpixel 답변이 도움이된다면 upvote하십시오. 당신은 유용하다고 생각되는 답변을 upvote 할 수 있고해야합니다. 감사! – awendt