Class: Alert

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
RCAP, Validation
Defined in:
lib/generators/rcap/models/templates/models/alert.rb

Overview

An Alert object is valid if

  • it has an identifier

  • it has a sender

  • it has a sent time

  • it has a valid status value

  • it has a valid messge type value

  • it has a valid scope value

  • all Info objects contained in infos are valid

Constant Summary collapse

STATUS_ACTUAL =

:nodoc:

"Actual"
STATUS_EXERCISE =

:nodoc:

"Exercise"
STATUS_SYSTEM =

:nodoc:

"System"
STATUS_TEST =

:nodoc:

"Test"
STATUS_DRAFT =

:nodoc:

"Draft"
VALID_STATUSES =

Valid values for status

[ STATUS_ACTUAL, STATUS_EXERCISE, STATUS_SYSTEM, STATUS_TEST, STATUS_DRAFT ]
MSG_TYPE_ALERT =

:nodoc:

"Alert"
MSG_TYPE_UPDATE =

:nodoc:

"Update"
MSG_TYPE_CANCEL =

:nodoc:

"Cancel"
MSG_TYPE_ACK =

:nodoc:

"Ack"
MSG_TYPE_ERROR =

:nodoc:

"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 =

:nodoc:

"Public"
SCOPE_RESTRICTED =

:nodoc:

"Restricted"
SCOPE_PRIVATE =

:nodoc:

"Private"
VALID_SCOPES =

Valid values for scope

[ SCOPE_PUBLIC, SCOPE_PRIVATE, SCOPE_RESTRICTED ]
XML_ELEMENT_NAME =

:nodoc:

'alert'
IDENTIFIER_ELEMENT_NAME =

:nodoc:

'identifier'
SENDER_ELEMENT_NAME =

:nodoc:

'sender'
SENT_ELEMENT_NAME =

:nodoc:

'sent'
STATUS_ELEMENT_NAME =

:nodoc:

'status'
MSG_TYPE_ELEMENT_NAME =

:nodoc:

'msgType'
SOURCE_ELEMENT_NAME =

:nodoc:

'source'
SCOPE_ELEMENT_NAME =

:nodoc:

'scope'
RESTRICTION_ELEMENT_NAME =

:nodoc:

'restriction'
ADDRESSES_ELEMENT_NAME =

:nodoc:

'addresses'
CODE_ELEMENT_NAME =

:nodoc:

'code'
NOTE_ELEMENT_NAME =

:nodoc:

'note'
REFERENCES_ELEMENT_NAME =

:nodoc:

'references'
INCIDENTS_ELEMENT_NAME =

:nodoc:

'incidents'
XPATH =

:nodoc:

'cap:alert'
IDENTIFIER_XPATH =

:nodoc:

"cap:#{ IDENTIFIER_ELEMENT_NAME }"
SENDER_XPATH =

:nodoc:

"cap:#{ SENDER_ELEMENT_NAME }"
SENT_XPATH =

:nodoc:

"cap:#{ SENT_ELEMENT_NAME }"
STATUS_XPATH =

:nodoc:

"cap:#{ STATUS_ELEMENT_NAME }"
MSG_TYPE_XPATH =

:nodoc:

"cap:#{ MSG_TYPE_ELEMENT_NAME }"
SOURCE_XPATH =

:nodoc:

"cap:#{ SOURCE_ELEMENT_NAME }"
SCOPE_XPATH =

:nodoc:

"cap:#{ SCOPE_ELEMENT_NAME }"
RESTRICTION_XPATH =

:nodoc:

"cap:#{ RESTRICTION_ELEMENT_NAME }"
ADDRESSES_XPATH =

:nodoc:

"cap:#{ ADDRESSES_ELEMENT_NAME }"
CODE_XPATH =

:nodoc:

"cap:#{ CODE_ELEMENT_NAME }"
NOTE_XPATH =

:nodoc:

"cap:#{ NOTE_ELEMENT_NAME }"
REFERENCES_XPATH =

:nodoc:

"cap:#{ REFERENCES_ELEMENT_NAME }"
INCIDENTS_XPATH =

:nodoc:

"cap:#{ INCIDENTS_ELEMENT_NAME }"
CAP_VERSION_YAML =

:nodoc:

"CAP Version"
IDENTIFIER_YAML =

:nodoc:

"Identifier"
SENDER_YAML =

:nodoc:

"Sender"
SENT_YAML =

:nodoc:

"Sent"
STATUS_YAML =

:nodoc:

"Status"
MSG_TYPE_YAML =

:nodoc:

"Message Type"
SOURCE_YAML =

:nodoc:

"Source"
SCOPE_YAML =

:nodoc:

"Scope"
RESTRICTION_YAML =

:nodoc:

"Restriction"
ADDRESSES_YAML =

:nodoc:

"Addresses"
CODE_YAML =

:nodoc:

"Code"
NOTE_YAML =

:nodoc:

"Note"
REFERENCES_YAML =

:nodoc:

"References"
INCIDENTS_YAML =

:nodoc:

"Incidents"
INFOS_YAML =

:nodoc:

"Information"
CAP_VERSION_KEY =

:nodoc:

'cap_version'
IDENTIFIER_KEY =

:nodoc:

'identifier'
SENDER_KEY =

:nodoc:

'sender'
SENT_KEY =

:nodoc:

'sent'
STATUS_KEY =

:nodoc:

'status'
MSG_TYPE_KEY =

:nodoc:

'msg_type'
SOURCE_KEY =

:nodoc:

'source'
SCOPE_KEY =

:nodoc:

'scope'
RESTRICTION_KEY =

:nodoc:

'restriction'
ADDRESSES_KEY =

:nodoc:

'addresses'
CODE_KEY =

:nodoc:

'code'
NOTE_KEY =

:nodoc:

'note'
REFERENCES_KEY =

:nodoc:

'references'
INCIDENTS_KEY =

:nodoc:

'incidents'
INFOS_KEY =

:nodoc:

'infos'

Constants included from RCAP

RCAP::CAP_VERSION, RCAP::VERSION, RCAP::XMLNS

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_h(alert_hash) ⇒ Object

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



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/generators/rcap/models/templates/models/alert.rb', line 327

def self.from_h( alert_hash )
  self.new(
    :identifier  => alert_hash[ IDENTIFIER_KEY ],
    :sender      => alert_hash[ SENDER_KEY ],
    :sent_at     => RCAP.parse_datetime( alert_hash[ SENT_KEY ]),
    :status      => alert_hash[ STATUS_KEY ],
    :msg_type    => alert_hash[ MSG_TYPE_KEY ],
    :source      => alert_hash[ SOURCE_KEY ],
    :scope       => alert_hash[ SCOPE_KEY ],
    :restriction => alert_hash[ RESTRICTION_KEY ],
    :addresses   => alert_hash[ ADDRESSES_KEY ],
    :code        => alert_hash[ CODE_KEY ],
    :note        => alert_hash[ NOTE_KEY ],
    :references  => alert_hash[ REFERENCES_KEY ],
    :incidents   => alert_hash[ INCIDENTS_KEY ]
    # :infos       => Array( alert_hash[ INFOS_KEY ]).map{ |info_hash| RCAP::Info.from_h( info_hash )}
  )
end

.from_json(json_string) ⇒ Object

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



352
353
354
# File 'lib/generators/rcap/models/templates/models/alert.rb', line 352

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

.from_xml(xml) ⇒ Object

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



226
227
228
# File 'lib/generators/rcap/models/templates/models/alert.rb', line 226

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

.from_xml_document(xml_document) ⇒ Object

:nodoc:



221
222
223
# File 'lib/generators/rcap/models/templates/models/alert.rb', line 221

def self.from_xml_document( xml_document ) # :nodoc:
  self.from_xml_element( xml_document.root )
end

.from_xml_element(alert_xml_element) ⇒ Object

:nodoc:



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/generators/rcap/models/templates/models/alert.rb', line 203

def self.from_xml_element( alert_xml_element ) # :nodoc:
  self.new( :identifier  => RCAP.xpath_text( alert_xml_element, RCAP::Alert::IDENTIFIER_XPATH ),
            :sender      => RCAP.xpath_text( alert_xml_element, SENDER_XPATH ),
            :sent_at     => (( sent = RCAP.xpath_first( alert_xml_element, SENT_XPATH )) ? DateTime.parse( sent.text ) : nil ),
            :status      => RCAP.xpath_text( alert_xml_element, STATUS_XPATH ),
            :msg_type    => RCAP.xpath_text( alert_xml_element, MSG_TYPE_XPATH ),
            :source      => RCAP.xpath_text( alert_xml_element, SOURCE_XPATH ),
            :scope       => RCAP.xpath_text( alert_xml_element, SCOPE_XPATH ),
            :restriction => RCAP.xpath_text( alert_xml_element, RESTRICTION_XPATH ),
            :addresses   => (( address = RCAP.xpath_text( alert_xml_element, ADDRESSES_XPATH )) ? address.unpack_cap_list : nil ),
            :code        => RCAP.xpath_text( alert_xml_element, CODE_XPATH ),
            :note        => RCAP.xpath_text( alert_xml_element, NOTE_XPATH ),
            :references  => (( references = RCAP.xpath_text( alert_xml_element, REFERENCES_XPATH )) ? references.split( ' ' ) : nil ),
            :incidents   => (( incidents = RCAP.xpath_text( alert_xml_element, INCIDENTS_XPATH )) ? incidents.split( ' ' ) : nil )
            # :infos       => RCAP.xpath_match( alert_xml_element, RCAP::Info::XPATH ).map{ |element| RCAP::Info.from_xml_element( element )}
  )
end

.from_yaml(yaml) ⇒ Object

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



268
269
270
# File 'lib/generators/rcap/models/templates/models/alert.rb', line 268

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

.from_yaml_data(alert_yaml_data) ⇒ Object

:nodoc:



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/generators/rcap/models/templates/models/alert.rb', line 272

def self.from_yaml_data( alert_yaml_data ) # :nodoc:
  Alert.new(
    :identifier  => alert_yaml_data[ IDENTIFIER_YAML ],
    :sender      => alert_yaml_data[ SENDER_YAML ],
    :sent_at     => ( sent = alert_yaml_data[ SENT_YAML ]).blank? ? nil : DateTime.parse( sent.to_s ),
    :status      => alert_yaml_data[ STATUS_YAML ],
    :msg_type    => alert_yaml_data[ MSG_TYPE_YAML ],
    :source      => alert_yaml_data[ SOURCE_YAML ],
    :scope       => alert_yaml_data[ SCOPE_YAML ],
    :restriction => alert_yaml_data[ RESTRICTION_YAML ],
    :addresses   => alert_yaml_data[ ADDRESSES_YAML ],
    :code        => alert_yaml_data[ CODE_YAML ],
    :note        => alert_yaml_data[ NOTE_YAML ],
    :references  => alert_yaml_data[ REFERENCES_YAML ],
    :incidents   => alert_yaml_data[ INCIDENTS_YAML ]
    # :infos       => Array( alert_yaml_data[ INFOS_YAML ]).map{ |info_yaml_data| RCAP::Info.from_yaml_data( info_yaml_data )}
  )
end

Instance Method Details

#inspectObject

:nodoc:



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/generators/rcap/models/templates/models/alert.rb', line 174

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

#to_hObject

Returns a Hash representation of an Alert object



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/generators/rcap/models/templates/models/alert.rb', line 308

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

#to_jsonObject

Returns a JSON string representation of an Alert object



347
348
349
# File 'lib/generators/rcap/models/templates/models/alert.rb', line 347

def to_json
  self.to_h.to_json
end

#to_referenceObject

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

sender,identifier,sent


170
171
172
# File 'lib/generators/rcap/models/templates/models/alert.rb', line 170

def to_reference
  "#{ self.sender },#{ self.identifier },#{ self.sent }"
end

#to_sObject

Returns a string representation of the alert of the form

sender/identifier/sent

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



199
200
201
# File 'lib/generators/rcap/models/templates/models/alert.rb', line 199

def to_s
  "#{ self.sender }/#{ self.identifier }/#{ self.sent }"
end

#to_xmlObject

Returns a string containing the XML representation of the alert.



164
165
166
# File 'lib/generators/rcap/models/templates/models/alert.rb', line 164

def to_xml
  self.to_xml_document.to_s
end

#to_xml_documentObject

:nodoc:



156
157
158
159
160
161
# File 'lib/generators/rcap/models/templates/models/alert.rb', line 156

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

#to_xml_elementObject

def initialize( attributes = {})

attributes = {} unless attributes
@identifier  = attributes[ :identifier ] || UUIDTools::UUID.random_create.to_s
@sender      = attributes[ :sender ]
@sent        = attributes[ :sent ] || DateTime.now
@status      = attributes[ :status ]
@msg_type    = attributes[ :msg_type ]
@scope       = attributes[ :scope ]
@source      = attributes[ :source ]
@restriction = attributes[ :restriction ]
@addresses   = Array( attributes[ :addresses ])
@references  = Array( attributes[ :references ])
@incidents   = Array( attributes[ :incidents ])
@infos       = Array( attributes[ :infos ])

end



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/generators/rcap/models/templates/models/alert.rb', line 128

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

#to_yaml(options = {}) ⇒ Object

Returns a string containing the YAML representation of the alert.



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/generators/rcap/models/templates/models/alert.rb', line 247

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