class ImageCollage::Cli

Constants

IMAGE_COLLAGE_DEFAULT_PATH

Public Instance Methods

run(args = ARGV) click to toggle source
# File lib/imagecollage/cli.rb, line 5
def run(args = ARGV)
  flickr_api_key = fetch_flickr_api_key
  image_collage_path = fetch_image_collage_path

  options = {
    flickr_api_key: flickr_api_key,
    image_collage_path: image_collage_path
  }

  runner = Runner.new(args, options)

  return runner.run
rescue ImageCollage::Error => e
  $stderr.puts
  $stderr.puts "Error: #{e.message}"
  return 2
rescue StandardError => e
  $stderr.puts e.message
  $stderr.puts e.backtrace
  return 2
end

Private Instance Methods

ask(message) click to toggle source
# File lib/imagecollage/cli.rb, line 52
def ask(message)
  $stdout.puts
  $stdout.print "#{message} "
  result = $stdin.gets.chomp
  $stdout.puts

  result
end
fetch_flickr_api_key() click to toggle source
# File lib/imagecollage/cli.rb, line 29
def fetch_flickr_api_key
  if (api_key = ENV['FLICKR_API_KEY']).nil?
    $stdout.puts 'Please provide a Flickr API key and set it as a'
    $stdout.puts 'shell environment variable (FLICKR_API_KEY) or paste it here.'

    api_key = ask 'Flickr API KEY:'

    raise ImageCollage::Error, 'Invalid Flickr API key' if api_key.empty?
  end

  api_key
end
fetch_image_collage_path() click to toggle source
# File lib/imagecollage/cli.rb, line 42
def fetch_image_collage_path
  $stdout.puts 'Please provide a path for the generated collage image (default: current path).'
  path = ask "Image collage path (#{IMAGE_COLLAGE_DEFAULT_PATH}):"
  path = IMAGE_COLLAGE_DEFAULT_PATH if path.empty?

  raise ImageCollage::Error, 'Invalid path' unless File.directory?(File.dirname(path))

  path
end