class Flickollage::Collage

Constants

RETRY_LIMIT

Attributes

dictionary[R]
images[R]
options[R]
words[R]

Public Class Methods

new(words, options) click to toggle source
# File lib/flickollage/collage.rb, line 13
def initialize(words, options)
  @words = words
  @options = options

  init_dictionary
  load_images
  download_images
  crop_images(options[:width], options[:height])
end

Public Instance Methods

generate_collage(path = options[:output]) click to toggle source
# File lib/flickollage/collage.rb, line 23
def generate_collage(path = options[:output])
  MiniMagick::Tool::Montage.new do |montage|
    images.each { |image| montage << image.image.path }

    montage.geometry "#{options[:width]}x#{options[:height]}+0+0"
    montage.tile "#{options[:cols]}x#{options[:rows]}"

    montage << path
  end
end

Private Instance Methods

crop_images(width, height) click to toggle source
# File lib/flickollage/collage.rb, line 47
def crop_images(width, height)
  images.each do |image|
    image.crop(width, height)
  end
end
download_images() click to toggle source
# File lib/flickollage/collage.rb, line 43
def download_images
  images.each(&:download)
end
init_dictionary() click to toggle source
# File lib/flickollage/collage.rb, line 36
def init_dictionary
  @dictionary = Dictionary.new(options[:dict])
  @dictionary.append(words)
rescue Flickollage::Dictionary::Error
  @dictionary = Dictionary.new(words)
end
load_image(attempt = 1) click to toggle source
# File lib/flickollage/collage.rb, line 60
def load_image(attempt = 1)
  word = dictionary.pop
  not_enought_words unless word
  Image.new(word)
rescue Flickollage::Image::Error
  too_many_attempts if attempt == RETRY_LIMIT
  load_image(attempt + 1)
end
load_images() click to toggle source
# File lib/flickollage/collage.rb, line 53
def load_images
  @images = []
  options[:number].times do
    @images << load_image
  end
end
not_enought_words() click to toggle source
# File lib/flickollage/collage.rb, line 69
def not_enought_words
  raise Flickollage::Error,
        'Not enought words. Please, specify more words or provide a bigger dictionary.'
end
too_many_attempts() click to toggle source
# File lib/flickollage/collage.rb, line 74
def too_many_attempts
  raise Flickollage::Error, 'Could not load images. Please, try later.'
end