Class: PhotoFlick::FlickrImageFetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/photo_flick/flickr_image_fetcher.rb

Overview

This class responsible for interacting with Flickr API

Instance Method Summary collapse

Constructor Details

#initialize(keywords) ⇒ FlickrImageFetcher

Returns a new instance of FlickrImageFetcher.



10
11
12
# File 'lib/photo_flick/flickr_image_fetcher.rb', line 10

def initialize(keywords)
	@keywords = keywords
end

Instance Method Details

#fetch_images!Object

Get all images from Flickr



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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