Class: WeatherPup::WeatherConditions
- Inherits:
-
Object
- Object
- WeatherPup::WeatherConditions
- Defined in:
- lib/weatherpup/weather_conditions.rb
Constant Summary collapse
- @@all =
WeatherConditions instance tracker array class variable
[]
Instance Attribute Summary collapse
-
#city_name ⇒ Object
Returns the value of attribute city_name.
-
#current_conditions_means ⇒ Object
Returns the value of attribute current_conditions_means.
-
#current_weather_description ⇒ Object
Returns the value of attribute current_weather_description.
-
#humidity ⇒ Object
Returns the value of attribute humidity.
-
#lat ⇒ Object
Returns the value of attribute lat.
-
#long ⇒ Object
Returns the value of attribute long.
-
#pressure ⇒ Object
Returns the value of attribute pressure.
-
#reading_date_and_time ⇒ Object
Returns the value of attribute reading_date_and_time.
-
#temperature ⇒ Object
Returns the value of attribute temperature.
-
#when_fetched ⇒ Object
Returns the value of attribute when_fetched.
-
#wind_direction_indicator ⇒ Object
Returns the value of attribute wind_direction_indicator.
-
#wind_direction_indicator_string ⇒ Object
Returns the value of attribute wind_direction_indicator_string.
-
#wind_speed ⇒ Object
Returns the value of attribute wind_speed.
-
#zip_code ⇒ Object
Returns the value of attribute zip_code.
Class Method Summary collapse
-
.all ⇒ Object
Class method used to read the all the instances of WeatherConditions in existence.
-
.list_all_previous ⇒ Object
This method prints a list out all the instances of WeatherConditions to screen.
Instance Method Summary collapse
-
#gps_api_fetch(latitude, longitude) ⇒ Object
Go actually hit the OpenWeatherMap Api and then return all the information.
-
#gps_process_api_data_to_attribs_hash(api_info) ⇒ Object
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.
-
#initialize ⇒ WeatherConditions
constructor
Adds the current WeatherConditions instance to the @@all instance tracker.
-
#print_gps_conditions ⇒ Object
Prints GPS conditions to screen from a WeatherConditions object.
-
#print_zip_conditions ⇒ Object
Prints Zip Conditions to screen from a WeatherConditions Object.
-
#wind_direction_indicator_to_text(wind_direction_in_degrees) ⇒ Object
This method figures out what the direction indicator text should when given a numeric wind direction in degrees.
-
#write_attributes(processed_api_data_hash) ⇒ Object
Writes attributes using mass assignment to the WeatherConditions object it is called on when given a processed api hash as an arg.
-
#zip_api_fetch(zip_code, country_code = "us") ⇒ Object
Go actually hit the OpenWeatherMap Api and then return all the information.
-
#zip_process_api_data_to_attribs_hash(api_info) ⇒ Object
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.
Constructor Details
#initialize ⇒ WeatherConditions
Adds the current WeatherConditions instance to the @@all instance tracker
12 13 14 |
# File 'lib/weatherpup/weather_conditions.rb', line 12 def initialize @@all << self end |
Instance Attribute Details
#city_name ⇒ Object
Returns the value of attribute city_name.
2 3 4 |
# File 'lib/weatherpup/weather_conditions.rb', line 2 def city_name @city_name end |
#current_conditions_means ⇒ Object
Returns the value of attribute current_conditions_means.
2 3 4 |
# File 'lib/weatherpup/weather_conditions.rb', line 2 def current_conditions_means @current_conditions_means end |
#current_weather_description ⇒ Object
Returns the value of attribute current_weather_description.
2 3 4 |
# File 'lib/weatherpup/weather_conditions.rb', line 2 def current_weather_description @current_weather_description end |
#humidity ⇒ Object
Returns the value of attribute humidity.
2 3 4 |
# File 'lib/weatherpup/weather_conditions.rb', line 2 def humidity @humidity end |
#lat ⇒ Object
Returns the value of attribute lat.
2 3 4 |
# File 'lib/weatherpup/weather_conditions.rb', line 2 def lat @lat end |
#long ⇒ Object
Returns the value of attribute long.
2 3 4 |
# File 'lib/weatherpup/weather_conditions.rb', line 2 def long @long end |
#pressure ⇒ Object
Returns the value of attribute pressure.
2 3 4 |
# File 'lib/weatherpup/weather_conditions.rb', line 2 def pressure @pressure end |
#reading_date_and_time ⇒ Object
Returns the value of attribute reading_date_and_time.
2 3 4 |
# File 'lib/weatherpup/weather_conditions.rb', line 2 def reading_date_and_time @reading_date_and_time end |
#temperature ⇒ Object
Returns the value of attribute temperature.
2 3 4 |
# File 'lib/weatherpup/weather_conditions.rb', line 2 def temperature @temperature end |
#when_fetched ⇒ Object
Returns the value of attribute when_fetched.
2 3 4 |
# File 'lib/weatherpup/weather_conditions.rb', line 2 def when_fetched @when_fetched end |
#wind_direction_indicator ⇒ Object
Returns the value of attribute wind_direction_indicator.
2 3 4 |
# File 'lib/weatherpup/weather_conditions.rb', line 2 def wind_direction_indicator @wind_direction_indicator end |
#wind_direction_indicator_string ⇒ Object
Returns the value of attribute wind_direction_indicator_string.
2 3 4 |
# File 'lib/weatherpup/weather_conditions.rb', line 2 def wind_direction_indicator_string @wind_direction_indicator_string end |
#wind_speed ⇒ Object
Returns the value of attribute wind_speed.
2 3 4 |
# File 'lib/weatherpup/weather_conditions.rb', line 2 def wind_speed @wind_speed end |
#zip_code ⇒ Object
Returns the value of attribute zip_code.
2 3 4 |
# File 'lib/weatherpup/weather_conditions.rb', line 2 def zip_code @zip_code end |
Class Method Details
.all ⇒ Object
Class method used to read the all the instances of WeatherConditions in existence.
17 18 19 |
# File 'lib/weatherpup/weather_conditions.rb', line 17 def self.all @@all end |
.list_all_previous ⇒ Object
This method prints a list out all the instances of WeatherConditions to screen
162 163 164 165 166 167 168 169 170 171 |
# File 'lib/weatherpup/weather_conditions.rb', line 162 def self.list_all_previous self.all.each.with_index(1) do |wc_obj, index| case wc_obj.current_conditions_means when "Zip Code" 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)}" when "GPS Coordinates" 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)}" end end end |
Instance Method Details
#gps_api_fetch(latitude, longitude) ⇒ Object
Go actually hit the OpenWeatherMap Api and then return all the information
52 53 54 55 |
# File 'lib/weatherpup/weather_conditions.rb', line 52 def gps_api_fetch(latitude, longitude) HTTParty.get("https://api.openweathermap.org/data/2.5/weather?lat=#{latitude}&lon=#{longitude}&APPID=#{APPID}&units=imperial") end |
#gps_process_api_data_to_attribs_hash(api_info) ⇒ Object
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/weatherpup/weather_conditions.rb', line 58 def gps_process_api_data_to_attribs_hash(api_info) #the data from the API for Wind direction for a GPS check is not as reliable and sometimes is missing. #Check to see if the data is there first, then write it out the variables used in the hash wind_direction_indicator_deg = api_info["wind"]["deg"] if wind_direction_indicator_deg.nil? wind_direction_indicator_deg = "No Data" wind_direction_indicator_string = "" else wind_direction_indicator_deg_rounded = wind_direction_indicator_deg.round wind_direction_indicator_string = wind_direction_indicator_to_text(wind_direction_indicator_deg_rounded) wind_direction_indicator_deg = wind_direction_indicator_deg.round.to_s + "°" end #creates attributes hash for mass assignment to be used by the #write_attributes method { :current_weather_description => api_info["weather"][0]["main"], #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 :current_conditions_means => "GPS Coordinates", :lat => api_info["coord"]["lat"].to_s, :long => api_info["coord"]["lon"].to_s, :temperature => api_info["main"]["temp"].round.to_s, :humidity => api_info["main"]["humidity"].to_s, :pressure => (api_info["main"]["pressure"] * 0.0295300).to_s[0..4], :wind_speed => api_info["wind"]["speed"].round.to_s, :wind_direction_indicator => wind_direction_indicator_deg, :wind_direction_indicator_string => wind_direction_indicator_string, :reading_date_and_time => Time.at(api_info["dt"]).to_datetime.strftime("%a, %b %d, %Y at %I:%M%P UTC%:::z"), :city_name => api_info["name"], :when_fetched => Time.now.to_datetime.strftime("%I:%M:%S%P (%b %d)") } end |
#print_gps_conditions ⇒ Object
Prints GPS conditions to screen from a WeatherConditions object.
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/weatherpup/weather_conditions.rb', line 119 def print_gps_conditions puts <<~CONDITIONS \nThe weather conditions for #{self.lat.colorize(:green)}, #{self.long.colorize(:green)} (#{self.city_name.colorize(:green)}): #{self.temperature.colorize(:light_blue)}°F #{self.humidity.colorize(:light_blue)}% Humidity #{self.current_weather_description.colorize(:light_blue)} Pressure: #{self.pressure.colorize(:light_blue)} in of Hg Wind Speed: #{self.wind_speed.colorize(:light_blue)} MPH Wind Direction: #{self.wind_direction_indicator_string.colorize(:light_blue)} (#{self.wind_direction_indicator.colorize(:light_blue)}) This data is based on the weather station reading time of: #{self.reading_date_and_time.colorize(:green)} **Weather Data provided by OpenWeatherMap.org** CONDITIONS end |
#print_zip_conditions ⇒ Object
Prints Zip Conditions to screen from a WeatherConditions Object.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/weatherpup/weather_conditions.rb', line 98 def print_zip_conditions puts <<~CONDITIONS \nThe weather conditions for #{self.city_name.colorize(:green).underline} (#{self.zip_code.colorize(:green)}): #{self.temperature.colorize(:light_blue)}°F #{self.humidity.colorize(:light_blue)}% Humidity #{self.current_weather_description.colorize(:light_blue)} Pressure: #{self.pressure.colorize(:light_blue)} in of Hg Wind Speed: #{self.wind_speed.colorize(:light_blue)} MPH Wind Direction: #{self.wind_direction_indicator_string.colorize(:light_blue)} (#{self.wind_direction_indicator.to_s.colorize(:blue)}) This data is based on the weather station reading time of: #{self.reading_date_and_time.colorize(:green)} **Weather Data provided by OpenWeatherMap.org** **Zip Code data courtesy of AggData.com** CONDITIONS end |
#wind_direction_indicator_to_text(wind_direction_in_degrees) ⇒ Object
This method figures out what the direction indicator text should when given a numeric wind direction in degrees
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/weatherpup/weather_conditions.rb', line 139 def wind_direction_indicator_to_text(wind_direction_in_degrees) indicator = nil if NORTH_RANGE_PART1.member?(wind_direction_in_degrees) || NORTH_RANGE_PART2.member?(wind_direction_in_degrees) indicator = "N" elsif NORTHEAST_RANGE.member?(wind_direction_in_degrees) indicator = "NE" elsif EAST_RANGE.member?(wind_direction_in_degrees) indicator = "E" elsif SOUTHEAST_RANGE.member?(wind_direction_in_degrees) indicator = "SE" elsif SOUTH_RANGE.member?(wind_direction_in_degrees) indicator = "S" elsif SOUTHWEST_RANGE.member?(wind_direction_in_degrees) indicator = "SW" elsif WEST_RANGE.member?(wind_direction_in_degrees) indicator = "W" elsif NORTHWEST_RANGE.member?(wind_direction_in_degrees) indicator = "NW" end indicator end |
#write_attributes(processed_api_data_hash) ⇒ Object
Writes attributes using mass assignment to the WeatherConditions object it is called on when given a processed api hash as an arg.
91 92 93 94 95 |
# File 'lib/weatherpup/weather_conditions.rb', line 91 def write_attributes(processed_api_data_hash) processed_api_data_hash.map do |key, value| self.send("#{key}=", value) end end |
#zip_api_fetch(zip_code, country_code = "us") ⇒ Object
Go actually hit the OpenWeatherMap Api and then return all the information
22 23 24 25 26 |
# File 'lib/weatherpup/weather_conditions.rb', line 22 def zip_api_fetch(zip_code, country_code = "us") #this will actually hit the OpenWeatherMap Api #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 HTTParty.get("https://api.openweathermap.org/data/2.5/weather?zip=#{zip_code},#{country_code}&APPID=#{APPID}&units=imperial") end |
#zip_process_api_data_to_attribs_hash(api_info) ⇒ Object
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/weatherpup/weather_conditions.rb', line 29 def zip_process_api_data_to_attribs_hash(api_info) #massage the wind_direction information before writing it to the attributes hash. wind_direction_indicator_deg = api_info["wind"]["deg"].round wind_direction_indicator_string = wind_direction_indicator_to_text(wind_direction_indicator_deg) #creates attributes hash for mass assignment to be used by the #write_attributes method { :current_weather_description => api_info["weather"][0]["main"], #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 :current_conditions_means => "Zip Code", :temperature => api_info["main"]["temp"].round.to_s, :humidity => api_info["main"]["humidity"].to_s, :pressure => (api_info["main"]["pressure"] * 0.0295300).to_s[0..4], :wind_speed => api_info["wind"]["speed"].round.to_s, :wind_direction_indicator => wind_direction_indicator_deg.to_s + "°", :wind_direction_indicator_string => wind_direction_indicator_string, :reading_date_and_time => Time.at(api_info["dt"]).to_datetime.strftime("%a, %b %d, %Y at %I:%M%P UTC%:::z"), :city_name => api_info["name"], :when_fetched => Time.now.to_datetime.strftime("%I:%M:%S%P (%b %d)") } end |