Class: EndangeredSpecies::CLI
- Inherits:
-
Object
- Object
- EndangeredSpecies::CLI
- Defined in:
- lib/endangered_species/cli.rb
Overview
CLI controller - user interaction
Instance Method Summary collapse
-
#call ⇒ Object
Main / general user input.
- #choose_articles(input) ⇒ Object
- #choose_species(input) ⇒ Object
- #continue ⇒ Object
- #continue_articles ⇒ Object
- #continue_species ⇒ Object
- #exit?(input) ⇒ Boolean
- #goodbye ⇒ Object
- #invalid?(input) ⇒ Boolean
-
#is_numeric?(input) ⇒ Boolean
Validate user input.
- #list_articles ⇒ Object
- #list_species ⇒ Object
-
#make_articles ⇒ Object
CLI - get user input for Articles on species (Enter 2 at menu).
-
#make_species ⇒ Object
CLI - get user input for Info on species (Enter 1 at menu).
- #menu ⇒ Object
- #more_info(species) ⇒ Object
- #no_user(input) ⇒ Object
- #print_articles(articles) ⇒ Object
- #print_more?(species) ⇒ Boolean
- #print_species(species) ⇒ Object
- #try_again ⇒ Object
- #yes_user(input) ⇒ Object
Instance Method Details
#call ⇒ Object
Main / general user input
7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/endangered_species/cli.rb', line 7 def call puts "" puts "Welcome! Learn about World Wildlife Fund's conservation of species and the environment!" sleep 1 puts "" puts "..." puts "" puts "...please wait one moment..." puts "" puts "..." make_species make_articles end |
#choose_articles(input) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/endangered_species/cli.rb', line 189 def choose_articles(input) input = "" while !exit?(input) puts "" puts "" puts "Please enter a number for which articles you would like to read:" puts "" input = gets.strip sleep 1 if invalid?(input) try_again elsif input.to_i-1 <= EndangeredSpecies::Articles.all.size articles = EndangeredSpecies::Articles.all[input.to_i-1] print_articles(articles) continue_articles else continue_articles end end end |
#choose_species(input) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/endangered_species/cli.rb', line 95 def choose_species(input) input = "" while !exit?(input) puts "" puts "" puts "Please enter a number for which species you would like to learn more about:" puts "" input = gets.strip sleep 1 if invalid?(input) continue_species elsif input.to_i-1 <= EndangeredSpecies::Species.all.size species = EndangeredSpecies::Species.all[input.to_i-1] print_species(species) print_more?(species) else continue_species end continue end end |
#continue ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/endangered_species/cli.rb', line 65 def continue input = "" while !exit?(input) puts "" puts "Return to main menu or exit? Enter Y or N." puts "" input = gets.strip if yes_user(input) elsif no_user(input) goodbye else try_again end end end |
#continue_articles ⇒ Object
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/endangered_species/cli.rb', line 210 def continue_articles input = "" while !exit?(input) puts "" puts "Do you want to continue reading articles? Enter Y or N." puts "" input = gets.strip if yes_user(input) list_articles choose_articles(input) elsif no_user(input) continue else try_again end end end |
#continue_species ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/endangered_species/cli.rb', line 136 def continue_species input = "" while !exit?(input) puts "" puts "Do you want to continue reading about species? Enter Y or N." puts "" input = gets.strip if yes_user(input) list_species choose_species(input) elsif no_user(input) continue else try_again end end end |
#exit?(input) ⇒ Boolean
252 253 254 |
# File 'lib/endangered_species/cli.rb', line 252 def exit?(input) true if input == "exit" || input == "exit!" rescue false end |
#goodbye ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/endangered_species/cli.rb', line 48 def goodbye sleep 1 puts "" puts "Thanks for visiting!" puts "" exit end |
#invalid?(input) ⇒ Boolean
248 249 250 |
# File 'lib/endangered_species/cli.rb', line 248 def invalid?(input) true if !is_numeric?(input) || ["nil", "0"].include?(input) rescue false end |
#is_numeric?(input) ⇒ Boolean
Validate user input
244 245 246 |
# File 'lib/endangered_species/cli.rb', line 244 def is_numeric?(input) true if Integer(input) rescue false end |
#list_articles ⇒ Object
182 183 184 185 186 187 |
# File 'lib/endangered_species/cli.rb', line 182 def list_articles EndangeredSpecies::Articles.all.each_with_index do |articles, i| puts "" puts "#{i+1}. #{articles.title}" end end |
#list_species ⇒ Object
88 89 90 91 92 93 |
# File 'lib/endangered_species/cli.rb', line 88 def list_species EndangeredSpecies::Species.all.each_with_index do |species, i| puts "" puts "#{i+1}. #{species.name}" end end |
#make_articles ⇒ Object
CLI - get user input for Articles on species (Enter 2 at menu)
178 179 180 |
# File 'lib/endangered_species/cli.rb', line 178 def make_articles EndangeredSpecies::Scraper.new.make_articles end |
#make_species ⇒ Object
CLI - get user input for Info on species (Enter 1 at menu)
84 85 86 |
# File 'lib/endangered_species/cli.rb', line 84 def make_species EndangeredSpecies::Scraper.new.make_species end |
#menu ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/endangered_species/cli.rb', line 22 def input = "" while !exit?(input) puts "" puts "Enter 1 if you would like to read more about endangered species." puts "" sleep 1 puts "or" puts "" puts "Enter 2 if you would like to read the latest news articles from WWF." puts "" input = gets.strip if ["1", "one"].include?(input) sleep 1 list_species choose_species(input) elsif ["2", "two"].include?(input) sleep 1 list_articles choose_articles(input) else try_again end end end |
#more_info(species) ⇒ Object
166 167 168 169 170 171 172 173 174 |
# File 'lib/endangered_species/cli.rb', line 166 def more_info(species) puts "" puts "----------- #{species.name} ----------- " puts "" puts "#{species.summary}" puts "" puts "#{species.habitat}" puts "" end |
#no_user(input) ⇒ Object
256 257 258 |
# File 'lib/endangered_species/cli.rb', line 256 def no_user(input) true if ["n", "no", "exit", "exit!"].include?(input.downcase) rescue false end |
#print_articles(articles) ⇒ Object
228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/endangered_species/cli.rb', line 228 def print_articles(articles) puts "" puts "----------- #{articles.title} ----------- " puts "" puts "Date: #{articles.date}" puts "" puts "Summary:" puts "#{articles.summary}" puts "" puts "More info:" puts "#{articles.url}" puts "" end |
#print_more?(species) ⇒ Boolean
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/endangered_species/cli.rb', line 117 def print_more?(species) input = "" while !exit?(input) puts "" puts "Would you like to read more? Enter Y or N." puts "" input = gets.strip.downcase if yes_user(input) more_info(species) sleep 1 continue_species elsif no_user(input) goodbye else try_again end end end |
#print_species(species) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/endangered_species/cli.rb', line 154 def print_species(species) puts "" puts "----------- #{species.name} ----------- " puts "" puts "Scientific name: #{species.scientific}" puts "" puts "Status: #{species.status}" puts "" puts "Website: #{species.url}" puts "" end |
#try_again ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/endangered_species/cli.rb', line 56 def try_again sleep 1 puts "" puts "Not sure what you're looking for, please try again!" puts "" sleep 1 continue end |
#yes_user(input) ⇒ Object
260 261 262 |
# File 'lib/endangered_species/cli.rb', line 260 def yes_user(input) true if ["y", "yes"].include?(input.downcase) rescue false end |