class Weather
Constants
- BASE_URL
Attributes
data[RW]
forecast[RW]
Public Class Methods
new(city)
click to toggle source
# File lib/easy_weather/weather.rb, line 8 def initialize(city) @data = get_general_data(city) @forecast = get_forecast_data(5, city) end
Public Instance Methods
astronomy_data()
click to toggle source
# File lib/easy_weather/weather.rb, line 22 def astronomy_data data_hash = { sunrise: data["sys"]["sunrise"], sunset: data["sys"]["sunset"] } "Sunrice: #{data_hash[:sunrise]} UTC, sunset: #{data_hash[:sunset]} UTC" end
atmosphere_data()
click to toggle source
# File lib/easy_weather/weather.rb, line 38 def atmosphere_data data_hash = { humidity: data["main"]["humidity"], pressure: data["main"]["pressure"], visibility: data["visibility"] } "Humidy: #{data_hash[:humidity]}%, pressure: #{data_hash[:pressure]}hPa, visibility: #{data_hash[:visibility]} meters" end
forecast_data()
click to toggle source
# File lib/easy_weather/weather.rb, line 13 def forecast_data forecast["list"].each_with_index do |day, index| puts "Day #{index + 1}\n" puts "Temperature: #{day["main"]["temp"]} centigrades\n" puts "Weather: #{day["weather"][0]["description"]}\n" end "Take your precautions for the week!" end
temperature()
click to toggle source
# File lib/easy_weather/weather.rb, line 30 def temperature "#{data["main"]["temp"]} centigrades" end
wind()
click to toggle source
# File lib/easy_weather/weather.rb, line 34 def wind "#{data["wind"]["speed"]} meters/sec" end
Private Instance Methods
fetch_data(url)
click to toggle source
# File lib/easy_weather/weather.rb, line 62 def fetch_data(url) response = HTTParty.get(url) JSON.parse(response.body) end
get_forecast_data(days, city)
click to toggle source
# File lib/easy_weather/weather.rb, line 56 def get_forecast_data(days, city) url = "#{BASE_URL}forecast?q=#{city} &units=metric&cnt=#{days}&appid=#{ENV["EASY_WEATHER"]}" fetch_data(url) end
get_general_data(city)
click to toggle source
# File lib/easy_weather/weather.rb, line 50 def get_general_data(city) url = "#{BASE_URL}weather?q=#{city} &units=metric&APPID=#{ENV["EASY_WEATHER"]}" fetch_data(url) end