class PhotoFlick::FlickrImageFetcher

This class responsible for interacting with Flickr API

Public Class Methods

new(keywords) click to toggle source
# File lib/photo_flick/flickr_image_fetcher.rb, line 10
def initialize(keywords)
                    @keywords = keywords
            end

Public Instance Methods

fetch_images!() click to toggle source

Get all images from Flickr

# File lib/photo_flick/flickr_image_fetcher.rb, line 15
def fetch_images!
                    validate_keywords
                    @keywords.each do |keyword|
                            image = nil
    loop do
      image = flickr.photos.search(text: keyword, 
                                   sort: 'interestingness-desc', 
                                   privacy_filter: 1, 
                                   per_page: 1).first
      break if image
      # Repeat with a random word if image not found
      keyword = dictionary.get_random_words(1).first
    end   
    download_image(image)
  end  
end

Private Instance Methods

dictionary() click to toggle source
# File lib/photo_flick/flickr_image_fetcher.rb, line 51
def dictionary
  @dictionary ||= PhotoFlick::Dictionary.new
end
download_image(image) click to toggle source

Downloads found images to 'tmp' folder

# File lib/photo_flick/flickr_image_fetcher.rb, line 41
def download_image(image)
                    url = FlickRaw.url(image)
                    Dir.mkdir('tmp') unless File.exists? 'tmp'
                    open(url) do |f|
                            File.open("tmp/#{image['id']}.jpg","wb") do |file|
      file.puts f.read
    end
  end
            end
validate_keywords() click to toggle source

Validate count of keywords

# File lib/photo_flick/flickr_image_fetcher.rb, line 35
def validate_keywords
                    return if (keyword_count = @keywords.length) == PhotoFlick::IMAGE_COUNT
  @keywords.concat dictionary.get_random_words(PhotoFlick::IMAGE_COUNT - keyword_count) 
            end