Class: WeatherPup::CLI
- Inherits:
-
Object
- Object
- WeatherPup::CLI
- Defined in:
- lib/weatherpup/cli.rb
Overview
CLI Controller
Instance Method Summary collapse
- #call ⇒ Object
-
#fetch_by_gps ⇒ Object
fetch the current conditions via zip code input from the user.
-
#fetch_by_zip ⇒ Object
fetches the current conditions via zip code input from the user.
- #fetch_previous ⇒ Object
- #get_and_print_by_gps(latitude, longitude) ⇒ Object
- #get_and_print_by_zip(zip_code) ⇒ Object
-
#goodbye ⇒ Object
Says thank you and goodbye to user.
- #main_menu ⇒ Object
-
#return_to_main_menu_prompt ⇒ Object
This helper method displays instructions for the user to return to the main menu and then waits for the user to type in “back” before proceeding.
-
#valid_coordinate_pair?(latitude, longitude) ⇒ Boolean
Do some basic checks to make sure that the coordinates are legit.
-
#zip_code_valid?(zip_to_check) ⇒ Boolean
Input validation method - Checks the VALID_US_ZIP_CODES constant to see if the zip code is real.
Instance Method Details
#call ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/weatherpup/cli.rb', line 3 def call #Clears the screen to make the interface look nicer / cleaner system "clear" #Welcome the user puts <<~WELCOME ▒█░░▒█ █▀▀ █░░ █▀▀ █▀▀█ █▀▄▀█ █▀▀ ▀▀█▀▀ █▀▀█ ▒█▒█▒█ █▀▀ █░░ █░░ █░░█ █░▀░█ █▀▀ ░▒█░░ █░░█ ▒█▄▀▄█ ▀▀▀ ▀▀▀ ▀▀▀ ▀▀▀▀ ▀░░░▀ ▀▀▀ ░▒█░░ ▀▀▀▀ ▒█░░▒█ █▀▀ █▀▀█ ▀▀█▀▀ █░░█ █▀▀ █▀▀█ ▒█▀▀█ █░░█ █▀▀█ █ ▒█▒█▒█ █▀▀ █▄▄█ ░░█░░ █▀▀█ █▀▀ █▄▄▀ ▒█▄▄█ █░░█ █░░█ ▀ ▒█▄▀▄█ ▀▀▀ ▀░░▀ ░░▀░░ ▀░░▀ ▀▀▀ ▀░▀▀ ▒█░░░ ░▀▀▀ █▀▀▀ ▄ ░░░░░░░░░░░░░░░░▄██ ░░░░▄████▄▄▄▄▄▄███████ ░░▄█▀████████████▀ ░▄▀░██▀██▀▀▀▀▀██▀▀▄ ░░░░█▄░▀█▄░░░░▀█▄▀▀ WELCOME #Run the main menu self. end |
#fetch_by_gps ⇒ Object
fetch the current conditions via zip code input from the user
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/weatherpup/cli.rb', line 139 def fetch_by_gps #initialize my trigger variable valid_coordinates for my until loop valid_coordinates = nil #Clear my screen first to make things look nicer system "clear" #Ask user for the latitude and longitude until we get a valid input. puts "\nOk, Current Weather Conditions by GPS coordinates!".colorize(:light_blue) until valid_coordinates #Grab the Lat from the user puts "\nPlease enter in the ".colorize(:light_blue) + "latitude".colorize(:green).underline + " in decimal notation:".colorize(:light_blue) latitude = gets.chomp #Checking to see if the user wants out of this loop and back to the main menu before proceeding if latitude.downcase == "back" system "clear" break end #Grab the Longitude from the user puts "\nPlease enter in the ".colorize(:light_blue) + "longitude".colorize(:magenta).underline + " in decimal notation:".colorize(:light_blue) longitude = gets.chomp #Checking to see if the user wants out of this loop and back to the main menu if longitude.downcase == "back" system "clear" break end #Checking to see if the coordinates are valid using method #valid_coordinate_pair? valid_coordinates = self.valid_coordinate_pair?(latitude, longitude) # If the coordinates are valid, get and display the weather info. if valid_coordinates #Clear the screen to make it look nicer system "clear" #Do the actual work of getting the data from the API, processing it, and printing it to screen self.get_and_print_by_gps(latitude, longitude) #Next I wait for the user to type in "back" to return to the main menu self. #Once the user types back, clear the screen and break out of this until loop system "clear" break else puts <<~INVALID_GPS \n#{"Invalid coordinates detected!".colorize(:light_red)} (Type #{"back".colorize(:red)} to return to the main menu). INVALID_GPS end end end |
#fetch_by_zip ⇒ Object
fetches the current conditions via zip code input from the user
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/weatherpup/cli.rb', line 68 def fetch_by_zip #initialize my trigger variable zip_code_valid for my until loop zip_code_valid = nil #Clear my screen first to make things look nicer system "clear" #Ask user for the zip code until we get a valid input. puts "\nOk, Current Weather Conditions by Zip Code!".colorize(:green) until zip_code_valid puts "\nPlease enter in the 5 Digit Zip Code:".colorize(:green) zip_code = gets.chomp #Checking to see if the user wants out of this loop and back to the main menu if zip_code.downcase == "back" system "clear" break end #Otherwise, run #zip_code_valid to check to see if what the user put in was valid zip_code_valid = zip_code_valid?(zip_code) #If the zipcode input is valid, get and display the weather info. if zip_code_valid #clear the screen system "clear" #Do the actual work of getting the data from the API, processing it, and printing it to screen self.get_and_print_by_zip(zip_code) #Next I wait for the user to type in "back" to return to the main menu self. #Once the user types back, clear the screen and break out of this until loop system "clear" break else #If given junk data (invalid zip, jibberish input, etc) tell the user its not valid then tell them how to get back to the main menu if they want. puts <<~INVALID_ZIP \n#{"Invalid zip code detected!".colorize(:light_red)} (Type #{"back".colorize(:red)} to return to the main menu). INVALID_ZIP end end end |
#fetch_previous ⇒ Object
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/weatherpup/cli.rb', line 222 def fetch_previous #Clear the screen to make it look nicer. system "clear" #first check to see if there have been any previous checks -- if the array is blank, tell the user that there isn't anything to show. Otherwise, print out a list of each WeatherConditions instance with details for the user pick from. if WeatherPup::WeatherConditions.all == [] system "clear" puts "\nThere are no previous fetches to display!".colorize(:cyan) self. system "clear" else #puts a blank line to give some headroom when displaying puts "\n" #ask the WeatherConditions class to print out a list of all its instances WeatherPup::WeatherConditions.list_all_previous #set my valid input range between 1 instance and however many instances of WeatherConditions there are in WeatherConditions @@all array. valid_input_range = 1..WeatherPup::WeatherConditions.all.length #Initialize my trigger to exit my until loop valid_input = nil #Until the user gives me a valid input, keep asking for a vaild input & give them the option to go back to the main menu. If they do give me valid input, then go display that historic WeatherConditions object. until valid_input puts <<~USER_PROMPT \nPlease type in the #{"number".colorize(:cyan)} of the previous fetch you would like to view or type #{"back".colorize(:red)} to return to the main menu. USER_PROMPT #get the input fro mthe user user_selection = gets.chomp.downcase if user_selection == "back" system "clear" break end #convert my user input to an integer so that I can check to see if its in the valid range. user_integer = user_selection.to_i valid_input = valid_input_range.member?(user_integer) #If it is in the valid range, go and print out the selected WeatherConditions objects info again. Otherwise, keep asking for valid input and tell them how to go back. if valid_input selected_wc_obj = WeatherPup::WeatherConditions.all[user_integer - 1] type = selected_wc_obj.current_conditions_means case type when "Zip Code" system "clear" selected_wc_obj.print_zip_conditions self. system "clear" break when "GPS Coordinates" system "clear" selected_wc_obj.print_gps_conditions self. system "clear" break end else puts "\n#{"Invalid selection.".colorize(:light_red)} Please try again or type #{"back".colorize(:red)} to return to the main menu." end end end end |
#get_and_print_by_gps(latitude, longitude) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/weatherpup/cli.rb', line 209 def get_and_print_by_gps(latitude, longitude) #Instantiate a new instance of WeatherConditions class gps_weather_conditions = WeatherPup::WeatherConditions.new #go fetch the raw api data api_raw_data = gps_weather_conditions.gps_api_fetch(latitude, longitude) #process raw data into an attributes hash to use with mass assignment method #write_attributes api_processed_data_hash = gps_weather_conditions.gps_process_api_data_to_attribs_hash(api_raw_data) #This next line takes the gps_weather_conditions variable which is a WeatherConditions Object Instance, taps into it and writes all of the attributes that I collected, then it prints the information located in the object itself to the user. gps_weather_conditions.tap {|weather_conditions_obj| weather_conditions_obj.write_attributes(api_processed_data_hash)}.print_gps_conditions end |
#get_and_print_by_zip(zip_code) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/weatherpup/cli.rb', line 120 def get_and_print_by_zip(zip_code) #Instantiate a new instance of WeatherConditions class zip_weather_conditions = WeatherPup::WeatherConditions.new #set the zip_code attribute to the zip code entered by the user zip_weather_conditions.zip_code = zip_code #go fetch the raw api data api_raw_data = zip_weather_conditions.zip_api_fetch(zip_code) #process raw data into an attributes hash to use with mass assignment method #write_attributes api_processed_data_hash = zip_weather_conditions.zip_process_api_data_to_attribs_hash(api_raw_data) #This next line takes the zip_weather_conditions variable which is a WeatherConditions Object Instance, taps into it and writes all of the attributes that I collected & processed from the API, then it prints the information located in the object itself to the user. zip_weather_conditions.tap {|weather_conditions_obj| weather_conditions_obj.write_attributes(api_processed_data_hash)}.print_zip_conditions end |
#goodbye ⇒ Object
Says thank you and goodbye to user.
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/weatherpup/cli.rb', line 302 def goodbye system "clear" puts <<~GOODBYE ▀▀█▀▀ █░░█ █▀▀█ █▀▀▄ █░█ █▀▀ █▀▀ █▀▀█ █▀▀█ ░▒█░░ █▀▀█ █▄▄█ █░░█ █▀▄ ▀▀█ █▀▀ █░░█ █▄▄▀ ░▒█░░ ▀░░▀ ▀░░▀ ▀░░▀ ▀░▀ ▀▀▀ ▀░░ ▀▀▀▀ ▀░▀▀ █▀▀█ █░░ █▀▀█ █░░█ ░▀░ █▀▀▄ █▀▀▀ █▀▀ █▀▀ ▀▀█▀▀ █▀▀ █░░█ █░░█ █░░ █▄▄█ █▄▄█ ▀█▀ █░░█ █░▀█ █▀▀ █▀▀ ░░█░░ █░░ █▀▀█ █▀▀▀ ▀▀▀ ▀░░▀ ▄▄▄█ ▀▀▀ ▀░░▀ ▀▀▀▀ ▀░░ ▀▀▀ ░░▀░░ ▀▀▀ ▀░░▀ █░░░█ ░▀░ ▀▀█▀▀ █░░█ █▀▄▀█ █▀▀ █ █▄█▄█ ▀█▀ ░░█░░ █▀▀█ █░▀░█ █▀▀ ▀ ░▀░▀░ ▀▀▀ ░░▀░░ ▀░░▀ ▀░░░▀ ▀▀▀ ▄ Thank you for using WeatherPup! Cool Welcome and Goodbye Text by fsymbols.com Created by Jeremiah Rodden 2019 GOODBYE end |
#main_menu ⇒ Object
27 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 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/weatherpup/cli.rb', line 27 def #Initialize my trigger variable "input" for my until loop input = "no input yet" until input.downcase == 'exit' #Show the user their options and provide instructions on how to use. puts <<~MAINMENU #{"How would you like me to fetch the current weather conditions for you today?".colorize(:light_red)} #{"1. Fetch by Zip Code".colorize(:green)} #{"2. Fetch by GPS Coordinates (Latitude and Longitude)".colorize(:light_blue)} #{"3. Fetch previously fetched conditions".colorize(:cyan)} Please type #{"1".colorize(:green)}, #{"2".colorize(:light_blue)}, #{"3".colorize(:cyan)} or type #{"exit".colorize(:red)} to quit. MAINMENU #get input from the user input = gets.chomp.downcase #Check that input for a valid input. If valid, then do what the user selected. Else, tell them the input is not valid and display the options again. case input when "1" self.fetch_by_zip when "2" self.fetch_by_gps when "3" self.fetch_previous when "exit" break else puts "\nSorry, that isn’t a valid input.".colorize(:red) end end #Once the user types exit, clear the screen and then display the goodbye message system "clear" self.goodbye end |
#return_to_main_menu_prompt ⇒ Object
This helper method displays instructions for the user to return to the main menu and then waits for the user to type in “back” before proceeding
293 294 295 296 297 298 299 |
# File 'lib/weatherpup/cli.rb', line 293 def return_to_main = "" until return_to_main.downcase == "back" puts "\nType #{"back".colorize(:red)} to return to the main menu." return_to_main = gets.chomp end end |
#valid_coordinate_pair?(latitude, longitude) ⇒ Boolean
Do some basic checks to make sure that the coordinates are legit
198 199 200 201 202 203 204 205 206 207 |
# File 'lib/weatherpup/cli.rb', line 198 def valid_coordinate_pair?(latitude, longitude) #If the lat & long user input contains alphabet characters, it's not valid if latitude.match(/[a-zA-Z]/) || longitude.match(/[a-zA-Z]/) false else #Checks to see if both the lat & long user input are within valid ranges VALID_LAT_RANGE.member?(latitude.to_f) && VALID_LONG_RANGE.member?(longitude.to_f) end #returns true if the lat and long coordinate pair is valid. end |
#zip_code_valid?(zip_to_check) ⇒ Boolean
Input validation method - Checks the VALID_US_ZIP_CODES constant to see if the zip code is real
116 117 118 |
# File 'lib/weatherpup/cli.rb', line 116 def zip_code_valid?(zip_to_check) VALID_US_ZIP_CODES.include?(zip_to_check) end |