class CLI

command line interface responsible for starting the scraping/org process and providing the user with an interface

Public Class Methods

choose_source() click to toggle source
# File lib/barbershop_contestants/cli.rb, line 37
def self.choose_source
  return :web
  # loop do
    # puts "Enter web or local:"
    # entry = gets.chomp
    # return entry.to_sym if %w[web local].include?(entry)
  # end
  ### Reverse the comments in this method to be offered a choice at launch
  ### between web scraping and local scraping.
end
display(type, commands) click to toggle source
# File lib/barbershop_contestants/cli.rb, line 89
def self.display(type, commands)
  # type contains competitor type string (chorus or quartet)
  # commands contains remainder of user input as string array
  if commands.any? { |c| c.start_with?("cham") } # check for champs command
    display_champs(type)
    true
  elsif (year = commands.find { |c| @type_years_hash[type].include? c.to_i })
    display_year(year.to_i, type)
    true
  else
    false
  end
end
display_champs(type) click to toggle source
# File lib/barbershop_contestants/cli.rb, line 103
def self.display_champs(type)
  champs_arr = Performance.filter_all(place: 1, type: type)
  title = "BHS International #{type.capitalize} Champions"
  headers = ["Year", type.capitalize, "District", "Score"]
  rows = champs_arr.map do |p|
    disp_arr = []
    disp_arr.push(p.year, p.competitor.name, p.competitor.district, p.score)
  end
  print_tty_table(title: title, headers: headers, rows: rows)
  true
end
display_year(year, type) click to toggle source
# File lib/barbershop_contestants/cli.rb, line 115
def self.display_year(year, type)
  Scraper.scrape_and_create_year(@source, year, type)
  year_arr = Performance.filter_all(year: year, type: type)
  title = "BHS International #{type.capitalize} Competition #{year}"
  headers = ["Place", type.capitalize, "District", "Score"]
  rows = year_arr.map do |p|
    disp_arr = []
    disp_arr.push(p.place, p.competitor.name, p.competitor.district, p.score)
  end
  print_tty_table(title: title, headers: headers, rows: rows)
end
help(*_) click to toggle source
# File lib/barbershop_contestants/cli.rb, line 154
def self.help(*_)
  request_command
end
input_loop() click to toggle source
# File lib/barbershop_contestants/cli.rb, line 48
def self.input_loop
  # binding.pry
  loop do
    puts "\nEnter a command:"
    process_command(gets.chomp.downcase)
  end
end
load(_, commands) click to toggle source
# File lib/barbershop_contestants/cli.rb, line 127
def self.load(_, commands)
  case commands
  when commands.any? { |c| c.start_with?("quar") }
    type = "quartet"
  when commands.any? { |c| c.start_with?("chor") }
    type = "chorus"
  else
    type = nil
  end
  load_all(type)
  true
end
load_all(type) click to toggle source
# File lib/barbershop_contestants/cli.rb, line 140
def self.load_all(type)
  if type
    @type_years_hash[type].each do |y|
      Scraper.scrape_and_create_year(@source, y, type)
    end
  else
    @type_years_hash.each do |type, year_range|
      year_range.each do |year|
        Scraper.scrape_and_create_year(@source, year, type)
      end
    end
  end
end
no_command() click to toggle source
# File lib/barbershop_contestants/cli.rb, line 208
def self.no_command
  puts "Sorry, that command was not recognized\n"
  request_command
end
parse_command_verb(commands) click to toggle source
# File lib/barbershop_contestants/cli.rb, line 83
def self.parse_command_verb(commands)
  verb = @command_verb_hash.find { |k, _| commands[0].start_with?(k) }
  # binding.pry
  verb ? send(verb[1][0], verb[1][1], commands.drop(1)) : false
end
print_tty_table(title: nil, headers: nil, rows:) click to toggle source
process_command(command) click to toggle source
# File lib/barbershop_contestants/cli.rb, line 71
def self.process_command(command)
  # parses the given input between command types.
  # full "quartet" and "chorus" parsing is in other methods.
  system "clear" or system "cls"
  command_arr = command.split
  if command_arr[0] # the user typed something
    parse_command_verb(command_arr) || show_competitor(command) || no_command
  else
    no_command
  end
end
quit(*_) click to toggle source
# File lib/barbershop_contestants/cli.rb, line 158
def self.quit(*_)
  puts "Goodbye!"
  exit
end
request_command() click to toggle source
# File lib/barbershop_contestants/cli.rb, line 56
def self.request_command
  puts "Please enter a command."
  puts "To see all entries in a contest, enter the type of contest " \
        "(quartet or chorus) and the year (1939-2018 for quartets, " \
        "1953-2018 for choruses)"
  puts "To see all performances currently in cache by a particular group, " \
        "enter the name of the group"
  puts "To see a list of all champions for a contest type, enter " \
      "'quartet champions' or 'chorus champions'"
  puts "To load all contests, or all contests of one type, " \
      "enter 'load all', 'load quartets', or 'load choruses'"
  puts "To quit, enter 'quit'"
  puts "To see this info again, enter 'help'"
end
show_competitor(name) click to toggle source
# File lib/barbershop_contestants/cli.rb, line 163
def self.show_competitor(name)
  comp = Competitor.all.find{ |c| c.name.downcase == name }
  # binding.pry
  if comp
    puts comp.name, comp.type.capitalize, "District: #{comp.district}"
    if comp.type == "quartet"
      comp.comments && (puts "Comments: #{comp.comments}")
      comp.members && (puts "Members: #{comp.members}")
    else
      comp.director && (puts "Director: #{comp.director}")
      comp.hometown && (puts "Hometown: #{comp.hometown}")
    end
    show_competitor_performances(comp)
  else
    false
  end
  true
end
show_competitor_performances(com) click to toggle source
# File lib/barbershop_contestants/cli.rb, line 182
def self.show_competitor_performances(com)
  title = "Performances"
  headers = ["Year", "Place", "Score"]
  headers << "# On Stage" if com.type == "chorus"
  perf_arr = com.performances.sort_by { |p| p.year }.reverse
  # binding.pry
  rows = perf_arr.map do |p|
    disp_arr = []
    disp_arr.push(p.number_on_stage) if com.type == "chorus"
    disp_arr.unshift(p.year, p.place, p.score)
  end
  # binding.pry
  print_tty_table(title: title, headers: headers, rows: rows)
end
start() click to toggle source
# File lib/barbershop_contestants/cli.rb, line 22
def self.start
  # welcome the user and show command list
  # have the bin file call this method
  # scrape data from here, logic primarily in scraper
  system "clear" or system "cls"
  puts @welcome_message
  @source = choose_source
  Scraper.scrape_and_create_quartet_champs(@source)
  Scraper.scrape_and_create_chorus_champs(@source)
  # Scraper.scrape_and_create_year(@source, 2018, "quartet") # remove in final
  # Scraper.scrape_and_create_year(@source, 2018, "chorus") # remove in final
  request_command
  input_loop
end