Class: WeatherUsa::CLI

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

Constant Summary collapse

BASE_PATH =
"https://api.weather.gov/"

Instance Method Summary collapse

Instance Method Details

#ask_for_new_locationObject



28
29
30
31
32
33
34
# File 'lib/weather_usa/cli.rb', line 28

def ask_for_new_location 
  puts     
  puts "  Please enter a geographic location in the United States.".blue
  puts "      (This can be a zip code or a city and state)".blue
  puts
  print "    >>  ".green  
end

#callObject



5
6
7
8
9
# File 'lib/weather_usa/cli.rb', line 5

def call
  system "clear"
  self.greeting
  self.new_location    
end

#create_weather_objects_from_arrayObject



141
142
143
144
145
# File 'lib/weather_usa/cli.rb', line 141

def create_weather_objects_from_array    
  @weather_array.each do |period|
    Weather.new(period)
  end 
end

#current_conditionsObject



147
148
149
# File 'lib/weather_usa/cli.rb', line 147

def current_conditions
  Weather.all[0]
end

#display_alertObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/weather_usa/cli.rb', line 88

def display_alert
  if current_conditions.current[:alert_headline]
    puts "    Current alert:  ".red + "#{current_conditions.current[:alert_headline]}.".yellow
    puts
    puts "    Do you want to see a detailed description of the alert? (y/n)".blue
    puts
    print "    >>  ".green
    input = gets.strip.downcase
    puts
    if input == "y"
      system "clear"
      current_conditions.current[:alert_description].split("\n").each do |line|
        puts "    #{line}".yellow
      end
      puts
    end
  end    
end

#display_current_conditionsObject



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/weather_usa/cli.rb', line 76

def display_current_conditions
  right_now = self.current_conditions
  right_now.current[:wind_speed] != nil ? wind_speed = meters_to_miles(right_now.current[:wind_speed]).round(0) : wind_speed = "N/A"
  right_now.current[:temperature] != nil ? temperature = to_fahr(right_now.current[:temperature]) : temperature = "N/A"
  puts "    Weather forecast for ".blue + "#{@location.name}".green + ":".blue
  puts "    Current observations at: ".blue + "#{right_now.current[:station_name]}".green + ",".blue
  puts "        the current temperature is ".blue + "#{temperature}".green + ",".blue
  puts "        the current wind speed is ".blue + "#{wind_speed} mph".green + " from the ".blue + 
                "#{wind_direction_from_degrees(right_now.current[:wind_direction])}".green + ".".blue
  puts "        Expected conditions: ".blue + "#{right_now.short_forecast}.".green
end

#display_detailed_forecastObject



107
108
109
110
111
112
113
114
# File 'lib/weather_usa/cli.rb', line 107

def display_detailed_forecast    
  puts
  puts
  puts "    Weather forecast for ".blue + "#{@location.name}".green + ":".blue
  current_conditions.detailed_forecast.split(".").each do |line|
    puts "    #{line}.".green
  end    
end

#display_extended_forecastObject



116
117
118
119
120
121
122
123
124
125
# File 'lib/weather_usa/cli.rb', line 116

def display_extended_forecast
  Weather.all.each_with_index do |period, index|
    period.is_daytime ? am_pm = "High" : am_pm = "Low"
    if index < 8
      puts "    #{period.name}".green
      puts "        #{period.short_forecast}".green
      puts "        #{am_pm} temperature: ".blue + "#{period.temperature}".green
    end   
  end
end

#get_locationObject



133
134
135
# File 'lib/weather_usa/cli.rb', line 133

def get_location
  @location = Geocode.get_geocode(@input)
end

#get_weather_informationObject



127
128
129
130
131
# File 'lib/weather_usa/cli.rb', line 127

def get_weather_information
  self.get_location
  self.scrape_weather_site
  self.create_weather_objects_from_array        
end

#goodbyeObject



36
37
38
39
40
41
42
43
# File 'lib/weather_usa/cli.rb', line 36

def goodbye
  puts
  puts
  puts "  *** Thank you for using Weather USA. ***".blue
  puts
  puts
  exit
end

#greetingObject



22
23
24
25
26
# File 'lib/weather_usa/cli.rb', line 22

def greeting
  puts
  puts 
  puts "        *** Welcome to Weather USA! ***".blue    
end

#meters_to_miles(number) ⇒ Object



155
156
157
# File 'lib/weather_usa/cli.rb', line 155

def meters_to_miles(number)
  (number * 2.23694).round(0)
end

#new_locationObject



11
12
13
14
15
16
17
18
19
20
# File 'lib/weather_usa/cli.rb', line 11

def new_location
  Weather.clear_all
  self.ask_for_new_location    
  @input = gets.strip
  self.get_weather_information
  system "clear"
  self.display_current_conditions
  self.display_alert
  self.options    
end

#optionsObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/weather_usa/cli.rb', line 45

def options
  puts
  puts "    What would you like to do? (Please select an option.)".blue
  puts
  puts "        1) See a detailed forecast for the current location.".blue
  puts
  puts "        2) See an extended forecast.".blue
  puts
  puts "        3) Choose a different location.".blue
  puts
  puts "        4) Exit the program.".blue
  puts
  print "    >>  ".green
  option = gets.strip.to_s
  system "clear"
  case option
  when "1"
    self.display_detailed_forecast
    self.options
  when "2"
    self.display_extended_forecast
    self.options
  when "3"      
    self.new_location
  when "4"
    self.goodbye
  else
    self.options
  end
end

#scrape_weather_siteObject



137
138
139
# File 'lib/weather_usa/cli.rb', line 137

def scrape_weather_site
  @weather_array = Scraper.scrape_weather_dot_gov(BASE_PATH, @location)
end

#to_fahr(number) ⇒ Object



151
152
153
# File 'lib/weather_usa/cli.rb', line 151

def to_fahr(number)
  ((number * 9) / 5 + 32).round(0) 
end

#wind_direction_from_degrees(degrees) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/weather_usa/cli.rb', line 159

def wind_direction_from_degrees(degrees)
  case degrees    
  when 0..22.5
    "N"
  when 22.6..67.5
    "NE"
  when 67.6..112.5
    "E"
  when 112.6..157.5
    "SE"
  when 157.6..202.5
    "S"
  when 202.6..247.5
    "SW"
  when 247.6..292.5
    "W"
  when 292.6..337.5
    "NW"
  when 337.6..360
    "N"
  else
    "N/A"
  end
end