Class: Weatherbot::API
Constant Summary collapse
- @@locations =
[]
Instance Attribute Summary collapse
-
#cloudiness ⇒ Object
Returns the value of attribute cloudiness.
-
#cloudiness24 ⇒ Object
Returns the value of attribute cloudiness24.
-
#cloudiness48 ⇒ Object
Returns the value of attribute cloudiness48.
-
#cloudiness72 ⇒ Object
Returns the value of attribute cloudiness72.
-
#condition ⇒ Object
Returns the value of attribute condition.
-
#condition24 ⇒ Object
Returns the value of attribute condition24.
-
#condition48 ⇒ Object
Returns the value of attribute condition48.
-
#condition72 ⇒ Object
Returns the value of attribute condition72.
-
#coordinates ⇒ Object
Returns the value of attribute coordinates.
-
#country ⇒ Object
Returns the value of attribute country.
-
#google_maps ⇒ Object
Returns the value of attribute google_maps.
-
#google_maps_link ⇒ Object
Returns the value of attribute google_maps_link.
-
#hr24_dt ⇒ Object
Returns the value of attribute hr24_dt.
-
#hr48_dt ⇒ Object
Returns the value of attribute hr48_dt.
-
#hr72_dt ⇒ Object
Returns the value of attribute hr72_dt.
-
#humidity ⇒ Object
Returns the value of attribute humidity.
-
#humidity24 ⇒ Object
Returns the value of attribute humidity24.
-
#humidity48 ⇒ Object
Returns the value of attribute humidity48.
-
#humidity72 ⇒ Object
Returns the value of attribute humidity72.
-
#location ⇒ Object
Returns the value of attribute location.
-
#location_name ⇒ Object
Returns the value of attribute location_name.
-
#report_time ⇒ Object
Returns the value of attribute report_time.
-
#response_code ⇒ Object
Returns the value of attribute response_code.
-
#sunrise ⇒ Object
Returns the value of attribute sunrise.
-
#sunset ⇒ Object
Returns the value of attribute sunset.
-
#temp24 ⇒ Object
Returns the value of attribute temp24.
-
#temp48 ⇒ Object
Returns the value of attribute temp48.
-
#temp72 ⇒ Object
Returns the value of attribute temp72.
-
#temp_avg ⇒ Object
Returns the value of attribute temp_avg.
-
#temp_celsius ⇒ Object
Returns the value of attribute temp_celsius.
-
#wind_direction ⇒ Object
Returns the value of attribute wind_direction.
-
#wind_direction24 ⇒ Object
Returns the value of attribute wind_direction24.
-
#wind_direction48 ⇒ Object
Returns the value of attribute wind_direction48.
-
#wind_direction72 ⇒ Object
Returns the value of attribute wind_direction72.
-
#wind_speed ⇒ Object
Returns the value of attribute wind_speed.
-
#wind_speed24 ⇒ Object
Returns the value of attribute wind_speed24.
-
#wind_speed48 ⇒ Object
Returns the value of attribute wind_speed48.
-
#wind_speed72 ⇒ Object
Returns the value of attribute wind_speed72.
Class Method Summary collapse
-
.current_weather(location) ⇒ Object
Takes user input to enter into URL query & gets current weather conditions in imperial units.
-
.forecast(location) ⇒ Object
Takes user input to enter into URL query for 3 day forecast in imperial units.
- .locations ⇒ Object
-
.open_link ⇒ Object
Method to open link in Google Maps depending on OS.
Instance Method Summary collapse
-
#initialize ⇒ API
constructor
A new instance of API.
Methods inherited from Helper
Constructor Details
#initialize ⇒ API
Returns a new instance of API.
7 8 9 10 |
# File 'lib/api.rb', line 7 def initialize @location = location @@locations << self end |
Instance Attribute Details
#cloudiness ⇒ Object
Returns the value of attribute cloudiness.
3 4 5 |
# File 'lib/api.rb', line 3 def cloudiness @cloudiness end |
#cloudiness24 ⇒ Object
Returns the value of attribute cloudiness24.
3 4 5 |
# File 'lib/api.rb', line 3 def cloudiness24 @cloudiness24 end |
#cloudiness48 ⇒ Object
Returns the value of attribute cloudiness48.
3 4 5 |
# File 'lib/api.rb', line 3 def cloudiness48 @cloudiness48 end |
#cloudiness72 ⇒ Object
Returns the value of attribute cloudiness72.
3 4 5 |
# File 'lib/api.rb', line 3 def cloudiness72 @cloudiness72 end |
#condition ⇒ Object
Returns the value of attribute condition.
3 4 5 |
# File 'lib/api.rb', line 3 def condition @condition end |
#condition24 ⇒ Object
Returns the value of attribute condition24.
3 4 5 |
# File 'lib/api.rb', line 3 def condition24 @condition24 end |
#condition48 ⇒ Object
Returns the value of attribute condition48.
3 4 5 |
# File 'lib/api.rb', line 3 def condition48 @condition48 end |
#condition72 ⇒ Object
Returns the value of attribute condition72.
3 4 5 |
# File 'lib/api.rb', line 3 def condition72 @condition72 end |
#coordinates ⇒ Object
Returns the value of attribute coordinates.
3 4 5 |
# File 'lib/api.rb', line 3 def coordinates @coordinates end |
#country ⇒ Object
Returns the value of attribute country.
3 4 5 |
# File 'lib/api.rb', line 3 def country @country end |
#google_maps ⇒ Object
Returns the value of attribute google_maps.
3 4 5 |
# File 'lib/api.rb', line 3 def google_maps @google_maps end |
#google_maps_link ⇒ Object
Returns the value of attribute google_maps_link.
3 4 5 |
# File 'lib/api.rb', line 3 def google_maps_link @google_maps_link end |
#hr24_dt ⇒ Object
Returns the value of attribute hr24_dt.
3 4 5 |
# File 'lib/api.rb', line 3 def hr24_dt @hr24_dt end |
#hr48_dt ⇒ Object
Returns the value of attribute hr48_dt.
3 4 5 |
# File 'lib/api.rb', line 3 def hr48_dt @hr48_dt end |
#hr72_dt ⇒ Object
Returns the value of attribute hr72_dt.
3 4 5 |
# File 'lib/api.rb', line 3 def hr72_dt @hr72_dt end |
#humidity ⇒ Object
Returns the value of attribute humidity.
3 4 5 |
# File 'lib/api.rb', line 3 def humidity @humidity end |
#humidity24 ⇒ Object
Returns the value of attribute humidity24.
3 4 5 |
# File 'lib/api.rb', line 3 def humidity24 @humidity24 end |
#humidity48 ⇒ Object
Returns the value of attribute humidity48.
3 4 5 |
# File 'lib/api.rb', line 3 def humidity48 @humidity48 end |
#humidity72 ⇒ Object
Returns the value of attribute humidity72.
3 4 5 |
# File 'lib/api.rb', line 3 def humidity72 @humidity72 end |
#location ⇒ Object
Returns the value of attribute location.
3 4 5 |
# File 'lib/api.rb', line 3 def location @location end |
#location_name ⇒ Object
Returns the value of attribute location_name.
3 4 5 |
# File 'lib/api.rb', line 3 def location_name @location_name end |
#report_time ⇒ Object
Returns the value of attribute report_time.
3 4 5 |
# File 'lib/api.rb', line 3 def report_time @report_time end |
#response_code ⇒ Object
Returns the value of attribute response_code.
3 4 5 |
# File 'lib/api.rb', line 3 def response_code @response_code end |
#sunrise ⇒ Object
Returns the value of attribute sunrise.
3 4 5 |
# File 'lib/api.rb', line 3 def sunrise @sunrise end |
#sunset ⇒ Object
Returns the value of attribute sunset.
3 4 5 |
# File 'lib/api.rb', line 3 def sunset @sunset end |
#temp24 ⇒ Object
Returns the value of attribute temp24.
3 4 5 |
# File 'lib/api.rb', line 3 def temp24 @temp24 end |
#temp48 ⇒ Object
Returns the value of attribute temp48.
3 4 5 |
# File 'lib/api.rb', line 3 def temp48 @temp48 end |
#temp72 ⇒ Object
Returns the value of attribute temp72.
3 4 5 |
# File 'lib/api.rb', line 3 def temp72 @temp72 end |
#temp_avg ⇒ Object
Returns the value of attribute temp_avg.
3 4 5 |
# File 'lib/api.rb', line 3 def temp_avg @temp_avg end |
#temp_celsius ⇒ Object
Returns the value of attribute temp_celsius.
3 4 5 |
# File 'lib/api.rb', line 3 def temp_celsius @temp_celsius end |
#wind_direction ⇒ Object
Returns the value of attribute wind_direction.
3 4 5 |
# File 'lib/api.rb', line 3 def wind_direction @wind_direction end |
#wind_direction24 ⇒ Object
Returns the value of attribute wind_direction24.
3 4 5 |
# File 'lib/api.rb', line 3 def wind_direction24 @wind_direction24 end |
#wind_direction48 ⇒ Object
Returns the value of attribute wind_direction48.
3 4 5 |
# File 'lib/api.rb', line 3 def wind_direction48 @wind_direction48 end |
#wind_direction72 ⇒ Object
Returns the value of attribute wind_direction72.
3 4 5 |
# File 'lib/api.rb', line 3 def wind_direction72 @wind_direction72 end |
#wind_speed ⇒ Object
Returns the value of attribute wind_speed.
3 4 5 |
# File 'lib/api.rb', line 3 def wind_speed @wind_speed end |
#wind_speed24 ⇒ Object
Returns the value of attribute wind_speed24.
3 4 5 |
# File 'lib/api.rb', line 3 def wind_speed24 @wind_speed24 end |
#wind_speed48 ⇒ Object
Returns the value of attribute wind_speed48.
3 4 5 |
# File 'lib/api.rb', line 3 def wind_speed48 @wind_speed48 end |
#wind_speed72 ⇒ Object
Returns the value of attribute wind_speed72.
3 4 5 |
# File 'lib/api.rb', line 3 def wind_speed72 @wind_speed72 end |
Class Method Details
.current_weather(location) ⇒ Object
Takes user input to enter into URL query & gets current weather conditions in imperial units
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/api.rb', line 17 def self.current_weather(location) # query sample: 'https://api.openweathermap.org/data/2.5/weather?q=new+york&appid=3207703ee5d0d14e6b6a53d10071018f&units=imperial' response = HTTParty.get("https://api.openweathermap.org/data/2.5/weather?q=#{location}&appid=3207703ee5d0d14e6b6a53d10071018f&units=imperial") parsed = response.parsed_response @current = self.new @current.response_code = parsed["cod"] # Check for invalid entry if @current.response_code === "404" puts "\n\nInvalid location, please enter a valid location.\n\n" return else # Assign attributes to current weather object @current.coordinates = parsed["coord"].values.reverse.join(", ") @current.location_name = parsed["name"] @current.report_time = Time.at(parsed["dt"]) @current.temp_avg = parsed["main"]["temp"] @current.condition = parsed["weather"].first["description"] @current.cloudiness = parsed["clouds"]["all"] # @current.pressure = parsed["main"]["pressure"] @current.humidity = parsed["main"]["humidity"] @current.wind_speed = parsed["wind"]["speed"] @current.sunrise = Time.at(parsed["sys"]["sunrise"]) @current.sunset = Time.at(parsed["sys"]["sunset"]) @current.wind_direction = degToCompass(parsed["wind"]["deg"]) @current.temp_celsius = toCelsius(parsed["main"]["temp"]) # Open query in browser to Google Maps @current.google_maps = "https://www.google.com/maps/place/#{@current.coordinates.gsub(" ", "")}" @google_maps_link = @current.google_maps # Check for odd locations with no country key if parsed.fetch("sys").has_key?("country") @current.country = parsed["sys"]["country"] else @current.country = nil end end @current end |
.forecast(location) ⇒ Object
Takes user input to enter into URL query for 3 day forecast in imperial units
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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/api.rb', line 63 def self.forecast(location) # query sample: https://api.openweathermap.org/data/2.5/forecast?q=new+york&appid=3207703ee5d0d14e6b6a53d10071018f&units=imperial response = HTTParty.get("https://api.openweathermap.org/data/2.5/forecast?q=#{location}&appid=3207703ee5d0d14e6b6a53d10071018f&units=imperial") parsed = response.parsed_response forecast = self.new forecast.location_name = @current.location_name # 24/48/72hr date forecast.hr24_dt = parsed["list"][8]["dt_txt"] forecast.hr48_dt = parsed["list"][16]["dt_txt"] forecast.hr72_dt = parsed["list"][24]["dt_txt"] # 24/48/72hr temp forecast.temp24 = parsed["list"][8]["main"]["temp"] forecast.temp48 = parsed["list"][16]["main"]["temp"] forecast.temp72 = parsed["list"][24]["main"]["temp"] # 24/48/72hr condition forecast.condition24 = parsed["list"][8]["weather"][0]["description"] forecast.condition48 = parsed["list"][16]["weather"][0]["description"] forecast.condition72 = parsed["list"][24]["weather"][0]["description"] # 24/48/72hr cloudiness forecast.cloudiness24 = parsed["list"][8]["clouds"]["all"] forecast.cloudiness48 = parsed["list"][16]["clouds"]["all"] forecast.cloudiness72 = parsed["list"][24]["clouds"]["all"] # 24/48/72hr humidity forecast.humidity24 = parsed["list"][8]["main"]["humidity"] forecast.humidity48 = parsed["list"][16]["main"]["humidity"] forecast.humidity72 = parsed["list"][24]["main"]["humidity"] # 24/48/72hr wind speed & direction forecast.wind_speed24 = parsed["list"][8]["wind"]["speed"] forecast.wind_speed48 = parsed["list"][16]["wind"]["speed"] forecast.wind_speed72 = parsed["list"][24]["wind"]["speed"] forecast.wind_direction24 = degToCompass(parsed["list"][8]["wind"]["deg"]) forecast.wind_direction48 = degToCompass(parsed["list"][16]["wind"]["deg"]) forecast.wind_direction72 = degToCompass(parsed["list"][24]["wind"]["deg"]) # Open query in browser to Google Maps forecast.google_maps = "https://www.google.com/maps/place/#{@current.coordinates.gsub(" ", "")}" @google_maps_link = @current.google_maps # Output 3 day forecast puts "\n-------------------------------\n" puts "\n\nIn 24 Hours:" puts "\nReport Time: #{forecast.hr24_dt}" puts "Location: #{forecast.location_name}, #{@current.country}" puts "Google Maps: #{forecast.google_maps}" puts "\nTemperature: #{forecast.temp24}ºF / #{toCelsius(forecast.temp24)}ºC" puts "Condition: #{forecast.condition24.capitalize}" puts "Cloudiness: #{forecast.cloudiness24}%" puts "\nHumidity: #{forecast.humidity24}%" puts "Wind Speed: #{forecast.wind_speed24} mph" puts "Wind Direction: #{forecast.wind_direction24}" puts "\n-------------------------------\n" puts "\n\nIn 48 Hours:" puts "\nReport Time: #{forecast.hr48_dt}" puts "Location: #{forecast.location_name}, #{@current.country}" puts "Google Maps: #{forecast.google_maps}" puts "\nTemperature: #{forecast.temp48}ºF / #{toCelsius(forecast.temp48)}ºC" puts "Condition: #{forecast.condition48.capitalize}" puts "Cloudiness: #{forecast.cloudiness48}%" puts "\nHumidity: #{forecast.humidity48}%" puts "Wind Speed: #{forecast.wind_speed48} mph" puts "Wind Direction: #{forecast.wind_direction48}" puts "\n-------------------------------\n" puts "\n\nIn 72 Hours:" puts "\nReport Time: #{forecast.hr72_dt}" puts "Location: #{forecast.location_name}, #{@current.country}" puts "Google Maps: #{forecast.google_maps}" puts "\nTemperature: #{forecast.temp72}ºF / #{toCelsius(forecast.temp72)}ºC" puts "Condition: #{forecast.condition72.capitalize}" puts "Cloudiness: #{forecast.cloudiness72}%" puts "\nHumidity: #{forecast.humidity72}%" puts "Wind Speed: #{forecast.wind_speed72} mph" puts "Wind Direction: #{forecast.wind_direction72}" end |
.locations ⇒ Object
12 13 14 |
# File 'lib/api.rb', line 12 def self.locations @@locations end |
.open_link ⇒ Object
Method to open link in Google Maps depending on OS
142 143 144 145 146 147 148 149 150 151 |
# File 'lib/api.rb', line 142 def self.open_link link = @google_maps_link if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/ system "start #{link}" elsif RbConfig::CONFIG['host_os'] =~ /darwin/ system "open #{link}" elsif RbConfig::CONFIG['host_os'] =~ /linux|bsd/ system "xdg-open #{link}" end end |