Class: WeatherData::Wind

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/weather_data/wind.rb

Constant Summary collapse

ATTRIBUTES =
[
  :direction,
  :speed
].freeze
CARDINAL_DIRECTIONS =
{
  :N   => 0.0,
  :NNE => 22.5,
  :NE  => 45.0,
  :ENE => 67.5,
  :E   => 90.0,
  :ESE => 112.5,
  :SE  => 135.0,
  :SSE => 157.5,
  :S   => 180.0,
  :SSW => 202.5,
  :SW  => 225.0,
  :WSW => 247.5,
  :W   => 270.0,
  :WNW => 292.5,
  :NW  => 315.0,
  :NNW => 337.5
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(speed, units = "m/s", azimuth_or_direction = nil) ⇒ Wind

Returns a new instance of Wind.



44
45
46
47
# File 'lib/weather_data/wind.rb', line 44

def initialize(speed, units = "m/s", azimuth_or_direction = nil)
  super normalize_speed(speed, units)
  @direction = to_direction(azimuth_or_direction) if azimuth_or_direction
end

Class Method Details

.parse(s) ⇒ Object

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
41
42
# File 'lib/weather_data/wind.rb', line 33

def self.parse(s)
  _, speed, units = /(\d+(?:\.\d+)?)\s*(m\/?s(?:ec)?|km\/?h(?:our)?)/i.match(s).to_a
  units ||= "m/s"

  raise ArgumentError.new(%Q{"#{s.inspect}" doesn't contain wind speed}) unless speed

  _, direction = /(#{CARDINAL_DIRECTIONS.keys.join('|')})/.match(s).to_a

  new(speed.to_f, units, direction)
end

Instance Method Details

#speedObject



49
50
51
# File 'lib/weather_data/wind.rb', line 49

def speed
  __getobj__
end

#speed=(value) ⇒ Object



53
54
55
# File 'lib/weather_data/wind.rb', line 53

def speed=(value)
  __setobj__(value)
end

#to_kmhObject



61
62
63
# File 'lib/weather_data/wind.rb', line 61

def to_kmh
  speed * 3.6
end

#to_sObject



65
66
67
# File 'lib/weather_data/wind.rb', line 65

def to_s
  ["#{speed} #{units}", direction].compact.join(" ")
end

#unitsObject



57
58
59
# File 'lib/weather_data/wind.rb', line 57

def units
  'm/s'
end