Module: RCAP::Alert

Defined in:
lib/rcap/alert.rb

Constant Summary collapse

XMLNS_KEY =
'xmlns'
YAML_CAP_VERSION_KEY =
'CAP Version'
JSON_CAP_VERSION_KEY =
'cap_version'
CAP_NAMESPACES =
[RCAP::CAP_1_0::Alert::XMLNS, RCAP::CAP_1_1::Alert::XMLNS, RCAP::CAP_1_2::Alert::XMLNS]

Class Method Summary collapse

Class Method Details

.from_h(hash) ⇒ RCAP::CAP_1_0::Alert, ...

Initialise a RCAP Alert from a Ruby hash produced by CAP_1_2::Alert#to_h. The cap_version key is inspected and a CAP 1.0, 1.1 or 1.2 Alert is instantiated accordingly.

Parameters:

  • hash (Hash)

    Alert as a Ruby hash.

Returns:


68
69
70
71
72
73
74
75
76
77
# File 'lib/rcap/alert.rb', line 68

def self.from_h(hash)
  case hash[JSON_CAP_VERSION_KEY]
  when CAP_1_0::Alert::CAP_VERSION
    CAP_1_0::Alert.from_h(hash)
  when CAP_1_1::Alert::CAP_VERSION
    CAP_1_1::Alert.from_h(hash)
  else
    CAP_1_2::Alert.from_h(hash)
  end
end

.from_json(json) ⇒ RCAP::CAP_1_0::Alert, ...

Initialise a RCAP Alert from a JSON document produced by CAP_1_2::Alert#to_json. The version of the document is inspected and a CAP 1.0, 1.1 or 1.2 Alert is instantiated accordingly.

Parameters:

  • json (#to_s)

    Alert in JSON format

Returns:


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

def self.from_json(json)
  json_hash = JSON.parse(json.to_s)
  from_h(json_hash)
end

.from_xml(xml, namespace_key = nil) ⇒ RCAP::CAP_1_0::Alert, ...

Initialise a RCAP Alert from a XML document.

Parameters:

  • xml (IO, String)

    CAP alert in XML format. Can be a String or any IO object.

  • namespace_key (String) (defaults to: nil)

    The XML namespace that the CAP alert is in. If omitted the namespace of the document is inspected and a CAP_1_0::Alert, CAP_1_1::Alert or CAP_1_2::Alert is instantiated accordingly. If no namespace can be detected a CAP 1.2 message will be assumed.

Returns:


17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rcap/alert.rb', line 17

def self.from_xml(xml, namespace_key = nil)
  xml_document = REXML::Document.new(xml)
  document_namespaces = xml_document.root.namespaces.invert
  namespace = namespace_key || CAP_NAMESPACES.find { |namepsace| document_namespaces[namepsace] }

  case namespace
  when CAP_1_0::Alert::XMLNS
    CAP_1_0::Alert.from_xml_document(xml_document)
  when CAP_1_1::Alert::XMLNS
    CAP_1_1::Alert.from_xml_document(xml_document)
  else
    CAP_1_2::Alert.from_xml_document(xml_document)
  end
end

.from_yaml(yaml) ⇒ RCAP::CAP_1_0::Alert, ...

Initialise a RCAP Alert from a YAML document produced by CAP_1_2::Alert#to_yaml. The version of the document is inspected and a CAP 1.0, 1.1 or 1.2 Alert is instantiated accordingly.

Parameters:

  • yaml (IO, String)

    CAP Alert in YAML format. Can be a String or any IO object.

Returns:


38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rcap/alert.rb', line 38

def self.from_yaml(yaml)
  yaml_data = YAML.load(yaml)

  case yaml_data[YAML_CAP_VERSION_KEY]
  when CAP_1_0::Alert::CAP_VERSION
    CAP_1_0::Alert.from_yaml_data(yaml_data)
  when CAP_1_1::Alert::CAP_VERSION
    CAP_1_1::Alert.from_yaml_data(yaml_data)
  else
    CAP_1_2::Alert.from_yaml_data(yaml_data)
  end
end