2014-10-19 115 views
0

我從flickr檢索個人的照片集(專輯)列表。我如何從這個散列數組中提取ID,然後使用flickr.photosets.getPhotos來列出每個相冊中的圖像?我仍然在學習Ruby,並且對此感到非常困惑。我很感激任何正確的方向。Ruby Flickr API和Photosets

當我嘗試將set.each do .. anything添加到@photosets塊 - 我得到一個沒有方法錯誤'each'。

photos_controller.rb

require 'flickraw' 

class PhotosController < ApplicationController 

    def index 
    FlickRaw.api_key = ENV['FlickRaw_api_key'] 
    FlickRaw.shared_secret = ENV['FlickRaw_shared_secret'] 

    @photosets = flickr.photosets.getList(user_id: '[email protected]').each do |set| 
     set 
    end 
    end 
end 

照片/ index.html.erb

<h1>Listing photosets</h1> 

<%= @photosets %> 

電流輸出

清單photosest

{ 
    "id" =>"72157633435549025", 
    "primary" =>"8720558709", 
    "secret" =>"d6f79d3d3c", 
    "server" =>"7395", 
    "farm" =>8, 
    "photos" =>"8", 
    "videos" =>0, 
    "title" =>"Poetry Project", 
    "description" =>"", 
    "needs_interstitial" =>0, 
    "visibility_can_see_set" =>1, 
    "count_views" =>"4", 
    "count_comments" =>"0", 
    "can_comment" =>0, 
    "date_create" =>"1368044794", 
    "date_update" =>"1368044886" 
}, 
{ 
    "id" =>"72157633308505122", 
    "primary" =>"8674031898", 
    "secret" =>"8cd5722def", 
    "server" =>"8543", 
    "farm" =>9, 
    "photos" =>"22", 
    "videos" =>0, 
    "title" =>"Photo Midterm", 
    "description" =>"", 
    "needs_interstitial" =>0, 
    "visibility_can_see_set" =>1, 
    "count_views" =>"2", 
    "count_comments" =>"0", 
    "can_comment" =>0, 
    "date_create" =>"1366676884", 
    "date_update" =>"1366943480" 
} 

回答

0

我想通了 - 這是我使用的代碼。這使您可以獲取有關個人相冊(又稱'照片')的信息,然後獲取每張照片集中的照片。

控制器 需要 'flickraw'

class PhotosController < ApplicationController 
    def index 
    FlickRaw.api_key = ENV['FlickRaw_api_key'] 
    FlickRaw.shared_secret = ENV['FlickRaw_shared_secret'] 

    @photosets = flickr.photosets.getList(user_id: '[email protected]').map do |set| 
     flickr.photosets.getPhotos(photoset_id: set['id']).photo.map do |photo| 
     FlickRaw.url_b(photo) 
     end 
    end 
    end 
end 

查看

<% @photosets[0].each do |image| %> 
<%= image_tag image %> 
<% end %> 
1

你快到了。如果您的目的是將的photosets的輸出數據和各自的圖片在你index.html.erb,你可以嘗試這樣的:

@photosets = flickr.photosets.getList(user_id: '[email protected]').each do |set| 
    set["photos"] = flickr.photosets.getPhotos(photoset_id: set["id"]) 
end 

這將輸出類似:

{ 
    "id" =>"72157633435549025", 
    "primary" =>"8720558709", 
    "secret" =>"d6f79d3d3c", 
    "server" =>"7395", 
    "farm" =>8, 
    "photos" =>"8", 
    "videos" =>0, 
    "title" =>"Poetry Project", 
    "description" =>"", 
    "needs_interstitial" =>0, 
    "visibility_can_see_set" =>1, 
    "count_views" =>"4", 
    "count_comments" =>"0", 
    "can_comment" =>0, 
    "date_create" =>"1368044794", 
    "date_update" =>"1368044886" 
    "photos" => [{"id" => "2484", 
       "secret"=>"123456", 
       "server"=>"1", 
       "title"=>"my photo", 
       "isprimary"=>"0"}, 
       {"id" => "2483", 
       "secret" => "123456", 
       "server" => "1", 
       "title" => "flickr rocks", 
       "isprimary" => "1"}] 
}, 
{ # etc... } 

我不確定你的意思通過sets.each do .. anything不起作用,因爲我的代碼中沒有看到sets

+0

這使得這麼多的意義,我......但我發現這個錯誤未定義的方法'[] ='for# latazzajones 2014-10-20 01:14:53

+0

任何想法的代碼部分觸發它? 'getList'或'getPhoto'期間? – 2014-10-20 01:17:55

+0

我在set [「photos」]後註釋掉了所有內容,並且沒有錯誤。 – latazzajones 2014-10-20 01:20:47