Class: CLI
- Inherits:
-
Object
- Object
- CLI
- Defined in:
- lib/barbershop_contestants/cli.rb
Overview
command line interface responsible for starting the scraping/org process and providing the user with an interface
Class Method Summary collapse
- .choose_source ⇒ Object
- .display(type, commands) ⇒ Object
- .display_champs(type) ⇒ Object
- .display_year(year, type) ⇒ Object
- .help(*_) ⇒ Object
- .input_loop ⇒ Object
- .load(_, commands) ⇒ Object
- .load_all(type) ⇒ Object
- .no_command ⇒ Object
- .parse_command_verb(commands) ⇒ Object
- .print_tty_table(title: nil, headers: nil, rows:) ⇒ Object
- .process_command(command) ⇒ Object
- .quit(*_) ⇒ Object
- .request_command ⇒ Object
- .show_competitor(name) ⇒ Object
- .show_competitor_performances(com) ⇒ Object
- .start ⇒ Object
Class Method Details
.choose_source ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# 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) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# 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) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 |
# 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) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 |
# 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(*_) ⇒ Object
154 155 156 |
# File 'lib/barbershop_contestants/cli.rb', line 154 def self.help(*_) request_command end |
.input_loop ⇒ Object
48 49 50 51 52 53 54 |
# 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) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 |
# 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) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 |
# 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 ⇒ Object
208 209 210 211 |
# 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) ⇒ Object
83 84 85 86 87 |
# 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:) ⇒ Object
197 198 199 200 201 202 203 204 205 206 |
# File 'lib/barbershop_contestants/cli.rb', line 197 def self.print_tty_table(title: nil, headers: nil, rows:) puts title if title # binding.pry if headers table = TTY::Table.new headers, rows else table = TTY::Table.new rows end puts table.render(:ascii, padding: [0,1,0,1]) end |
.process_command(command) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 |
# 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(*_) ⇒ Object
158 159 160 161 |
# File 'lib/barbershop_contestants/cli.rb', line 158 def self.quit(*_) puts "Goodbye!" exit end |
.request_command ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# 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) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# 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) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# 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 ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# 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 |