Class: ClimateTracker::CLI
- Inherits:
-
Object
- Object
- ClimateTracker::CLI
- Defined in:
- lib/climate_tracker/cli.rb
Instance Attribute Summary collapse
-
#data_set ⇒ Object
Returns the value of attribute data_set.
-
#delta_temp ⇒ Object
Returns the value of attribute delta_temp.
-
#start_date ⇒ Object
Returns the value of attribute start_date.
-
#start_date_temp ⇒ Object
Returns the value of attribute start_date_temp.
-
#std_stop_date ⇒ Object
Returns the value of attribute std_stop_date.
-
#stop_date ⇒ Object
Returns the value of attribute stop_date.
Instance Method Summary collapse
- #call ⇒ Object
- #compare ⇒ Object
- #date_valid?(date) ⇒ Boolean
-
#initialize ⇒ CLI
constructor
A new instance of CLI.
- #input_valid? ⇒ Boolean
- #list ⇒ Object
- #pick_date ⇒ Object
- #pick_state ⇒ Object
- #standarize_date(date) ⇒ Object
Constructor Details
#initialize ⇒ CLI
Returns a new instance of CLI.
4 5 6 7 8 9 10 |
# File 'lib/climate_tracker/cli.rb', line 4 def initialize current_date = DateTime.now.to_date.strftime("%F") date_array = current_date.split("-") date_array[0] = date_array[0].to_i-1 @std_stop_date = date_array.join("-") #Dataset doesn't go to 2016 so need to use 2015 as maximum year. @data_set = ClimateTracker::NOAA_Data.new end |
Instance Attribute Details
#data_set ⇒ Object
Returns the value of attribute data_set.
2 3 4 |
# File 'lib/climate_tracker/cli.rb', line 2 def data_set @data_set end |
#delta_temp ⇒ Object
Returns the value of attribute delta_temp.
2 3 4 |
# File 'lib/climate_tracker/cli.rb', line 2 def delta_temp @delta_temp end |
#start_date ⇒ Object
Returns the value of attribute start_date.
2 3 4 |
# File 'lib/climate_tracker/cli.rb', line 2 def start_date @start_date end |
#start_date_temp ⇒ Object
Returns the value of attribute start_date_temp.
2 3 4 |
# File 'lib/climate_tracker/cli.rb', line 2 def start_date_temp @start_date_temp end |
#std_stop_date ⇒ Object
Returns the value of attribute std_stop_date.
2 3 4 |
# File 'lib/climate_tracker/cli.rb', line 2 def std_stop_date @std_stop_date end |
#stop_date ⇒ Object
Returns the value of attribute stop_date.
2 3 4 |
# File 'lib/climate_tracker/cli.rb', line 2 def stop_date @stop_date end |
Instance Method Details
#call ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/climate_tracker/cli.rb', line 12 def call puts "" puts "Welcome to the Climate Tracker - New England" puts "" puts "" puts "This Climate Tracker displays the average monthly temperature for any date the User requests for New England." puts "" puts "Before we begin, a target state must be entered (this can be changed later if desired)." self.pick_state @input = "" until @input == "exit" do puts "Please type 'list' to find average temperature for your chosen state at a selected date, 'compare' to find the change in temperature between two dates, 'exit' to exit." @input = gets.strip.downcase until self.input_valid? do puts "Please enter either 'list' or 'compare':" @input = gets.strip.downcase end if @input == "compare" self.compare elsif @input == "list" self.list elsif @input == "exit" break end end end |
#compare ⇒ Object
66 67 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 |
# File 'lib/climate_tracker/cli.rb', line 66 def compare puts "This is the temperature comparision calculator. To begin, please answer a couple questions:" puts "" puts "Would you still like to use the target state: #{@state}? (y/n)" @input = gets.strip.downcase if @input == "n" @data_set.re_pull = true self.pick_state end if @data_set.pull_count >= 1 puts "Would you like to pick a new target date? Current target is #{@start_date}? (y/n)" @input = gets.strip.downcase if @input == "y" puts "What is your target date? (DD/MM/YYY)" target_date = self.pick_date @start_date = self.standarize_date(target_date) @data_set.re_pull = true end else puts "What is your target date? (DD/MM/YYY)" target_date = self.pick_date @start_date = self.standarize_date(target_date) end puts "Would you like to set a year to compare to? If not, will use one year ago today: #{@std_stop_date}. (y/n)" decide = gets.strip if decide == "y" || decide == "yes" puts "Please pick a date: (DD/MM/YYY)" stop_date = self.pick_date @stop_date = self.standarize_date(stop_date) else @stop_date = @std_stop_date end puts "Processing..." @delta_temp = @data_set.temp_difference(@start_date, @stop_date, @state) puts "In #{@state}, #{@stop_date} was #{@delta_temp[0]}°F #{@delta_temp[2]} than #{@start_date} (#{(@delta_temp[1]-100).round(2)}% #{@delta_temp[3]})!" end |
#date_valid?(date) ⇒ Boolean
135 136 137 138 139 140 141 |
# File 'lib/climate_tracker/cli.rb', line 135 def date_valid?(date) date =~ /\A\d{2}\/\d{2}\/\d{4}\z/ ? true : (return false) date_array = date.split("/") date_array[0].to_i > 00 && date_array[0].to_i < 31 ? true : (return false) date_array[1].to_i > 00 && date_array[1].to_i < 12 ? true : (return false) date_array[2].to_i > 1775 && date_array[2].to_i < 2016 ? true : (return false) end |
#input_valid? ⇒ Boolean
148 149 150 |
# File 'lib/climate_tracker/cli.rb', line 148 def input_valid? @input == "compare" || @input == "list" || @input == "exit" ? true : false end |
#list ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/climate_tracker/cli.rb', line 43 def list puts "This function will return the average monthly temperature for a given date in your chosen state." puts "" puts "Would you still like to use the target state: #{@state}? (y/n)" @input = gets.strip.downcase if @input == "n" @data_set.re_pull = true puts "Please pick a target state:" self.pick_state end puts "Great. Now please pick a date (DD/MM/YYY)" date = self.pick_date @start_date = self.standarize_date(date) puts "Processing..." #download avg monthly temp values for date & state, find average for year. temp = @data_set.pull_data(@start_date, @state).gather_values puts "#{@state}'s monthly average temperature on #{@start_date} was #{temp.round(2)}°F." @input = "" puts "Would you like to pick a new state or compare this result to another date? (list or compare)" end |
#pick_date ⇒ Object
125 126 127 128 129 130 131 132 133 |
# File 'lib/climate_tracker/cli.rb', line 125 def pick_date new_date = gets.strip until self.date_valid?(new_date) puts "Invalid date or date format. Please enter your target date: (DD/MM/YYY)" new_date = gets.strip end puts "Accepted." new_date end |
#pick_state ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/climate_tracker/cli.rb', line 108 def pick_state puts "Please pick your desired state. To see a list of states, type 'states':" state = gets.strip.upcase if state == 'STATES' || state == 'STATE' puts "Gathering states..." puts "#{ClimateTracker::NOAA_Data.states}" puts "" self.pick_state elsif @data_set.class.states.include?(state) puts "Accepted." @state = state else puts "State not recognized." self.pick_state end end |
#standarize_date(date) ⇒ Object
143 144 145 146 |
# File 'lib/climate_tracker/cli.rb', line 143 def standarize_date(date) date_array = date.split("/") standard_date = date_array.reverse!.join("-") end |