Class: Metar::Data::Wind

Inherits:
Base
  • Object
show all
Defined in:
lib/metar/data/wind.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#raw

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#value

Constructor Details

#initialize(raw, direction:, speed:, gusts: nil) ⇒ Wind

Returns a new instance of Wind.



73
74
75
76
77
78
# File 'lib/metar/data/wind.rb', line 73

def initialize(raw, direction:, speed:, gusts: nil)
  @raw = raw
  @direction = direction
  @speed = speed
  @gusts = gusts
end

Instance Attribute Details

#directionObject (readonly)

Returns the value of attribute direction.



71
72
73
# File 'lib/metar/data/wind.rb', line 71

def direction
  @direction
end

#gustsObject (readonly)

Returns the value of attribute gusts.



71
72
73
# File 'lib/metar/data/wind.rb', line 71

def gusts
  @gusts
end

#speedObject (readonly)

Returns the value of attribute speed.



71
72
73
# File 'lib/metar/data/wind.rb', line 71

def speed
  @speed
end

Class Method Details

.parse(raw, strict: false) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/metar/data/wind.rb', line 6

def self.parse(raw, strict: false)
  return nil if raw.nil?

  plain_match =
    if strict
      /^(\d{3})(\d{2}(|MPS|KMH|KT))$/
    else
      /^(\d{3})(\d{2,3}(|MPS|KMH|KT))$/
    end

  m1 = raw.match(plain_match)
  if m1
    return nil if m1[1].to_i > 360

    return new(
      raw,
      direction: Metar::Data::Direction.new(m1[1]),
      speed: Metar::Data::Speed.parse(m1[2])
    )
  end

  m2 = raw.match(/^(\d{3})(\d{2})G(\d{2,3}(|MPS|KMH|KT))$/)
  if m2
    return nil if m2[1].to_i > 360

    return new(
      raw,
      direction: Metar::Data::Direction.new(m2[1]),
      speed: Metar::Data::Speed.parse(m2[2] + m2[4]),
      gusts: Metar::Data::Speed.parse(m2[3])
    )
  end

  m3 = raw.match(/^VRB(\d{2})G(\d{2,3})(|MPS|KMH|KT)$/)
  if m3
    speed = m3[1] + m3[3]
    gusts = m3[2] + m3[3]
    return new(
      raw,
      direction: :variable_direction,
      speed: Metar::Data::Speed.parse(speed),
      gusts: Metar::Data::Speed.parse(gusts)
    )
  end

  m4 = raw.match(/^VRB(\d{2}(|MPS|KMH|KT))$/)
  if m4
    speed = Metar::Data::Speed.parse(m4[1])
    return new(raw, direction: :variable_direction, speed: speed)
  end

  m5 = raw.match(%r{^/{3}(\d{2}(|MPS|KMH|KT))$})
  if m5
    speed = Metar::Data::Speed.parse(m5[1])
    return new(raw, direction: :unknown_direction, speed: speed)
  end

  m6 = raw.match(%r{^/////(|MPS|KMH|KT)$})
  if m6
    return new(raw, direction: :unknown_direction, speed: :unknown_speed)
  end

  nil
end

Instance Method Details

#to_s(options = {}) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/metar/data/wind.rb', line 80

def to_s(options = {})
  options = {
    direction_units: :compass,
    speed_units: :kilometers_per_hour
  }.merge(options)
  speed =
    case @speed
    when :unknown_speed
      I18n.t('metar.wind.unknown_speed')
    else
      @speed.to_s(
        abbreviated: true,
        precision: 0,
        units: options[:speed_units]
      )
    end
  direction =
    case @direction
    when :variable_direction
      I18n.t('metar.wind.variable_direction')
    when :unknown_direction
      I18n.t('metar.wind.unknown_direction')
    else
      @direction.to_s(units: options[:direction_units])
    end
  s = "#{speed} #{direction}"

  if !@gusts.nil?
    g = @gusts.to_s(
      abbreviated: true,
      precision: 0,
      units: options[:speed_units]
    )
    s += " #{I18n.t('metar.wind.gusts')} #{g}"
  end

  s
end