Class: OpenWeatherLite::WeatherResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/open_weather_lite/weather_response.rb

Overview

Wrapper class to hold the json response from api endpoint

includes methods that interact on the response

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(weather) ⇒ WeatherResponse



7
8
9
# File 'lib/open_weather_lite/weather_response.rb', line 7

def initialize(weather)
  @weather = JSON.parse(weather, symbolize_names: true)
end

Instance Attribute Details

#weatherObject

Returns the value of attribute weather.



5
6
7
# File 'lib/open_weather_lite/weather_response.rb', line 5

def weather
  @weather
end

Instance Method Details

#cloudsObject



42
43
44
45
# File 'lib/open_weather_lite/weather_response.rb', line 42

def clouds
  return 0 if weather[:clouds].nil?
  weather[:clouds][:all]
end

#cloudy?Boolean



38
39
40
# File 'lib/open_weather_lite/weather_response.rb', line 38

def cloudy?
  !weather[:clouds].nil? && weather[:clouds][:all] > 0
end

#coordsObject



11
12
13
14
15
16
# File 'lib/open_weather_lite/weather_response.rb', line 11

def coords
  {
    latitude: weather[:coord][:lat],
    longitude: weather[:coord][:lon]
  }
end

#humidityObject



26
27
28
# File 'lib/open_weather_lite/weather_response.rb', line 26

def humidity
  weather[:main][:humidity]
end

#pressureObject



22
23
24
# File 'lib/open_weather_lite/weather_response.rb', line 22

def pressure
  weather[:main][:pressure]
end

#rainObject



51
52
53
54
# File 'lib/open_weather_lite/weather_response.rb', line 51

def rain
  return 0 unless rainy?
  weather[:rain][:'3h']
end

#rainy?Boolean



47
48
49
# File 'lib/open_weather_lite/weather_response.rb', line 47

def rainy?
  !weather[:rain].nil? && weather[:rain][:'3h'] > 0
end

#snowObject



60
61
62
63
# File 'lib/open_weather_lite/weather_response.rb', line 60

def snow
  return 0 unless snowy?
  weather[:snow][:'3h']
end

#snowy?Boolean



56
57
58
# File 'lib/open_weather_lite/weather_response.rb', line 56

def snowy?
  !weather[:snow].nil? && weather[:snow][:'3h'] > 0
end

#sunriseObject



65
66
67
# File 'lib/open_weather_lite/weather_response.rb', line 65

def sunrise
  weather[:sys][:sunrise]
end

#sunsetObject



69
70
71
# File 'lib/open_weather_lite/weather_response.rb', line 69

def sunset
  weather[:sys][:sunset]
end

#temperatureObject



18
19
20
# File 'lib/open_weather_lite/weather_response.rb', line 18

def temperature
  weather[:main][:temp]
end

#temperature_rangeObject



30
31
32
# File 'lib/open_weather_lite/weather_response.rb', line 30

def temperature_range
  [weather[:main][:temp_min], weather[:main][:temp_max]]
end

#windObject



34
35
36
# File 'lib/open_weather_lite/weather_response.rb', line 34

def wind
  weather[:wind]
end