Class: RainJackets::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/rain_jackets/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#jacketsObject

Returns the value of attribute jackets.



2
3
4
# File 'lib/rain_jackets/cli.rb', line 2

def jackets
  @jackets
end

Instance Method Details

#callObject



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_inputObject



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)
    print_ratings(input)

  elsif input == "menu"
    print_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

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.overall_rating}/100 Overall Rating"
    end
  puts "-----------------------------------------------------------------------------------"
end

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 print_menu
  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

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 print_ratings(input)
  rating_attribute = read_rating_input(input)
  # Dynamically access specified attribute reader and
  # Sort all jacket instances in descending order of the rating attribute's value
  jackets_sorted_by_rating = @jackets.sort_by { |jacket| jacket.send(rating_attribute)}.reverse

  # Convert rating attribute name to readable capitalized title
  rating_category_name = rating_attribute.split('_').map(&:capitalize).join(' ')

  puts ""
  puts "-------------Best Jackets Ranked by #{rating_category_name} ------------------"
  jackets_sorted_by_rating.each_with_index do |jacket, idx|
    puts " #{idx + 1}. #{jacket.name}#{jacket.send(rating_attribute)}/10"
  end
  puts "---------------------------------------------------------------------"
end

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.overall_rating}/100"
  puts "• Rating Categories:"
  puts "  - Water Resistance: #{jacket.water_resistance_rating}/10"
  puts "  - Breathability: #{jacket.breathability_rating}/10"
  puts "  - Comfort: #{jacket.comfort_rating}/10"
  puts "  - Weight: #{jacket.weight_rating}/10"
  puts "  - Durability: #{jacket.durability_rating}/10"
  puts "  - Packed Size: #{jacket.packed_size_rating}/10"
  puts "-----------------------------------------------------------------"
end

#prompt_inputObject

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 read_rating_input(input)
  # case statement to run multiple conditions against input value
  case input
    when 'wr'
      rating_category = "water_resistance_rating"
    when 'b'
      rating_category = "breathability_rating"
    when 'c'
      rating_category = "comfort_rating"
    when 'w'
      rating_category = "weight_rating"
    when 'd'
      rating_category = "durability_rating"
    when 'ps'
      rating_category = "packed_size_rating"
  end
  rating_category
end