Class: Weather

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_weather/weather.rb

Constant Summary collapse

BASE_URL =
"https://api.openweathermap.org/data/2.5/"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(city) ⇒ Weather

Returns a new instance of Weather.



8
9
10
11
# File 'lib/easy_weather/weather.rb', line 8

def initialize(city)
  @data = get_general_data(city) 
  @forecast = get_forecast_data(5, city)
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



6
7
8
# File 'lib/easy_weather/weather.rb', line 6

def data
  @data
end

#forecastObject

Returns the value of attribute forecast.



6
7
8
# File 'lib/easy_weather/weather.rb', line 6

def forecast
  @forecast
end

Instance Method Details

#astronomy_dataObject



22
23
24
25
26
27
28
# 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_dataObject



38
39
40
41
42
43
44
45
# 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_dataObject



13
14
15
16
17
18
19
20
# 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

#temperatureObject



30
31
32
# File 'lib/easy_weather/weather.rb', line 30

def temperature
  "#{data["main"]["temp"]} centigrades" 
end

#windObject



34
35
36
# File 'lib/easy_weather/weather.rb', line 34

def wind
  "#{data["wind"]["speed"]} meters/sec"
end