class Fccc::CLI

Public Instance Methods

collage(*keywords) click to toggle source
# File lib/fccc/cli.rb, line 20
def collage(*keywords)
  collage = Utils::Collage.new(SMALL_WIDTH, SMALL_HEIGHT, COLLAGE_WIDTH, COLLAGE_HEIGHT, NUM_KEYWORDS)
  if !options[:input].nil?
    # Get keywords from a file
    keywords = File.readlines(File.join(Dir.pwd, "#{options[:input]}")).map do |line|
      line.strip if line.strip != ""
    end
  end
  # Clean up keywords
  keywords = keywords.map do |kw|
    kw.gsub(/[,]/ ,"").strip
  end
  # Force resize keywords array.
  # If it has more than NUM_KEYWORDS elements, just ignore extra elements.
  # If it has less than NUM_KEYWORDS elements, fill the rest with nil.
  keywords = Array.new(NUM_KEYWORDS).zip(keywords).map(&:last)
  # Download images
  collage.download_images_by_keywords(keywords)
  # Create and save collage
  output = options[:output]
  if output.nil?
    # Default filename with timestamp
    output_file = File.join(Dir.pwd, "collage-#{Time.now.utc.iso8601.gsub(/[:]/ ,"-")}.jpg")
  else
    # User-supplied filename
    output_file = File.join(Dir.pwd, "#{output}.jpg")
    while File.exist?(output_file) do
      # Increment suffix if the file exists
      output_file = Utils.increment_filename(output_file)
    end
  end
  collage.create_collage(output_file)
end