Class: RainJackets::CLI
- Inherits:
-
Object
- Object
- RainJackets::CLI
- Defined in:
- lib/rain_jackets/cli.rb
Instance Attribute Summary collapse
-
#jackets ⇒ Object
Returns the value of attribute jackets.
Instance Method Summary collapse
- #call ⇒ Object
- #get_input ⇒ Object
-
#handle_input(input) ⇒ Object
Handles the user pint.
-
#print_list_all ⇒ Object
Display all best rain jackets.
-
#print_menu ⇒ Object
Display all menu commands/ Define input interface.
-
#print_ratings(input) ⇒ Object
Display ranked list based on chosen rating category.
-
#print_selected_jacket(jacket_number) ⇒ Object
Display details of chosen jacket.
-
#prompt_input ⇒ Object
Print instructions to screen and prompts user for input.
-
#read_rating_input(input) ⇒ Object
Convert user input to jacket attribute name string.
Instance Attribute Details
#jackets ⇒ Object
Returns the value of attribute jackets.
2 3 4 |
# File 'lib/rain_jackets/cli.rb', line 2 def jackets @jackets end |
Instance Method Details
#call ⇒ Object
4 5 6 7 8 9 10 11 12 13 |
# File 'lib/rain_jackets/cli.rb', line 4 def call # Calls Scraper class method that will return array of all jacket objects # Store all Jacket instances in CLI's instance variable @jackets RainJackets::Scraper.initialize_jacket_objects @jackets = RainJackets::Jacket.all # Greet user with text input puts "" puts "Welcome to the Best Women's Rain Jackets Rater!" prompt_input end |
#get_input ⇒ Object
22 23 24 25 |
# File 'lib/rain_jackets/cli.rb', line 22 def get_input input = gets.strip.downcase handle_input(input) end |
#handle_input(input) ⇒ Object
Handles the user pint
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/rain_jackets/cli.rb', line 28 def handle_input(input) if input == "all" print_list_all elsif (1..5).include?(input.to_i) print_selected_jacket(input.to_i) elsif ['wr', 'b', 'c', 'w', 'd', 'ps'].include?(input) (input) elsif input == "menu" get_input elsif input == "exit" puts "Goodbye! Hope you found your favorite jacket!" exit else # Make sure that the program doesn't break with unexpected user input puts "" puts "I don't understand that answer. Please try again!" end prompt_input # Reinitiate program loop at the end of non-exited handle_input logic end |
#print_list_all ⇒ Object
Display all best rain jackets
55 56 57 58 59 60 61 62 63 |
# File 'lib/rain_jackets/cli.rb', line 55 def print_list_all puts "" puts "----------------------- Best Women's Rain Jackets of 2019: -----------------------------" @jackets.each_with_index do |jacket, i| ranking = (i+1).to_s puts " #{ranking}. #{jacket.name.split(" - ").first} — #{jacket.price} - #{jacket.}/100 Overall Rating" end puts "-----------------------------------------------------------------------------------" end |
#print_menu ⇒ Object
Display all menu commands/ Define input interface
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/rain_jackets/cli.rb', line 125 def puts "" puts "========================== MENU ===============================" puts "• List all jackets -> enter 'all'" puts "• More information on specific jacket -> enter jacket #'1-5'" puts "• List jackets by speific rating category -> enter:" puts " 'wr' — Water Resistance" puts " 'b' — Brethability" puts " 'c' — Comfort" puts " 'w' — Weight" puts " 'd' — Durability" puts " 'ps' — Packed Size" puts "• Exit program -> enter 'exit'" puts "===============================================================" puts "► What would you like to do?" end |
#print_ratings(input) ⇒ Object
Display ranked list based on chosen rating category
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/rain_jackets/cli.rb', line 107 def (input) = (input) # Dynamically access specified attribute reader and # Sort all jacket instances in descending order of the rating attribute's value = @jackets.sort_by { |jacket| jacket.send()}.reverse # Convert rating attribute name to readable capitalized title = .split('_').map(&:capitalize).join(' ') puts "" puts "-------------Best Jackets Ranked by #{} ------------------" .each_with_index do |jacket, idx| puts " #{idx + 1}. #{jacket.name} — #{jacket.send()}/10" end puts "---------------------------------------------------------------------" end |
#print_selected_jacket(jacket_number) ⇒ Object
Display details of chosen jacket
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/rain_jackets/cli.rb', line 66 def print_selected_jacket(jacket_number) jacket = @jackets[jacket_number - 1] puts "" puts "---------------- #{jacket_number}. #{jacket.name} ----------------" puts "• Jacket Description: #{jacket.description}" puts "• Price: #{jacket.price}" puts "• Pros: #{jacket.pros}" puts "• Cons: #{jacket.cons}" puts "• URL: #{jacket.url}" puts "• Overall Rating: #{jacket.}/100" puts "• Rating Categories:" puts " - Water Resistance: #{jacket.}/10" puts " - Breathability: #{jacket.}/10" puts " - Comfort: #{jacket.}/10" puts " - Weight: #{jacket.}/10" puts " - Durability: #{jacket.}/10" puts " - Packed Size: #{jacket.}/10" puts "-----------------------------------------------------------------" end |
#prompt_input ⇒ Object
Print instructions to screen and prompts user for input
16 17 18 19 20 |
# File 'lib/rain_jackets/cli.rb', line 16 def prompt_input puts "► What would you like to do?" puts "► Enter: 'menu' to see all commands / 'exit' to exit program." get_input end |
#read_rating_input(input) ⇒ Object
Convert user input to jacket attribute name string
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/rain_jackets/cli.rb', line 87 def (input) # case statement to run multiple conditions against input value case input when 'wr' = "water_resistance_rating" when 'b' = "breathability_rating" when 'c' = "comfort_rating" when 'w' = "weight_rating" when 'd' = "durability_rating" when 'ps' = "packed_size_rating" end end |