class CLI

Public Instance Methods

call() click to toggle source
# File lib/apod_cli/cli.rb, line 9
def call #All CLI logic should come from this command
  puts "\nHello.\n"
  @scraper = Scraper.new
  @printer = Printer.new
  @data = @scraper.data
  start
end
more_info(arr) click to toggle source
# File lib/apod_cli/cli.rb, line 160
def more_info(arr)
  if arr.length > 1
    puts "Would you like more information on one or more of these matches?"
  else
    puts "Would you like more information on this match?"
  end
  puts "[y/n]".colorize(:red)
  if valid_input(["y", "n"]) == "y"
    puts ""
    if arr.length > 1
      puts "Please enter the " + "results number".colorize(:red) + " of any link(s) you would like more\ninformation on, comma separated. Or, type " + "'all'".colorize(:red) + " for more information on all results."
      wanted = [(1..arr.length).to_a.map{|e| e.to_s}, "all"].flatten
      validated = false #Should maybe turn this into a valid_split method or something, but it's not necessary right now.
      while !validated
        searchterms = gets.chomp.strip.downcase.split(",")
        failed = false
        searchterms.each do |searchterm|
          searchterm.strip!
          if !wanted.include?(searchterm)
            puts "That's not a valid input! Please try again."
            failed = true
            break
          end
        end
        if !failed then validated = true end
      end
    else
      searchterms = ["all"]
    end
    if searchterms.include?("all")
      print_pages(arr)
    else
      selected = []
      searchterms.each do |searchterm|
        selected << arr[searchterm.to_i - 1]
      end
      print_pages(selected)
    end
  else
    puts ""
  end
end
print_pages(arr) click to toggle source
sample() click to toggle source
# File lib/apod_cli/cli.rb, line 44
def sample
  puts "\nPlease enter the number (" + "[1]".colorize(:red) + " - " + "[#{@data.length}]".colorize(:red) + ") of links you would\nlike to sample. Or, type " + "'all'".colorize(:red) + " for information on all results."
  wanted = [(1..@data.length).to_a.map{|e| e.to_s}, "all"].flatten
  num = valid_input(wanted)
  if num == "all"
    sample = @data
  else
    sample = @data.sample(num.to_i)
  end
  puts ""
  print_links(sample)
  puts ""
  more_info(sample)
end
start() click to toggle source
# File lib/apod_cli/cli.rb, line 17
def start
  puts "Which type of APOD lookup would you like to perform?"
  puts "[1]".colorize(:red) + " Search by date"
  puts "[2]".colorize(:red) + " Search by name"
  puts "[3]".colorize(:red) + " Search by date and name"
  puts "[4]".colorize(:red) + " Sample data"
  search_type = valid_input(["1", "2", "3", "4"]).to_i
  case search_type
  when 1
    date_search
  when 2
    name_search
  when 3
    date_search(true)
  when 4
    sample
  end
  puts "Would you like to perform another lookup?"
  puts "[y/n]".colorize(:red)
  if valid_input(["y", "n"]) == "y"
    puts ""
    start
  else
    puts "Goodbye."
  end
end
valid_input(wanted) click to toggle source
# File lib/apod_cli/cli.rb, line 219
  def valid_input(wanted)
  input = gets.chomp.strip.downcase
  if wanted.include?(input)
    return input
  else
    puts "That's not a valid input! Please try again."
    valid_input(wanted)
  end
end