Class: WeatherData::Weather
- Inherits:
-
Object
- Object
- WeatherData::Weather
- Defined in:
- lib/weather_data/weather.rb
Constant Summary collapse
- ATTRIBUTES =
[ :temperature, :humidity, :wind ].freeze
Class Method Summary collapse
Instance Method Summary collapse
- #apparent_temperature ⇒ Object
- #dew_point ⇒ Object
- #humidity=(value) ⇒ Object
- #humindex ⇒ Object
-
#initialize(attributes = {}) ⇒ Weather
constructor
A new instance of Weather.
- #temperature=(value) ⇒ Object
- #vapour_pressure ⇒ Object
Constructor Details
#initialize(attributes = {}) ⇒ Weather
Returns a new instance of Weather.
36 37 38 39 40 |
# File 'lib/weather_data/weather.rb', line 36 def initialize(attributes = {}) ATTRIBUTES.each do |attr| self.send(:"#{attr}=", attributes[attr]) end end |
Class Method Details
.parse(s) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/weather_data/weather.rb', line 17 def self.parse(s) attributes = {}.tap do |attrs| { :temperature => Temperature, :humidity => Humidity, :wind => Wind }.each do |attr, parser| begin attrs[attr] = parser.parse(s) rescue ArgumentError end end end raise ArgumentError.new(%Q{"#{s}" doesn't contain weather data}) if attributes.empty? new(attributes) end |
Instance Method Details
#apparent_temperature ⇒ Object
66 67 68 69 70 71 |
# File 'lib/weather_data/weather.rb', line 66 def apparent_temperature if vapour_pressure && temperature t = temperature.to_celsius.degrees Temperature::Celsius.new(t + 0.33 * vapour_pressure - 0.7 * wind.to_f - 4.0) end end |
#dew_point ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/weather_data/weather.rb', line 42 def dew_point if humidity && temperature t = temperature.to_celsius.degrees a, b, c = magnus_formula_constants(t) gamma = Math.log(humidity / 100) + b * t / (c + t) Temperature::Celsius.new(c * gamma / (b - gamma)) end end |
#humidity=(value) ⇒ Object
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/weather_data/weather.rb', line 84 def humidity=(value) @humidity = case value when String Humidity.parse(value) when Humidity::Relative value else raise ArgumentError.new(%Q{#{value.inspect} must be String or an instance of WeatherData::Humidity::Relative}) end end |
#humindex ⇒ Object
59 60 61 62 63 64 |
# File 'lib/weather_data/weather.rb', line 59 def humindex if temperature && dew_point t = temperature.to_celsius.degrees + 0.5555 * (6.11 * Math.exp(5417.753 * (1 / 273.16 - 1 / dew_point.to_kelvin.degrees)) - 10) t.celsius end end |
#temperature=(value) ⇒ Object
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/weather_data/weather.rb', line 73 def temperature=(value) @temperature = case value when String Temperature.parse(value) when Temperature::Base value else raise ArgumentError.new(%Q{#{value.inspect} must be String or kind of WeatherData::Temperature::Base}) end end |
#vapour_pressure ⇒ Object
52 53 54 55 56 57 |
# File 'lib/weather_data/weather.rb', line 52 def vapour_pressure if humidity t = temperature.to_celsius.degrees (humidity / 100.0) * 6.105 * Math.exp(17.27 * t / (237.7 + t)) end end |