class ImageCollage::ImageUrlRequester

Public Class Methods

new(keywords, flickr_api, options = {}) click to toggle source
# File lib/imagecollage/image_url_requester.rb, line 3
def initialize(keywords, flickr_api, options = {})
  @keywords = keywords || []
  @flickr_api = flickr_api
  @options = options || {}

  @image_url_count = Integer(options[:image_url_count]) || 10
  @keywords_pool = fill_keywords_pool(@keywords)
end

Public Instance Methods

image_urls() click to toggle source
# File lib/imagecollage/image_url_requester.rb, line 12
def image_urls
  image_urls = []

  while image_urls.length < @image_url_count
    keyword = @keywords_pool.shift || random_keyword

    print "Fetching image (#{image_urls.length + 1}/#{@image_url_count}) for #{keyword}..."

    response = @flickr_api.photos_search(keyword: keyword)
    image_url = @flickr_api.parse_image_url(response.parsed_response)

    if image_url
      image_urls << image_url
      puts 'OK'
    else
      puts 'FAILED'
    end
  end

  image_urls
end

Private Instance Methods

fill_keywords_pool(keywords) click to toggle source
# File lib/imagecollage/image_url_requester.rb, line 40
def fill_keywords_pool(keywords)
  keywords_pool = Array.new(keywords)

  while keywords_pool.length < @image_url_count
    keywords_pool << random_keyword
  end

  keywords_pool
end
random_keyword() click to toggle source
# File lib/imagecollage/image_url_requester.rb, line 36
def random_keyword
  RandomWordApi.fetch
end