Class RCAP::CAP_1_1::Alert

  1. lib/rcap/cap_1_1/alert.rb
Parent: Object

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

Included modules

  1. Validation

Constants

XMLNS = "urn:oasis:names:tc:emergency:cap:1.1"
CAP_VERSION = "1.1"
VALID_STATUSES = [ STATUS_ACTUAL, STATUS_EXERCISE, STATUS_SYSTEM, STATUS_TEST, STATUS_DRAFT ]   Valid values for status
VALID_MSG_TYPES = [ MSG_TYPE_ALERT, MSG_TYPE_UPDATE, MSG_TYPE_CANCEL, MSG_TYPE_ACK, MSG_TYPE_ERROR ]   Valid values for msg_type
VALID_SCOPES = [ SCOPE_PUBLIC, SCOPE_PRIVATE, SCOPE_RESTRICTED ]   Valid values for scope

Attributes

addresses [R] Collection of address strings. Depends on scope being SCOPE_PRIVATE.
codes [R]
identifier [RW] If not set a UUID will be set by default
incidents [R] Collection of incident strings
infos [R] Collection of Info objects
msg_type [RW] Value can only be one of VALID_MSG_TYPES
note [RW]
references [R] Collection of reference strings - see Alert#to_reference
restriction [RW] Depends on scope being SCOPE_RESTRICTED.
scope [RW] Value can only be one of VALID_SCOPES
sender [RW]
sent [RW] Sent Time - If not set will value will be time of creation.
source [RW]
status [RW] Value can only be one of VALID_STATUSES

Public class methods

from_h ( alert_hash )

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

[show source]
# File lib/rcap/cap_1_1/alert.rb, line 338
      def self.from_h( alert_hash )
        self.new(
          :identifier  => alert_hash[ IDENTIFIER_KEY ],
          :sender      => alert_hash[ SENDER_KEY ],
          :sent        => 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 ],
          :codes       => alert_hash[ CODES_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| Info.from_h( info_hash )})
      end
from_json ( json_string )

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

[show source]
# File lib/rcap/cap_1_1/alert.rb, line 366
      def self.from_json( json_string )
        self.from_h( JSON.parse( json_string ))
      end
from_xml ( xml )

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

[show source]
# File lib/rcap/cap_1_1/alert.rb, line 237
      def self.from_xml( xml )
        self.from_xml_document( REXML::Document.new( xml ))
      end
from_yaml ( yaml )

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

[show source]
# File lib/rcap/cap_1_1/alert.rb, line 279
      def self.from_yaml( yaml )
        self.from_yaml_data( YAML.load( yaml ))
      end
new ( attributes = {})
[show source]
# File lib/rcap/cap_1_1/alert.rb, line 109
      def initialize( attributes = {})
        @identifier  = attributes[ :identifier ] || RCAP.generate_identifier
        @sender      = attributes[ :sender ]
        @sent        = attributes[ :sent ]
        @status      = attributes[ :status ]
        @msg_type    = attributes[ :msg_type ]
        @scope       = attributes[ :scope ]
        @source      = attributes[ :source ]
        @restriction = attributes[ :restriction ]
        @addresses   = Array( attributes[ :addresses ])
        @codes       = Array( attributes[ :codes ])
        @references  = Array( attributes[ :references ])
        @incidents   = Array( attributes[ :incidents ])
        @infos       = Array( attributes[ :infos ])
      end

Public instance methods

add_info ( info_attributes = {})

Creates a new Info object and adds it to the infos array. The info_attributes are passed as a parameter to Info.new.

[show source]
# File lib/rcap/cap_1_1/alert.rb, line 127
      def add_info( info_attributes  = {})
        info = Info.new( info_attributes )
        self.infos << info
        info
      end
to_h ()

Returns a Hash representation of an Alert object

[show source]
# File lib/rcap/cap_1_1/alert.rb, line 319
      def to_h
        RCAP.attribute_values_to_hash( [ CAP_VERSION_KEY, 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 ],
                                       [ CODES_KEY,       self.codes ],
                                       [ NOTE_KEY,        self.note ],
                                       [ REFERENCES_KEY,  self.references ],
                                       [ INCIDENTS_KEY,   self.incidents ],
                                       [ INFOS_KEY,       self.infos.map{ |info| info.to_h  }])
      end
to_json ( pretty_print = false )

Returns a JSON string representation of an Alert object

[show source]
# File lib/rcap/cap_1_1/alert.rb, line 357
      def to_json( pretty_print = false )
        if pretty_print
          JSON.pretty_generate( self.to_h )
        else
          self.to_h.to_json
        end
      end
to_reference ()

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

sender,identifier,sent
[show source]
# File lib/rcap/cap_1_1/alert.rb, line 183
      def to_reference
        "#{ self.sender },#{ self.identifier },#{ self.sent }"
      end
to_s ()

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.

[show source]
# File lib/rcap/cap_1_1/alert.rb, line 211
      def to_s
        "#{ self.sender }/#{ self.identifier }/#{ self.sent }"
      end
to_xml ( pretty_print = false )

Returns a string containing the XML representation of the alert.

[show source]
# File lib/rcap/cap_1_1/alert.rb, line 171
      def to_xml( pretty_print = false )
        if pretty_print
          xml_document = ""
          XML_PRETTY_PRINTER.write( self.to_xml_document, xml_document )
          xml_document
        else
          self.to_xml_document.to_s
        end
      end
to_yaml ( options = {} )

Returns a string containing the YAML representation of the alert.

[show source]
# File lib/rcap/cap_1_1/alert.rb, line 258
      def to_yaml( options = {} )
        RCAP.attribute_values_to_hash(
          [ CAP_VERSION_YAML,  CAP_VERSION ],
          [ IDENTIFIER_YAML,    self.identifier ],
          [ SENDER_YAML,        self.sender ],
          [ SENT_YAML,          self.sent ],
          [ 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 ],
          [ CODES_YAML,         self.codes ],
          [ NOTE_YAML,          self.note ],
          [ REFERENCES_YAML,    self.references ],
          [ INCIDENTS_YAML,     self.incidents ],
          [ INFOS_YAML,         self.infos ]
        ).to_yaml( options )
      end