Class: Fccc::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/fccc/cli.rb

Instance Method Summary collapse

Instance Method Details

#collage(*keywords) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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