Class: RCAP::Base::Alert

Inherits:
Object
  • Object
show all
Includes:
Validation
Defined in:
lib/rcap/base/alert.rb,
lib/rcap/formatters/yaml.rb

Direct Known Subclasses

CAP_1_0::Alert, CAP_1_1::Alert, CAP_1_2::Alert

Constant Summary collapse

STATUS_ACTUAL =
'Actual'
STATUS_EXERCISE =
'Exercise'
STATUS_SYSTEM =
'System'
STATUS_TEST =
'Test'
VALID_STATUSES =

Valid values for status

[STATUS_ACTUAL, STATUS_EXERCISE, STATUS_SYSTEM, STATUS_TEST]
MSG_TYPE_ALERT =
'Alert'
MSG_TYPE_UPDATE =
'Update'
MSG_TYPE_CANCEL =
'Cancel'
MSG_TYPE_ACK =
'Ack'
MSG_TYPE_ERROR =
'Error'
VALID_MSG_TYPES =

Valid values for msg_type

[MSG_TYPE_ALERT, MSG_TYPE_UPDATE, MSG_TYPE_CANCEL, MSG_TYPE_ACK, MSG_TYPE_ERROR]
SCOPE_PUBLIC =
'Public'
SCOPE_RESTRICTED =
'Restricted'
SCOPE_PRIVATE =
'Private'
VALID_SCOPES =

Valid values for scope

[SCOPE_PUBLIC, SCOPE_PRIVATE, SCOPE_RESTRICTED]
XML_ELEMENT_NAME =
'alert'
IDENTIFIER_ELEMENT_NAME =
'identifier'
SENDER_ELEMENT_NAME =
'sender'
SENT_ELEMENT_NAME =
'sent'
STATUS_ELEMENT_NAME =
'status'
MSG_TYPE_ELEMENT_NAME =
'msgType'
SOURCE_ELEMENT_NAME =
'source'
SCOPE_ELEMENT_NAME =
'scope'
RESTRICTION_ELEMENT_NAME =
'restriction'
ADDRESSES_ELEMENT_NAME =
'addresses'
CODE_ELEMENT_NAME =
'code'
NOTE_ELEMENT_NAME =
'note'
REFERENCES_ELEMENT_NAME =
'references'
INCIDENTS_ELEMENT_NAME =
'incidents'
XPATH =
'cap:alert'
IDENTIFIER_XPATH =
"cap:#{ IDENTIFIER_ELEMENT_NAME }"
SENDER_XPATH =
"cap:#{ SENDER_ELEMENT_NAME }"
SENT_XPATH =
"cap:#{ SENT_ELEMENT_NAME }"
STATUS_XPATH =
"cap:#{ STATUS_ELEMENT_NAME }"
MSG_TYPE_XPATH =
"cap:#{ MSG_TYPE_ELEMENT_NAME }"
SOURCE_XPATH =
"cap:#{ SOURCE_ELEMENT_NAME }"
SCOPE_XPATH =
"cap:#{ SCOPE_ELEMENT_NAME }"
RESTRICTION_XPATH =
"cap:#{ RESTRICTION_ELEMENT_NAME }"
ADDRESSES_XPATH =
"cap:#{ ADDRESSES_ELEMENT_NAME }"
CODE_XPATH =
"cap:#{ CODE_ELEMENT_NAME }"
NOTE_XPATH =
"cap:#{ NOTE_ELEMENT_NAME }"
REFERENCES_XPATH =
"cap:#{ REFERENCES_ELEMENT_NAME }"
INCIDENTS_XPATH =
"cap:#{ INCIDENTS_ELEMENT_NAME }"
CAP_VERSION_YAML =
'CAP Version'
IDENTIFIER_YAML =
'Identifier'
SENDER_YAML =
'Sender'
SENT_YAML =
'Sent'
STATUS_YAML =
'Status'
MSG_TYPE_YAML =
'Message Type'
SOURCE_YAML =
'Source'
SCOPE_YAML =
'Scope'
RESTRICTION_YAML =
'Restriction'
ADDRESSES_YAML =
'Addresses'
CODES_YAML =
'Codes'
NOTE_YAML =
'Note'
REFERENCES_YAML =
'References'
INCIDENTS_YAML =
'Incidents'
INFOS_YAML =
'Information'
CAP_VERSION_KEY =
'cap_version'
IDENTIFIER_KEY =
'identifier'
SENDER_KEY =
'sender'
SENT_KEY =
'sent'
STATUS_KEY =
'status'
MSG_TYPE_KEY =
'msg_type'
SOURCE_KEY =
'source'
SCOPE_KEY =
'scope'
RESTRICTION_KEY =
'restriction'
ADDRESSES_KEY =
'addresses'
CODES_KEY =
'codes'
NOTE_KEY =
'note'
REFERENCES_KEY =
'references'
INCIDENTS_KEY =
'incidents'
INFOS_KEY =
'infos'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Validation

#errors, included, #valid?, #validate

Constructor Details

#initialize {|alert| ... } ⇒ Alert

Initialises a new Alert object. Yields the initialised alert to a block.

Examples:

alert = RCAP::CAP_1_2::Alert.new do |alert|
          alert.sender = alerts@example.org
          alert.status = Alert::STATUS_ACTUAL
          alert.msg_type = Alert::MSG_TYPE_ALERT
          alert.scope = Alert::SCOPE_PUBLIC
        end

Yield Parameters:

  • alert (Alert)

    The newly initialised Alert.


83
84
85
86
87
88
89
90
91
# File 'lib/rcap/base/alert.rb', line 83

def initialize
  @identifier  = RCAP.generate_identifier
  @addresses   = []
  @codes       = []
  @references  = []
  @incidents   = []
  @infos       = []
  yield(self) if block_given?
end

Instance Attribute Details

#addressesArray<String> (readonly)

Returns Collection of address strings. Depends on scope being SCOPE_PRIVATE

Returns:


47
48
49
# File 'lib/rcap/base/alert.rb', line 47

def addresses
  @addresses
end

#codesArray<String> (readonly)

Returns:


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

def codes
  @codes
end

#identifierString

Returns If not set a UUID will be set by default on object initialisation

Returns:

  • (String)

    If not set a UUID will be set by default on object initialisation


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

def identifier
  @identifier
end

#incidentsArray<String> (readonly)

Returns Collection of incident strings

Returns:


54
55
56
# File 'lib/rcap/base/alert.rb', line 54

def incidents
  @incidents
end

#infosArray<Info> (readonly)

Returns:


56
57
58
# File 'lib/rcap/base/alert.rb', line 56

def infos
  @infos
end

#msg_typeString

Returns Can only be one of VALID_MSG_TYPES

Returns:


36
37
38
# File 'lib/rcap/base/alert.rb', line 36

def msg_type
  @msg_type
end

#noteString

Returns:


44
45
46
# File 'lib/rcap/base/alert.rb', line 44

def note
  @note
end

#referencesArray<String> (readonly)

Returns Collection of references to previous alerts

Returns:

  • (Array<String>)

    Collection of references to previous alerts

See Also:


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

def references
  @references
end

#restrictionString

Returns Required if scope is SCOPE_RESTRICTED

Returns:


42
43
44
# File 'lib/rcap/base/alert.rb', line 42

def restriction
  @restriction
end

#scopeString

Returns Can only be one of VALID_SCOPES

Returns:


38
39
40
# File 'lib/rcap/base/alert.rb', line 38

def scope
  @scope
end

#senderString

Returns:


30
31
32
# File 'lib/rcap/base/alert.rb', line 30

def sender
  @sender
end

#sentDateTime

Returns If not set will be time of creation.

Returns:

  • (DateTime)

    If not set will be time of creation.


32
33
34
# File 'lib/rcap/base/alert.rb', line 32

def sent
  @sent
end

#sourceString

Returns:


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

def source
  @source
end

#statusString

Returns Can only be one of VALID_STATUSES

Returns:


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

def status
  @status
end

Class Method Details

.from_h(alert_hash) ⇒ RCAP::CAP_1_0::Alert

Initialises an Alert object from a Hash produced by Alert#to_h

Parameters:

  • alert_hash (Hash)

Returns:


398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'lib/rcap/base/alert.rb', line 398

def self.from_h(alert_hash)
  new do |alert|
    alert.identifier  = RCAP.strip_if_given(alert_hash[IDENTIFIER_KEY])
    alert.sender      = RCAP.strip_if_given(alert_hash[SENDER_KEY])
    alert.sent        = RCAP.parse_datetime(alert_hash[SENT_KEY])
    alert.status      = RCAP.strip_if_given(alert_hash[STATUS_KEY])
    alert.msg_type    = RCAP.strip_if_given(alert_hash[MSG_TYPE_KEY])
    alert.source      = RCAP.strip_if_given(alert_hash[SOURCE_KEY])
    alert.scope       = RCAP.strip_if_given(alert_hash[SCOPE_KEY])
    alert.restriction = RCAP.strip_if_given(alert_hash[RESTRICTION_KEY])
    Array(alert_hash[ ADDRESSES_KEY]).each do |address|
      alert.addresses << address.strip
    end
    Array(alert_hash[ CODES_KEY]).each do |code|
      alert.codes << code.strip
    end
    alert.note = alert_hash[ NOTE_KEY]
    Array(alert_hash[ REFERENCES_KEY]).each do |reference|
      alert.references << reference.strip
    end

    Array(alert_hash[ INCIDENTS_KEY]).each do |incident|
      alert.incidents << incident.strip
    end

    Array(alert_hash[ INFOS_KEY]).each do |info_hash|
      alert.infos << alert.info_class.from_h(info_hash)
    end
  end
end

.from_json(json_string) ⇒ Alert

Initialises an Alert object from a JSON string produced by Alert#to_json

Parameters:

Returns:


445
446
447
# File 'lib/rcap/base/alert.rb', line 445

def self.from_json(json_string)
  from_h(JSON.parse(json_string))
end

.from_xml(xml) ⇒ Alert

Initialise an Alert object from an XML string. Any object that is a subclass of IO (e.g. File) can be passed in.

Parameters:

Returns:


275
276
277
# File 'lib/rcap/base/alert.rb', line 275

def self.from_xml(xml)
  from_xml_document(REXML::Document.new(xml))
end

.from_xml_document(xml_document) ⇒ Alert

Parameters:

  • xml_document (REXML::Document)

Returns:


267
268
269
# File 'lib/rcap/base/alert.rb', line 267

def self.from_xml_document(xml_document)
  from_xml_element(xml_document.root)
end

.from_xml_element(alert_xml_element) ⇒ RCAP::CAP_1_0::Alert

Parameters:

  • alert_xml_element (REXML::Element)

Returns:


230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/rcap/base/alert.rb', line 230

def self.from_xml_element(alert_xml_element)
  new do |alert|
    alert.identifier  = RCAP.xpath_text(alert_xml_element, IDENTIFIER_XPATH, alert.xmlns)
    alert.sender      = RCAP.xpath_text(alert_xml_element, SENDER_XPATH, alert.xmlns)
    alert.sent        = RCAP.parse_datetime(RCAP.xpath_text(alert_xml_element, SENT_XPATH, alert.xmlns))
    alert.status      = RCAP.xpath_text(alert_xml_element, STATUS_XPATH, alert.xmlns)
    alert.msg_type    = RCAP.xpath_text(alert_xml_element, MSG_TYPE_XPATH, alert.xmlns)
    alert.source      = RCAP.xpath_text(alert_xml_element, SOURCE_XPATH, alert.xmlns)
    alert.scope       = RCAP.xpath_text(alert_xml_element, SCOPE_XPATH, alert.xmlns)
    alert.restriction = RCAP.xpath_text(alert_xml_element, RESTRICTION_XPATH, alert.xmlns)

    RCAP.unpack_if_given(RCAP.xpath_text(alert_xml_element, ADDRESSES_XPATH, alert.xmlns)).each do |address|
      alert.addresses << address.strip
    end

    RCAP.xpath_match(alert_xml_element, CODE_XPATH, alert.xmlns).each do |element|
      alert.codes << element.text.strip
    end

    alert.note = RCAP.xpath_text(alert_xml_element, NOTE_XPATH, alert.xmlns)

    RCAP.unpack_if_given(RCAP.xpath_text(alert_xml_element, REFERENCES_XPATH, alert.xmlns)).each do |reference|
      alert.references << reference.strip
    end

    RCAP.unpack_if_given(RCAP.xpath_text(alert_xml_element, INCIDENTS_XPATH, alert.xmlns)).each do |incident|
      alert.incidents << incident.strip
    end

    RCAP.xpath_match(alert_xml_element, Info::XPATH, alert.xmlns).each do |element|
      alert.infos << alert.info_class.from_xml_element(element)
    end
  end
end

.from_yaml(yaml) ⇒ Alert

Initialise an Alert object from a YAML string. Any object that is a subclass of IO (e.g. File) can be passed in.

Parameters:

Returns:


320
321
322
# File 'lib/rcap/base/alert.rb', line 320

def self.from_yaml(yaml)
  from_yaml_data(YAML.load(yaml))
end

.from_yaml_data(alert_yaml_data) ⇒ Alert

Initialise an Alert object from a hash reutrned from YAML.load.

Parameters:

  • alert_yaml_data (hash)

Returns:


328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/rcap/base/alert.rb', line 328

def self.from_yaml_data(alert_yaml_data)
  new do |alert|
    alert.identifier  = RCAP.strip_if_given(alert_yaml_data[ IDENTIFIER_YAML])
    alert.sender      = RCAP.strip_if_given(alert_yaml_data[ SENDER_YAML])
    alert.sent        = RCAP.parse_datetime(alert_yaml_data[ SENT_YAML])
    alert.status      = RCAP.strip_if_given(alert_yaml_data[ STATUS_YAML])
    alert.msg_type    = RCAP.strip_if_given(alert_yaml_data[ MSG_TYPE_YAML])
    alert.source      = RCAP.strip_if_given(alert_yaml_data[ SOURCE_YAML])
    alert.scope       = RCAP.strip_if_given(alert_yaml_data[ SCOPE_YAML])
    alert.restriction = RCAP.strip_if_given(alert_yaml_data[ RESTRICTION_YAML])
    Array(alert_yaml_data[ ADDRESSES_YAML]).each do |address|
      alert.addresses << address.strip
    end
    Array(alert_yaml_data[ CODES_YAML]).each do |code|
      alert.codes << code.strip
    end
    alert.note        = alert_yaml_data[ NOTE_YAML]
    Array(alert_yaml_data[ REFERENCES_YAML]).each do |reference|
      alert.references << reference.strip
    end
    Array(alert_yaml_data[ INCIDENTS_YAML]).each do |incident|
      alert.incidents << incident.strip
    end
    Array(alert_yaml_data[ INFOS_YAML]).each do |info_yaml_data|
      alert.infos <<  alert.info_class.from_yaml_data(info_yaml_data)
    end
  end
end

Instance Method Details

#add_info {|Info| ... } ⇒ Info

Creates a new Info object and adds it to the #infos array.

Yields:

  • (Info)

    The newly initialised Info object.

Returns:

  • (Info)

    The initialised Info object after being yielded to the block

See Also:


98
99
100
101
102
103
# File 'lib/rcap/base/alert.rb', line 98

def add_info
  info_class.new.tap do |info|
    yield(info) if block_given?
    @infos << info
  end
end

#inspectString

Returns:


182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/rcap/base/alert.rb', line 182

def inspect
  alert_inspect = ["CAP Version:  #{ self.class::CAP_VERSION }",
                   "Identifier:   #{ @identifier }",
                   "Sender:       #{ @sender }",
                   "Sent:         #{ @sent }",
                   "Status:       #{ @status }",
                   "Message Type: #{ @msg_type }",
                   "Source:       #{ @source }",
                   "Scope:        #{ @scope }",
                   "Restriction:  #{ @restriction }",
                   "Addresses:    #{ @addresses.to_s_for_cap }",
                   'Codes:',
                   @codes.map { |code| '  ' + code }.join("\n") + '',
                   "Note:         #{ @note }",
                   "References:",
                     @references.join("\n "),
                   "Incidents:    #{ @incidents.join(' ')}",
                   'Information:',
                   @infos.map { |info| '  ' + info.to_s }.join("\n")].join("\n")
  RCAP.format_lines_for_inspect('ALERT', alert_inspect)
end

#to_hHash

Returns a Hash representation of an Alert object

Returns:

  • (Hash)

376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/rcap/base/alert.rb', line 376

def to_h
  RCAP.attribute_values_to_hash([CAP_VERSION_KEY, self.class::CAP_VERSION],
                                [IDENTIFIER_KEY,  @identifier],
                                [SENDER_KEY,      @sender],
                                [SENT_KEY,        RCAP.to_s_for_cap(@sent)],
                                [STATUS_KEY,      @status],
                                [MSG_TYPE_KEY,    @msg_type],
                                [SOURCE_KEY,      @source],
                                [SCOPE_KEY,       @scope],
                                [RESTRICTION_KEY, @restriction],
                                [ADDRESSES_KEY,   @addresses],
                                [CODES_KEY,       @codes],
                                [NOTE_KEY,        @note],
                                [REFERENCES_KEY,  @references],
                                [INCIDENTS_KEY,   @incidents],
                                [INFOS_KEY,       @infos.map { |info| info.to_h  }])
end

#to_json(pretty_print = false) ⇒ String

Returns a JSON string representation of an Alert object

Parameters:

  • pretty_print (true, false) (defaults to: false)

Returns:


433
434
435
436
437
438
439
# File 'lib/rcap/base/alert.rb', line 433

def to_json(pretty_print = false)
  if pretty_print
    JSON.pretty_generate(to_h)
  else
    to_h.to_json
  end
end

#to_referenceString

Returns a string representation of the alert suitable for usage as a reference in a CAP message of the form

sender,identifier,sent

Returns:


177
178
179
# File 'lib/rcap/base/alert.rb', line 177

def to_reference
  "#{ @sender },#{ @identifier },#{ RCAP.to_s_for_cap(@sent)}"
end

#to_sString

Returns a string representation of the alert of the form

sender/identifier/sent

See #to_reference for another string representation suitable as a CAP reference.

Returns:


209
210
211
# File 'lib/rcap/base/alert.rb', line 209

def to_s
  "#{ @sender }/#{ @identifier }/#{ RCAP.to_s_for_cap(@sent)}"
end

#to_xml(pretty_print = false) ⇒ String

Returns a string containing the XML representation of the alert.

Parameters:

  • pretty_print (true, false) (defaults to: false)

    Pretty print output

Returns:


163
164
165
166
167
168
169
170
171
# File 'lib/rcap/base/alert.rb', line 163

def to_xml(pretty_print = false)
  if pretty_print
    xml_document = ''
    RCAP::XML_PRETTY_PRINTER.write(to_xml_document, xml_document)
    xml_document
  else
    to_xml_document.to_s
  end
end

#to_xml_documentREXML::Document

Returns:

  • (REXML::Document)

152
153
154
155
156
157
# File 'lib/rcap/base/alert.rb', line 152

def to_xml_document
  xml_document = REXML::Document.new
  xml_document.add(REXML::XMLDecl.new)
  xml_document.add(to_xml_element)
  xml_document
end

#to_xml_elementREXML::Element

Returns:

  • (REXML::Element)

121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/rcap/base/alert.rb', line 121

def to_xml_element
  xml_element = REXML::Element.new(XML_ELEMENT_NAME)
  xml_element.add_namespace(self.class::XMLNS)
  xml_element.add_element(IDENTIFIER_ELEMENT_NAME).add_text(@identifier)   if @identifier
  xml_element.add_element(SENDER_ELEMENT_NAME).add_text(@sender)           if @sender
  xml_element.add_element(SENT_ELEMENT_NAME).add_text(@sent.to_s_for_cap)  if @sent
  xml_element.add_element(STATUS_ELEMENT_NAME).add_text(@status)           if @status
  xml_element.add_element(MSG_TYPE_ELEMENT_NAME).add_text(@msg_type)       if @msg_type
  xml_element.add_element(SOURCE_ELEMENT_NAME).add_text(@source)           if @source
  xml_element.add_element(SCOPE_ELEMENT_NAME).add_text(@scope)             if @scope
  xml_element.add_element(RESTRICTION_ELEMENT_NAME).add_text(@restriction) if @restriction
  if @addresses.any?
    xml_element.add_element(ADDRESSES_ELEMENT_NAME).add_text(@addresses.to_s_for_cap)
  end
  @codes.each do |code|
    xml_element.add_element(CODE_ELEMENT_NAME).add_text(code)
  end
  xml_element.add_element(NOTE_ELEMENT_NAME).add_text(@note) if @note
  if @references.any?
    xml_element.add_element(REFERENCES_ELEMENT_NAME).add_text(@references.join(' '))
  end
  if @incidents.any?
    xml_element.add_element(INCIDENTS_ELEMENT_NAME).add_text(@incidents.join(' '))
  end
  @infos.each do |info|
    xml_element.add_element(info.to_xml_element)
  end
  xml_element
end

#to_yamlString

Returns a string containing the YAML representation of the alert.

Returns:


298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/rcap/base/alert.rb', line 298

def to_yaml(options = {})
  RCAP.attribute_values_to_hash([CAP_VERSION_YAML, self.class::CAP_VERSION],
                                [IDENTIFIER_YAML,  @identifier],
                                [SENDER_YAML,      @sender],
                                [SENT_YAML,        @sent],
                                [STATUS_YAML,      @status],
                                [MSG_TYPE_YAML,    @msg_type],
                                [SOURCE_YAML,      @source],
                                [SCOPE_YAML,       @scope],
                                [RESTRICTION_YAML, @restriction],
                                [ADDRESSES_YAML,   @addresses],
                                [CODES_YAML,       @codes],
                                [NOTE_YAML,        @note],
                                [REFERENCES_YAML,  @references],
                                [INCIDENTS_YAML,   @incidents],
                                [INFOS_YAML,       @infos.map(&:to_yaml_data)]).to_yaml(options)
end