class WeatherPup::WeatherConditions

Attributes

city_name[RW]
current_conditions_means[RW]
current_weather_description[RW]
humidity[RW]
lat[RW]
long[RW]
pressure[RW]
reading_date_and_time[RW]
temperature[RW]
when_fetched[RW]
wind_direction_indicator[RW]
wind_direction_indicator_string[RW]
wind_speed[RW]
zip_code[RW]

Public Class Methods

all() click to toggle source

Class method used to read the all the instances of WeatherConditions in existence.

# File lib/weatherpup/weather_conditions.rb, line 17
def self.all
   @@all
end
list_all_previous() click to toggle source

This method prints a list out all the instances of WeatherConditions to screen

# 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
new() click to toggle source

Adds the current WeatherConditions instance to the @@all instance tracker

# File lib/weatherpup/weather_conditions.rb, line 12
def initialize
   @@all << self
end

Public Instance Methods

gps_api_fetch(latitude, longitude) click to toggle source

Go actually hit the OpenWeatherMap Api and then return all the information

# 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) click to toggle source

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

# 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() click to toggle source

Prints GPS conditions to screen from a WeatherConditions object.

print_zip_conditions() click to toggle source

Prints Zip Conditions to screen from a WeatherConditions Object.

wind_direction_indicator_to_text(wind_direction_in_degrees) click to toggle source

This method figures out what the direction indicator text should when given a numeric wind direction in degrees

# 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) click to toggle source

Writes attributes using mass assignment to the WeatherConditions object it is called on when given a processed api hash as an arg.

# 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") click to toggle source

Go actually hit the OpenWeatherMap Api and then return all the information

# 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) click to toggle source

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

# 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