Search code examples
ruby-on-railsrubyarrayshashflickr

Ruby Flickr API and Photosets


I'm retrieving a list of an individual's photosets(albums) from flickr. How can I extract the id's from this array of hashes and then use flickr.photosets.getPhotos to list the images in each album? I'm still learning Ruby and feeling very stuck on this. I'm grateful for any push in the right direction.

When I try adding set.each do .. anything to the @photosets block - I get a no method error for '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: '67921947@N02').each do |set|
       set
    end
  end
end

photos/index.html.erb

<h1>Listing photosets</h1>

<%= @photosets %>

The current output

Listing 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"
}

Solution

  • I figured it out - here's the code that I used. This allows you to pull the information about an individual's albums(aka 'photosets') and then for the photos in each photoset.

    Controller 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: '67921947@N02').map do |set|
          flickr.photosets.getPhotos(photoset_id: set['id']).photo.map do |photo|
            FlickRaw.url_b(photo)
          end
        end
      end
    end
    

    View

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