Class: NWS::Alert
- Inherits:
-
Object
- Object
- NWS::Alert
- Defined in:
- lib/nws/alert.rb
Overview
Represents a weather alert from the NWS
Instance Attribute Summary collapse
-
#affected_zones ⇒ Array<String>
readonly
URLs of affected forecast zones.
-
#area_desc ⇒ String?
readonly
Description of affected areas.
-
#certainty ⇒ String?
readonly
Certainty level (e.g., “Observed”, “Likely”, “Possible”).
-
#description ⇒ String?
readonly
Detailed description of the alert.
-
#effective ⇒ Time?
readonly
When the alert becomes effective.
-
#ends ⇒ Time?
readonly
When the alert conditions end.
-
#event ⇒ String?
readonly
Event type (e.g., “Tornado Warning”, “Winter Storm Watch”).
-
#expires ⇒ Time?
readonly
When the alert expires.
-
#headline ⇒ String?
readonly
Alert headline.
-
#id ⇒ String?
readonly
Alert identifier.
-
#instruction ⇒ String?
readonly
Instructions for the public.
-
#message_type ⇒ String?
readonly
Message type (e.g., “Alert”, “Update”, “Cancel”).
-
#onset ⇒ Time?
readonly
When the alert conditions begin.
-
#raw_data ⇒ Hash
readonly
Raw API response data.
-
#response ⇒ String?
readonly
Recommended response type.
-
#sender_name ⇒ String?
readonly
Name of the sending organization.
-
#sent ⇒ Time?
readonly
When the alert was sent.
-
#severity ⇒ String?
readonly
Severity level (e.g., “Extreme”, “Severe”, “Moderate”, “Minor”).
-
#status ⇒ String?
readonly
Alert status (e.g., “Actual”, “Test”).
-
#urgency ⇒ String?
readonly
Urgency level (e.g., “Immediate”, “Expected”, “Future”).
Class Method Summary collapse
-
.fetch(state: nil, point: nil, zone: nil, active: true, client: NWS.client) ⇒ Array<Alert>
Fetch alerts from the NWS API.
-
.types(client: NWS.client) ⇒ Array<String>
Fetch all available alert types.
Instance Method Summary collapse
-
#active? ⇒ Boolean
Check if the alert is currently active.
-
#advisory? ⇒ Boolean
Check if this is an advisory.
-
#detailed ⇒ String
Get a detailed formatted string representation of the alert.
-
#initialize(data) ⇒ Alert
constructor
Initialize an Alert from API response data.
-
#severe? ⇒ Boolean
Check if this is a severe alert.
-
#to_s ⇒ String
Get a formatted string representation of the alert.
-
#warning? ⇒ Boolean
Check if this is a warning.
-
#watch? ⇒ Boolean
Check if this is a watch.
Constructor Details
#initialize(data) ⇒ Alert
Initialize an Alert from API response data
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_zones ⇒ Array<String> (readonly)
Returns URLs of affected forecast zones.
61 62 63 |
# File 'lib/nws/alert.rb', line 61 def affected_zones @affected_zones end |
#area_desc ⇒ String? (readonly)
Returns Description of affected areas.
10 11 12 |
# File 'lib/nws/alert.rb', line 10 def area_desc @area_desc end |
#certainty ⇒ String? (readonly)
Returns Certainty level (e.g., “Observed”, “Likely”, “Possible”).
37 38 39 |
# File 'lib/nws/alert.rb', line 37 def certainty @certainty end |
#description ⇒ String? (readonly)
Returns Detailed description of the alert.
52 53 54 |
# File 'lib/nws/alert.rb', line 52 def description @description end |
#effective ⇒ Time? (readonly)
Returns When the alert becomes effective.
16 17 18 |
# File 'lib/nws/alert.rb', line 16 def effective @effective end |
#ends ⇒ Time? (readonly)
Returns When the alert conditions end.
25 26 27 |
# File 'lib/nws/alert.rb', line 25 def ends @ends end |
#event ⇒ String? (readonly)
Returns Event type (e.g., “Tornado Warning”, “Winter Storm Watch”).
43 44 45 |
# File 'lib/nws/alert.rb', line 43 def event @event end |
#expires ⇒ Time? (readonly)
Returns When the alert expires.
22 23 24 |
# File 'lib/nws/alert.rb', line 22 def expires @expires end |
#headline ⇒ String? (readonly)
Returns Alert headline.
49 50 51 |
# File 'lib/nws/alert.rb', line 49 def headline @headline end |
#id ⇒ String? (readonly)
Returns Alert identifier.
7 8 9 |
# File 'lib/nws/alert.rb', line 7 def id @id end |
#instruction ⇒ String? (readonly)
Returns Instructions for the public.
55 56 57 |
# File 'lib/nws/alert.rb', line 55 def instruction @instruction end |
#message_type ⇒ String? (readonly)
Returns Message type (e.g., “Alert”, “Update”, “Cancel”).
31 32 33 |
# File 'lib/nws/alert.rb', line 31 def end |
#onset ⇒ Time? (readonly)
Returns When the alert conditions begin.
19 20 21 |
# File 'lib/nws/alert.rb', line 19 def onset @onset end |
#raw_data ⇒ Hash (readonly)
Returns Raw API response data.
64 65 66 |
# File 'lib/nws/alert.rb', line 64 def raw_data @raw_data end |
#response ⇒ String? (readonly)
Returns Recommended response type.
58 59 60 |
# File 'lib/nws/alert.rb', line 58 def response @response end |
#sender_name ⇒ String? (readonly)
Returns Name of the sending organization.
46 47 48 |
# File 'lib/nws/alert.rb', line 46 def sender_name @sender_name end |
#sent ⇒ Time? (readonly)
Returns When the alert was sent.
13 14 15 |
# File 'lib/nws/alert.rb', line 13 def sent @sent end |
#severity ⇒ String? (readonly)
Returns Severity level (e.g., “Extreme”, “Severe”, “Moderate”, “Minor”).
34 35 36 |
# File 'lib/nws/alert.rb', line 34 def severity @severity end |
#status ⇒ String? (readonly)
Returns Alert status (e.g., “Actual”, “Test”).
28 29 30 |
# File 'lib/nws/alert.rb', line 28 def status @status end |
#urgency ⇒ String? (readonly)
Returns 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
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
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
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
144 145 146 |
# File 'lib/nws/alert.rb', line 144 def advisory? !!@event&.downcase&.include?("advisory") end |
#detailed ⇒ String
Get a detailed formatted string representation of the alert
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
123 124 125 |
# File 'lib/nws/alert.rb', line 123 def severe? %w[Extreme Severe].include?(@severity) end |
#to_s ⇒ String
Get a formatted string representation of the alert
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
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
137 138 139 |
# File 'lib/nws/alert.rb', line 137 def watch? !!@event&.downcase&.include?("watch") end |