Class: NWS::Alert

Inherits:
Object
  • Object
show all
Defined in:
lib/nws/alert.rb

Overview

Represents a weather alert from the NWS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Alert

Initialize an Alert from API response data

Parameters:

  • data (Hash)

    Parsed JSON response from the alerts API



69
70
71
72
# File 'lib/nws/alert.rb', line 69

def initialize(data)
  @raw_data = data
  parse_data(data)
end

Instance Attribute Details

#affected_zonesArray<String> (readonly)

Returns URLs of affected forecast zones.

Returns:

  • (Array<String>)

    URLs of affected forecast zones



61
62
63
# File 'lib/nws/alert.rb', line 61

def affected_zones
  @affected_zones
end

#area_descString? (readonly)

Returns Description of affected areas.

Returns:

  • (String, nil)

    Description of affected areas



10
11
12
# File 'lib/nws/alert.rb', line 10

def area_desc
  @area_desc
end

#certaintyString? (readonly)

Returns Certainty level (e.g., “Observed”, “Likely”, “Possible”).

Returns:

  • (String, nil)

    Certainty level (e.g., “Observed”, “Likely”, “Possible”)



37
38
39
# File 'lib/nws/alert.rb', line 37

def certainty
  @certainty
end

#descriptionString? (readonly)

Returns Detailed description of the alert.

Returns:

  • (String, nil)

    Detailed description of the alert



52
53
54
# File 'lib/nws/alert.rb', line 52

def description
  @description
end

#effectiveTime? (readonly)

Returns When the alert becomes effective.

Returns:

  • (Time, nil)

    When the alert becomes effective



16
17
18
# File 'lib/nws/alert.rb', line 16

def effective
  @effective
end

#endsTime? (readonly)

Returns When the alert conditions end.

Returns:

  • (Time, nil)

    When the alert conditions end



25
26
27
# File 'lib/nws/alert.rb', line 25

def ends
  @ends
end

#eventString? (readonly)

Returns Event type (e.g., “Tornado Warning”, “Winter Storm Watch”).

Returns:

  • (String, nil)

    Event type (e.g., “Tornado Warning”, “Winter Storm Watch”)



43
44
45
# File 'lib/nws/alert.rb', line 43

def event
  @event
end

#expiresTime? (readonly)

Returns When the alert expires.

Returns:

  • (Time, nil)

    When the alert expires



22
23
24
# File 'lib/nws/alert.rb', line 22

def expires
  @expires
end

#headlineString? (readonly)

Returns Alert headline.

Returns:

  • (String, nil)

    Alert headline



49
50
51
# File 'lib/nws/alert.rb', line 49

def headline
  @headline
end

#idString? (readonly)

Returns Alert identifier.

Returns:

  • (String, nil)

    Alert identifier



7
8
9
# File 'lib/nws/alert.rb', line 7

def id
  @id
end

#instructionString? (readonly)

Returns Instructions for the public.

Returns:

  • (String, nil)

    Instructions for the public



55
56
57
# File 'lib/nws/alert.rb', line 55

def instruction
  @instruction
end

#message_typeString? (readonly)

Returns Message type (e.g., “Alert”, “Update”, “Cancel”).

Returns:

  • (String, nil)

    Message type (e.g., “Alert”, “Update”, “Cancel”)



31
32
33
# File 'lib/nws/alert.rb', line 31

def message_type
  @message_type
end

#onsetTime? (readonly)

Returns When the alert conditions begin.

Returns:

  • (Time, nil)

    When the alert conditions begin



19
20
21
# File 'lib/nws/alert.rb', line 19

def onset
  @onset
end

#raw_dataHash (readonly)

Returns Raw API response data.

Returns:

  • (Hash)

    Raw API response data



64
65
66
# File 'lib/nws/alert.rb', line 64

def raw_data
  @raw_data
end

#responseString? (readonly)

Returns Recommended response type.

Returns:

  • (String, nil)

    Recommended response type



58
59
60
# File 'lib/nws/alert.rb', line 58

def response
  @response
end

#sender_nameString? (readonly)

Returns Name of the sending organization.

Returns:

  • (String, nil)

    Name of the sending organization



46
47
48
# File 'lib/nws/alert.rb', line 46

def sender_name
  @sender_name
end

#sentTime? (readonly)

Returns When the alert was sent.

Returns:

  • (Time, nil)

    When the alert was sent



13
14
15
# File 'lib/nws/alert.rb', line 13

def sent
  @sent
end

#severityString? (readonly)

Returns Severity level (e.g., “Extreme”, “Severe”, “Moderate”, “Minor”).

Returns:

  • (String, nil)

    Severity level (e.g., “Extreme”, “Severe”, “Moderate”, “Minor”)



34
35
36
# File 'lib/nws/alert.rb', line 34

def severity
  @severity
end

#statusString? (readonly)

Returns Alert status (e.g., “Actual”, “Test”).

Returns:

  • (String, nil)

    Alert status (e.g., “Actual”, “Test”)



28
29
30
# File 'lib/nws/alert.rb', line 28

def status
  @status
end

#urgencyString? (readonly)

Returns Urgency level (e.g., “Immediate”, “Expected”, “Future”).

Returns:

  • (String, nil)

    Urgency level (e.g., “Immediate”, “Expected”, “Future”)



40
41
42
# File 'lib/nws/alert.rb', line 40

def urgency
  @urgency
end

Class Method Details

.fetch(state: nil, point: nil, zone: nil, active: true, client: NWS.client) ⇒ Array<Alert>

Fetch alerts from the NWS API

Parameters:

  • state (String, nil) (defaults to: nil)

    Two-letter state code to filter alerts

  • point (Array<Float>, nil) (defaults to: nil)

    Latitude and longitude pair [lat, lon]

  • zone (String, nil) (defaults to: nil)

    NWS zone identifier

  • active (Boolean) (defaults to: true)

    Whether to fetch only active alerts

  • client (Client) (defaults to: NWS.client)

    NWS API client instance

Returns:

  • (Array<Alert>)

    Array of Alert objects



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/nws/alert.rb', line 82

def self.fetch(state: nil, point: nil, zone: nil, active: true, client: NWS.client)
  params = {}
  params[:status] = "actual" if active

  path = if state
           params[:area] = state.upcase
           "/alerts/active"
         elsif point
           params[:point] = "#{point[0]},#{point[1]}"
           "/alerts/active"
         elsif zone
           "/alerts/active/zone/#{zone}"
         else
           "/alerts/active"
         end

  data = client.get(path, params: params)
  features = data["features"] || []
  features.map { |f| new(f) }
end

.types(client: NWS.client) ⇒ Array<String>

Fetch all available alert types

Parameters:

  • client (Client) (defaults to: NWS.client)

    NWS API client instance

Returns:

  • (Array<String>)

    Array of alert event type names



107
108
109
110
# File 'lib/nws/alert.rb', line 107

def self.types(client: NWS.client)
  data = client.get("/alerts/types")
  data["eventTypes"] || []
end

Instance Method Details

#active?Boolean

Check if the alert is currently active

Returns:

  • (Boolean)

    True if the alert has not yet expired



115
116
117
118
# File 'lib/nws/alert.rb', line 115

def active?
  return false unless @expires
  Time.now < @expires
end

#advisory?Boolean

Check if this is an advisory

Returns:

  • (Boolean)

    True if the event name contains “advisory”



144
145
146
# File 'lib/nws/alert.rb', line 144

def advisory?
  !!@event&.downcase&.include?("advisory")
end

#detailedString

Get a detailed formatted string representation of the alert

Returns:

  • (String)

    Multi-line detailed alert with description and instructions



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/nws/alert.rb', line 166

def detailed
  lines = []
  lines << "#{@event}"
  lines << "=" * @event.length
  lines << ""
  lines << "Severity: #{@severity}"
  lines << "Urgency: #{@urgency}"
  lines << "Certainty: #{@certainty}"
  lines << ""
  lines << "Areas Affected:"
  lines << "  #{@area_desc}"
  lines << ""
  lines << "Timing:"
  lines << "  Effective: #{@effective&.strftime('%Y-%m-%d %I:%M %p')}"
  lines << "  Expires: #{@expires&.strftime('%Y-%m-%d %I:%M %p')}"
  lines << ""
  lines << "Headline:"
  lines << "  #{@headline}"
  lines << ""
  lines << "Description:"
  wrap_text(@description, 80).each { |line| lines << "  #{line}" }
  if @instruction
    lines << ""
    lines << "Instructions:"
    wrap_text(@instruction, 80).each { |line| lines << "  #{line}" }
  end
  lines.join("\n")
end

#severe?Boolean

Check if this is a severe alert

Returns:

  • (Boolean)

    True if severity is “Extreme” or “Severe”



123
124
125
# File 'lib/nws/alert.rb', line 123

def severe?
  %w[Extreme Severe].include?(@severity)
end

#to_sString

Get a formatted string representation of the alert

Returns:

  • (String)

    Multi-line formatted alert summary



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/nws/alert.rb', line 151

def to_s
  lines = []
  lines << "#{@event}"
  lines << "  Severity: #{@severity} | Urgency: #{@urgency} | Certainty: #{@certainty}"
  lines << "  Areas: #{@area_desc}"
  lines << "  Effective: #{@effective&.strftime('%Y-%m-%d %I:%M %p')}"
  lines << "  Expires: #{@expires&.strftime('%Y-%m-%d %I:%M %p')}"
  lines << ""
  lines << "  #{@headline}"
  lines.join("\n")
end

#warning?Boolean

Check if this is a warning

Returns:

  • (Boolean)

    True if the event name contains “warning”



130
131
132
# File 'lib/nws/alert.rb', line 130

def warning?
  !!@event&.downcase&.include?("warning")
end

#watch?Boolean

Check if this is a watch

Returns:

  • (Boolean)

    True if the event name contains “watch”



137
138
139
# File 'lib/nws/alert.rb', line 137

def watch?
  !!@event&.downcase&.include?("watch")
end