weatherpup 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +2 -1
- data/Gemfile.lock +1 -1
- data/README.md +3 -3
- data/lib/weatherpup.rb +3 -2
- data/lib/weatherpup/cli.rb +110 -56
- data/lib/weatherpup/version.rb +1 -1
- data/lib/weatherpup/weather_conditions.rb +32 -18
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f1e92311036112a9b35d4eb6b651a067c56a2043b7a978c23de3787649103e17
|
4
|
+
data.tar.gz: b300cb0a6f00afa06ea04fc01775397e4ec2f1ec123cd189e8509ce6e70e95df
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6ff6964a4bdcf7b80e2a2d8617c807b510e5a3a28c14c3e7f2c303404b04d222fef41626e6094f6b1c40da6d2a269d053261be83b2ca2e21f24944149ef935fb
|
7
|
+
data.tar.gz: ad7d512aa35ca94df9d87c3c9beb3d2be15c6ccd8f486cc4d2991ec28955a22ea15bfd73d8f3d299d44230aee16ccf06a9fa6ea43f267b1a10cddbeb32453ae8
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# WeatherPup
|
2
2
|
|
3
|
-
Welcome to WeatherPup! WeatherPup is a CLI written in Ruby that fetches current weather information based on a user entered US Zip Code or a GPS Coordinate Pair (Latitude and Longitude)
|
3
|
+
Welcome to WeatherPup! WeatherPup is a CLI written in Ruby that fetches current weather information based on a user entered US Zip Code or a GPS Coordinate Pair (Latitude and Longitude). Previously fetched weather conditions can also be viewed.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -30,9 +30,9 @@ After running the `weatherpup` executable from the `bin` folder, you'll be prese
|
|
30
30
|
|
31
31
|
Type in `1` to get the current weather conditions by US Zip Code. After doing this, you'll be asked to type in the 5 digit zip code. You'll then be shown the current weather for that zip code. From there you'll have the option to go `back` to the main menu.
|
32
32
|
|
33
|
-
Type in `2` to get the current weather conditions by GPS Coordinates (Latitude and Longitude pair). After doing this, you'll be asked to type in the
|
33
|
+
Type in `2` to get the current weather conditions by GPS Coordinates (Latitude and Longitude pair). After doing this, you'll be asked to type in the _latitude_ in decimal format. Example Latitude in decimal format: `40.705204`
|
34
34
|
|
35
|
-
You'll then be asked to type in the
|
35
|
+
You'll then be asked to type in the _longitude_ in decimal format. Example Longitude in decimal format: `-74.013845`
|
36
36
|
|
37
37
|
Then you'll be shown the current weather for that GPS Coordinate pair. From there you'll have the option to go `back` to the main menu.
|
38
38
|
|
data/lib/weatherpup.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#This is acting as my environment file.
|
1
|
+
#This is acting as my environment file. Leaving it called "weatherpup.rb" per rubygem standards.
|
2
2
|
require_relative "./weatherpup/version"
|
3
3
|
require_relative "./weatherpup/cli"
|
4
4
|
require_relative "./weatherpup/weather_conditions"
|
@@ -6,9 +6,10 @@ require_relative "./weatherpup/weather_conditions"
|
|
6
6
|
require 'httparty'
|
7
7
|
require 'colorize'
|
8
8
|
require 'yaml'
|
9
|
-
require 'pry'
|
10
9
|
|
10
|
+
#Read in a YAML file that contains all the valid US zip codes -- provided by www.aggdata.com
|
11
11
|
VALID_US_ZIP_CODES = YAML.load(File.read("./lib/weatherpup/valid_us_zip_codes.yml"))
|
12
|
+
|
12
13
|
APPID = "e4ece4d3cbcfdd05d69889c6753d57da"
|
13
14
|
|
14
15
|
#These are the ranges that will allow me to figure out the direction indicator is when I get back information from the weather API for the wind direction in degrees
|
data/lib/weatherpup/cli.rb
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
# CLI Controller
|
2
2
|
class WeatherPup::CLI
|
3
3
|
def call
|
4
|
+
#Clears the screen to make the interface look nicer / cleaner
|
4
5
|
system "clear"
|
5
|
-
|
6
|
+
|
7
|
+
#Welcome the user
|
6
8
|
puts <<~WELCOME
|
7
9
|
▒█░░▒█ █▀▀ █░░ █▀▀ █▀▀█ █▀▄▀█ █▀▀ ▀▀█▀▀ █▀▀█
|
8
10
|
▒█▒█▒█ █▀▀ █░░ █░░ █░░█ █░▀░█ █▀▀ ░▒█░░ █░░█
|
@@ -18,12 +20,16 @@ class WeatherPup::CLI
|
|
18
20
|
░▄▀░██▀██▀▀▀▀▀██▀▀▄
|
19
21
|
░░░░█▄░▀█▄░░░░▀█▄▀▀
|
20
22
|
WELCOME
|
21
|
-
|
23
|
+
#Run the main menu
|
24
|
+
self.main_menu
|
22
25
|
end
|
23
26
|
|
24
27
|
def main_menu
|
25
|
-
|
28
|
+
#Initialize my trigger variable "input" for my until loop
|
29
|
+
input = "no input yet"
|
26
30
|
until input.downcase == 'exit'
|
31
|
+
|
32
|
+
#Show the user their options and provide instructions on how to use.
|
27
33
|
puts <<~MAINMENU
|
28
34
|
|
29
35
|
#{"How would you like me to fetch the current weather conditions for you today?".colorize(:light_red)}
|
@@ -34,8 +40,11 @@ class WeatherPup::CLI
|
|
34
40
|
|
35
41
|
Please type #{"1".colorize(:green)}, #{"2".colorize(:light_blue)}, #{"3".colorize(:cyan)} or type #{"exit".colorize(:red)} to quit.
|
36
42
|
MAINMENU
|
43
|
+
|
44
|
+
#get input from the user
|
37
45
|
input = gets.chomp.downcase
|
38
46
|
|
47
|
+
#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.
|
39
48
|
case input
|
40
49
|
when "1"
|
41
50
|
self.fetch_by_zip
|
@@ -49,20 +58,21 @@ class WeatherPup::CLI
|
|
49
58
|
puts "\nSorry, that isn’t a valid input.".colorize(:red)
|
50
59
|
end
|
51
60
|
end
|
61
|
+
|
62
|
+
#Once the user types exit, clear the screen and then display the goodbye message
|
52
63
|
system "clear"
|
53
64
|
self.goodbye
|
54
65
|
end
|
55
66
|
|
56
|
-
#
|
67
|
+
#fetches the current conditions via zip code input from the user
|
57
68
|
def fetch_by_zip
|
58
|
-
#
|
59
|
-
#zip_code = nil
|
69
|
+
#initialize my trigger variable zip_code_valid for my until loop
|
60
70
|
zip_code_valid = nil
|
61
71
|
|
62
72
|
#Clear my screen first to make things look nicer
|
63
73
|
system "clear"
|
64
74
|
|
65
|
-
#Ask user for the zip code
|
75
|
+
#Ask user for the zip code until we get a valid input.
|
66
76
|
puts "\nOk, Current Weather Conditions by Zip Code!".colorize(:green)
|
67
77
|
until zip_code_valid
|
68
78
|
puts "\nPlease enter in the 5 Digit Zip Code:".colorize(:green)
|
@@ -74,25 +84,25 @@ class WeatherPup::CLI
|
|
74
84
|
break
|
75
85
|
end
|
76
86
|
|
77
|
-
#Otherwise, check to see if what the user put in was valid
|
87
|
+
#Otherwise, run #zip_code_valid to check to see if what the user put in was valid
|
78
88
|
zip_code_valid = zip_code_valid?(zip_code)
|
79
89
|
|
80
|
-
#If the zipcode input is valid,
|
90
|
+
#If the zipcode input is valid, get and display the weather info.
|
81
91
|
if zip_code_valid
|
92
|
+
#clear the screen
|
82
93
|
system "clear"
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
api_processed_data_hash = zip_weather_conditions.zip_process_api_data_to_attribs_hash(api_raw_data)
|
87
|
-
|
88
|
-
#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, then it prints the information located in the object itself.
|
89
|
-
zip_weather_conditions.tap {|weather_conditions_obj| weather_conditions_obj.write_attributes(api_processed_data_hash)}.print_zip_conditions
|
94
|
+
|
95
|
+
#Do the actual work of getting the data from the API, processing it, and printing it to screen
|
96
|
+
self.get_and_print_by_zip(zip_code)
|
90
97
|
|
91
98
|
#Next I wait for the user to type in "back" to return to the main menu
|
92
|
-
return_to_main_menu_prompt
|
99
|
+
self.return_to_main_menu_prompt
|
100
|
+
|
101
|
+
#Once the user types back, clear the screen and break out of this until loop
|
93
102
|
system "clear"
|
94
103
|
break
|
95
104
|
else
|
105
|
+
#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.
|
96
106
|
puts <<~INVALID_ZIP
|
97
107
|
\n#{"Invalid zip code detected!".colorize(:light_red)}
|
98
108
|
|
@@ -101,62 +111,77 @@ class WeatherPup::CLI
|
|
101
111
|
end
|
102
112
|
end
|
103
113
|
end
|
104
|
-
|
105
|
-
def return_to_main_menu_prompt
|
106
|
-
return_to_main = ""
|
107
|
-
until return_to_main.downcase == "back"
|
108
|
-
puts "\nType #{"back".colorize(:red)} to return to the main menu."
|
109
|
-
return_to_main = gets.chomp
|
110
|
-
end
|
111
|
-
end
|
112
114
|
|
113
|
-
|
115
|
+
#Input validation method - Checks the VALID_US_ZIP_CODES constant to see if the zip code is real
|
114
116
|
def zip_code_valid?(zip_to_check)
|
115
|
-
#checks the VALID_US_ZIP_CODES constant to see if the zip code is real
|
116
117
|
VALID_US_ZIP_CODES.include?(zip_to_check)
|
118
|
+
end
|
119
|
+
|
120
|
+
def get_and_print_by_zip(zip_code)
|
121
|
+
#Instantiate a new instance of WeatherConditions class
|
122
|
+
zip_weather_conditions = WeatherPup::WeatherConditions.new
|
123
|
+
|
124
|
+
#set the zip_code attribute to the zip code entered by the user
|
125
|
+
zip_weather_conditions.zip_code = zip_code
|
126
|
+
|
127
|
+
#go fetch the raw api data
|
128
|
+
api_raw_data = zip_weather_conditions.zip_api_fetch(zip_code)
|
129
|
+
|
130
|
+
#process raw data into an attributes hash to use with mass assignment method #write_attributes
|
131
|
+
api_processed_data_hash = zip_weather_conditions.zip_process_api_data_to_attribs_hash(api_raw_data)
|
132
|
+
|
133
|
+
#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.
|
134
|
+
zip_weather_conditions.tap {|weather_conditions_obj| weather_conditions_obj.write_attributes(api_processed_data_hash)}.print_zip_conditions
|
117
135
|
end
|
118
136
|
|
137
|
+
|
119
138
|
#fetch the current conditions via zip code input from the user
|
120
139
|
def fetch_by_gps
|
121
|
-
|
122
|
-
longitude = nil
|
140
|
+
#initialize my trigger variable valid_coordinates for my until loop
|
123
141
|
valid_coordinates = nil
|
142
|
+
|
143
|
+
#Clear my screen first to make things look nicer
|
124
144
|
system "clear"
|
125
|
-
|
145
|
+
|
146
|
+
#Ask user for the latitude and longitude until we get a valid input.
|
147
|
+
puts "\nOk, Current Weather Conditions by GPS coordinates!".colorize(:light_blue)
|
126
148
|
|
127
149
|
until valid_coordinates
|
128
150
|
#Grab the Lat from the user
|
129
|
-
puts "\nPlease enter in the ".colorize(:
|
151
|
+
puts "\nPlease enter in the ".colorize(:light_blue) + "latitude".colorize(:green).underline + " in decimal notation:".colorize(:light_blue)
|
130
152
|
latitude = gets.chomp
|
131
153
|
|
132
|
-
#Checking to see if the user wants out of this loop and back to the main menu
|
154
|
+
#Checking to see if the user wants out of this loop and back to the main menu before proceeding
|
133
155
|
if latitude.downcase == "back"
|
134
156
|
system "clear"
|
135
157
|
break
|
136
158
|
end
|
137
159
|
|
138
|
-
#Grab the
|
139
|
-
puts "\nPlease enter in the ".colorize(:
|
160
|
+
#Grab the Longitude from the user
|
161
|
+
puts "\nPlease enter in the ".colorize(:light_blue) + "longitude".colorize(:magenta).underline + " in decimal notation:".colorize(:light_blue)
|
140
162
|
longitude = gets.chomp
|
163
|
+
|
141
164
|
#Checking to see if the user wants out of this loop and back to the main menu
|
142
165
|
if longitude.downcase == "back"
|
143
166
|
system "clear"
|
144
167
|
break
|
145
168
|
end
|
146
169
|
|
147
|
-
#Checking to see if the coordinates are valid using
|
170
|
+
#Checking to see if the coordinates are valid using method #valid_coordinate_pair?
|
148
171
|
valid_coordinates = self.valid_coordinate_pair?(latitude, longitude)
|
149
172
|
|
150
|
-
# If the coordinates are valid,
|
173
|
+
# If the coordinates are valid, get and display the weather info.
|
151
174
|
if valid_coordinates
|
175
|
+
#Clear the screen to make it look nicer
|
152
176
|
system "clear"
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
#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.
|
157
|
-
gps_weather_conditions.tap {|weather_conditions_obj| weather_conditions_obj.write_attributes(api_processed_data_hash)}.print_gps_conditions
|
177
|
+
|
178
|
+
#Do the actual work of getting the data from the API, processing it, and printing it to screen
|
179
|
+
self.get_and_print_by_gps(latitude, longitude)
|
158
180
|
|
159
|
-
|
181
|
+
#Next I wait for the user to type in "back" to return to the main menu
|
182
|
+
self.return_to_main_menu_prompt
|
183
|
+
|
184
|
+
#Once the user types back, clear the screen and break out of this until loop
|
160
185
|
system "clear"
|
161
186
|
break
|
162
187
|
else
|
@@ -181,44 +206,62 @@ class WeatherPup::CLI
|
|
181
206
|
#returns true if the lat and long coordinate pair is valid.
|
182
207
|
end
|
183
208
|
|
209
|
+
def get_and_print_by_gps(latitude, longitude)
|
210
|
+
#Instantiate a new instance of WeatherConditions class
|
211
|
+
gps_weather_conditions = WeatherPup::WeatherConditions.new
|
212
|
+
|
213
|
+
#go fetch the raw api data
|
214
|
+
api_raw_data = gps_weather_conditions.gps_api_fetch(latitude, longitude)
|
215
|
+
|
216
|
+
#process raw data into an attributes hash to use with mass assignment method #write_attributes
|
217
|
+
api_processed_data_hash = gps_weather_conditions.gps_process_api_data_to_attribs_hash(api_raw_data)
|
218
|
+
#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.
|
219
|
+
gps_weather_conditions.tap {|weather_conditions_obj| weather_conditions_obj.write_attributes(api_processed_data_hash)}.print_gps_conditions
|
220
|
+
end
|
221
|
+
|
184
222
|
def fetch_previous
|
223
|
+
#Clear the screen to make it look nicer.
|
185
224
|
system "clear"
|
225
|
+
|
226
|
+
#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.
|
186
227
|
if WeatherPup::WeatherConditions.all == []
|
187
228
|
system "clear"
|
188
229
|
puts "\nThere are no previous fetches to display!".colorize(:cyan)
|
189
|
-
return_to_main_menu_prompt
|
230
|
+
self.return_to_main_menu_prompt
|
190
231
|
system "clear"
|
191
232
|
else
|
192
233
|
#puts a blank line to give some headroom when displaying
|
193
234
|
puts "\n"
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
when "Zip Code"
|
198
|
-
puts "#{index}. Weather by Zip Code: #{wc_obj.zip_code.colorize(:green)} (#{wc_obj.city_name.colorize(:green)}) fetched at #{wc_obj.when_fetched.colorize(:red)}"
|
199
|
-
when "GPS Coordinates"
|
200
|
-
puts "#{index}. Weather by GPS: #{wc_obj.lat.colorize(:light_blue)}, #{wc_obj.long.colorize(:light_blue)} (#{wc_obj.city_name.colorize(:light_blue)}) fetched at #{wc_obj.when_fetched.colorize(:red)}"
|
201
|
-
end
|
202
|
-
end
|
235
|
+
|
236
|
+
#ask the WeatherConditions class to print out a list of all its instances
|
237
|
+
WeatherPup::WeatherConditions.list_all_previous
|
203
238
|
|
239
|
+
#set my valid input range between 1 instance and however many instances of WeatherConditions there are in WeatherConditions @@all array.
|
204
240
|
valid_input_range = 1..WeatherPup::WeatherConditions.all.length
|
241
|
+
|
242
|
+
#Initialize my trigger to exit my until loop
|
205
243
|
valid_input = nil
|
206
244
|
|
245
|
+
#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.
|
207
246
|
until valid_input
|
208
247
|
puts <<~USER_PROMPT
|
209
248
|
\nPlease type in the #{"number".colorize(:cyan)} of the previous fetch you would like to view
|
210
249
|
or type #{"back".colorize(:red)} to return to the main menu.
|
211
250
|
USER_PROMPT
|
212
|
-
|
251
|
+
|
252
|
+
#get the input fro mthe user
|
253
|
+
user_selection = gets.chomp.downcase
|
213
254
|
|
214
|
-
if user_selection
|
255
|
+
if user_selection == "back"
|
215
256
|
system "clear"
|
216
257
|
break
|
217
258
|
end
|
218
259
|
|
260
|
+
#convert my user input to an integer so that I can check to see if its in the valid range.
|
219
261
|
user_integer = user_selection.to_i
|
220
262
|
valid_input = valid_input_range.member?(user_integer)
|
221
263
|
|
264
|
+
#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.
|
222
265
|
if valid_input
|
223
266
|
selected_wc_obj = WeatherPup::WeatherConditions.all[user_integer - 1]
|
224
267
|
type = selected_wc_obj.current_conditions_means
|
@@ -227,13 +270,13 @@ class WeatherPup::CLI
|
|
227
270
|
when "Zip Code"
|
228
271
|
system "clear"
|
229
272
|
selected_wc_obj.print_zip_conditions
|
230
|
-
return_to_main_menu_prompt
|
273
|
+
self.return_to_main_menu_prompt
|
231
274
|
system "clear"
|
232
275
|
break
|
233
276
|
when "GPS Coordinates"
|
234
277
|
system "clear"
|
235
278
|
selected_wc_obj.print_gps_conditions
|
236
|
-
return_to_main_menu_prompt
|
279
|
+
self.return_to_main_menu_prompt
|
237
280
|
system "clear"
|
238
281
|
break
|
239
282
|
end
|
@@ -243,7 +286,18 @@ class WeatherPup::CLI
|
|
243
286
|
end
|
244
287
|
end
|
245
288
|
end
|
289
|
+
|
246
290
|
|
291
|
+
|
292
|
+
#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
|
+
def return_to_main_menu_prompt
|
294
|
+
return_to_main = ""
|
295
|
+
until return_to_main.downcase == "back"
|
296
|
+
puts "\nType #{"back".colorize(:red)} to return to the main menu."
|
297
|
+
return_to_main = gets.chomp
|
298
|
+
end
|
299
|
+
end
|
300
|
+
|
247
301
|
#Says thank you and goodbye to user.
|
248
302
|
def goodbye
|
249
303
|
system "clear"
|
data/lib/weatherpup/version.rb
CHANGED
@@ -3,32 +3,35 @@ class WeatherPup::WeatherConditions
|
|
3
3
|
:temperature, :pressure, :humidity, :current_weather_description, :pressure,
|
4
4
|
:wind_speed, :wind_direction_indicator, :wind_direction_indicator_string, :reading_date_and_time,
|
5
5
|
:city_name, :when_fetched
|
6
|
-
#With this class I should be able to create a WeatherConditions instance by Zip code or GPS coordinates
|
6
|
+
#With this class I should be able to create a WeatherConditions instance by Zip code or GPS coordinates. Also I should be able to list out all the previous WeatherConditions instances that I have.
|
7
7
|
|
8
|
+
#WeatherConditions instance tracker array class variable
|
8
9
|
@@all = []
|
9
10
|
|
11
|
+
#Adds the current WeatherConditions instance to the @@all instance tracker
|
10
12
|
def initialize
|
11
13
|
@@all << self
|
12
14
|
end
|
13
15
|
|
16
|
+
#Class method used to read the all the instances of WeatherConditions in existence.
|
14
17
|
def self.all
|
15
18
|
@@all
|
16
19
|
end
|
17
20
|
|
21
|
+
#Go actually hit the OpenWeatherMap Api and then return all the information
|
18
22
|
def zip_api_fetch(zip_code, country_code = "us")
|
19
23
|
#this will actually hit the OpenWeatherMap Api
|
20
24
|
#I have defaulted the country code to US for now, but have built the GET request to be flexible to add multi-country postal codes later
|
21
25
|
HTTParty.get("https://api.openweathermap.org/data/2.5/weather?zip=#{zip_code},#{country_code}&APPID=#{APPID}&units=imperial")
|
22
26
|
end
|
23
27
|
|
24
|
-
|
25
|
-
|
26
|
-
#
|
27
|
-
api_info = api_data
|
28
|
+
#Process the raw API data that was grabbed by #zip_api_fetch and and output a hash that will be used by #write_attributes to mass assign WeatherConditions instance attributes
|
29
|
+
def zip_process_api_data_to_attribs_hash(api_info)
|
30
|
+
#massage the wind_direction information before writing it to the attributes hash.
|
28
31
|
wind_direction_indicator_deg = api_info["wind"]["deg"].round
|
29
32
|
wind_direction_indicator_string = wind_direction_indicator_to_text(wind_direction_indicator_deg)
|
30
33
|
|
31
|
-
#creates
|
34
|
+
#creates attributes hash for mass assignment to be used by the #write_attributes method
|
32
35
|
{
|
33
36
|
:current_weather_description => api_info["weather"][0]["main"],
|
34
37
|
#I'm leaving :current_condtions_means in here in case I want to combine my print_gps and print_zip methods, otherwise, it's not needed at the moment
|
@@ -45,18 +48,16 @@ class WeatherPup::WeatherConditions
|
|
45
48
|
}
|
46
49
|
end
|
47
50
|
|
51
|
+
#Go actually hit the OpenWeatherMap Api and then return all the information
|
48
52
|
def gps_api_fetch(latitude, longitude)
|
49
53
|
|
50
|
-
#I think I want to split this out into two methods -- #gps_api_data_fetch which will just do the fetch based on the Lat, Longs given to it. Then have another method, #create_api_data_hash that will do the rest of this in another method. The #create_api_data_hash method will need to be run in the #new_by_gps method and the create_api_data_hash method will call the #gps_apa_data_fetch method and save that into a variable called api_info
|
51
|
-
|
52
|
-
#this will actually hit the OpenWeatherMap Api and then return all the information
|
53
54
|
HTTParty.get("https://api.openweathermap.org/data/2.5/weather?lat=#{latitude}&lon=#{longitude}&APPID=#{APPID}&units=imperial")
|
54
55
|
end
|
55
56
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
57
|
+
#Process the raw API data that was grabbed by #gps_api_fetch and and output a hash that will be used by #write_attributes to mass assign WeatherConditions instance attributes
|
58
|
+
def gps_process_api_data_to_attribs_hash(api_info)
|
59
|
+
#the data from the API for Wind direction for a GPS check is not as reliable and sometimes is missing.
|
60
|
+
#Check to see if the data is there first, then write it out the variables used in the hash
|
60
61
|
wind_direction_indicator_deg = api_info["wind"]["deg"]
|
61
62
|
if wind_direction_indicator_deg.nil?
|
62
63
|
wind_direction_indicator_deg = "No Data"
|
@@ -66,9 +67,8 @@ class WeatherPup::WeatherConditions
|
|
66
67
|
wind_direction_indicator_string = wind_direction_indicator_to_text(wind_direction_indicator_deg_rounded)
|
67
68
|
wind_direction_indicator_deg = wind_direction_indicator_deg.round.to_s + "°"
|
68
69
|
end
|
69
|
-
#NOTE: REFACTOR Needed / Bug -- if you do another city I need to localize the time to that location's time zone - especially for sunrise and sunset
|
70
70
|
|
71
|
-
#creates
|
71
|
+
#creates attributes hash for mass assignment to be used by the #write_attributes method
|
72
72
|
{
|
73
73
|
:current_weather_description => api_info["weather"][0]["main"],
|
74
74
|
#I'm leaving :current_condtions_means in here in case I want to combine my print_gps and print_zip methods, otherwise, it's not needed at the moment
|
@@ -87,13 +87,14 @@ class WeatherPup::WeatherConditions
|
|
87
87
|
}
|
88
88
|
end
|
89
89
|
|
90
|
+
#Writes attributes using mass assignment to the WeatherConditions object it is called on when given a processed api hash as an arg.
|
90
91
|
def write_attributes(processed_api_data_hash)
|
91
|
-
#this will take whatever the API hash was and create attributes for the object that it's run on.
|
92
92
|
processed_api_data_hash.map do |key, value|
|
93
93
|
self.send("#{key}=", value)
|
94
94
|
end
|
95
95
|
end
|
96
96
|
|
97
|
+
#Prints Zip Conditions to screen from a WeatherConditions Object.
|
97
98
|
def print_zip_conditions
|
98
99
|
puts <<~CONDITIONS
|
99
100
|
\nThe weather conditions for #{self.city_name.colorize(:green).underline} (#{self.zip_code.colorize(:green)}):
|
@@ -114,6 +115,7 @@ class WeatherPup::WeatherConditions
|
|
114
115
|
CONDITIONS
|
115
116
|
end
|
116
117
|
|
118
|
+
#Prints GPS conditions to screen from a WeatherConditions object.
|
117
119
|
def print_gps_conditions
|
118
120
|
puts <<~CONDITIONS
|
119
121
|
\nThe weather conditions for #{self.lat.colorize(:green)}, #{self.long.colorize(:green)} (#{self.city_name.colorize(:green)}):
|
@@ -132,9 +134,9 @@ class WeatherPup::WeatherConditions
|
|
132
134
|
**Weather Data provided by OpenWeatherMap.org**
|
133
135
|
CONDITIONS
|
134
136
|
end
|
135
|
-
|
137
|
+
|
138
|
+
#This method figures out what the direction indicator text should when given a numeric wind direction in degrees
|
136
139
|
def wind_direction_indicator_to_text(wind_direction_in_degrees)
|
137
|
-
#This code may be a little smelly. I may be able to do this by iterating over a hash of the constants and check wind_direction_in_degrees agains the value of each then return the key. Meh... not sure.
|
138
140
|
indicator = nil
|
139
141
|
if NORTH_RANGE_PART1.member?(wind_direction_in_degrees) || NORTH_RANGE_PART2.member?(wind_direction_in_degrees)
|
140
142
|
indicator = "N"
|
@@ -155,4 +157,16 @@ class WeatherPup::WeatherConditions
|
|
155
157
|
end
|
156
158
|
indicator
|
157
159
|
end
|
160
|
+
|
161
|
+
#This method prints a list out all the instances of WeatherConditions to screen
|
162
|
+
def self.list_all_previous
|
163
|
+
self.all.each.with_index(1) do |wc_obj, index|
|
164
|
+
case wc_obj.current_conditions_means
|
165
|
+
when "Zip Code"
|
166
|
+
puts "#{index}. Weather by Zip Code: #{wc_obj.zip_code.colorize(:green)} (#{wc_obj.city_name.colorize(:green)}) fetched at #{wc_obj.when_fetched.colorize(:red)}"
|
167
|
+
when "GPS Coordinates"
|
168
|
+
puts "#{index}. Weather by GPS: #{wc_obj.lat.colorize(:light_blue)}, #{wc_obj.long.colorize(:light_blue)} (#{wc_obj.city_name.colorize(:light_blue)}) fetched at #{wc_obj.when_fetched.colorize(:red)}"
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|
158
172
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: weatherpup
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeremiah Rodden
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-07-
|
11
|
+
date: 2019-07-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|