rcap 2.4.1 → 2.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +4 -0
  3. data/lib/rcap/alert.rb +28 -29
  4. data/lib/rcap/base/alert.rb +192 -192
  5. data/lib/rcap/base/area.rb +84 -86
  6. data/lib/rcap/base/circle.rb +32 -32
  7. data/lib/rcap/base/info.rb +248 -253
  8. data/lib/rcap/base/parameter.rb +23 -24
  9. data/lib/rcap/base/point.rb +23 -23
  10. data/lib/rcap/base/polygon.rb +32 -32
  11. data/lib/rcap/base/resource.rb +57 -57
  12. data/lib/rcap/cap_1_0/alert.rb +79 -83
  13. data/lib/rcap/cap_1_0/area.rb +0 -2
  14. data/lib/rcap/cap_1_0/circle.rb +0 -1
  15. data/lib/rcap/cap_1_0/info.rb +1 -3
  16. data/lib/rcap/cap_1_0/parameter.rb +11 -12
  17. data/lib/rcap/cap_1_0/point.rb +0 -1
  18. data/lib/rcap/cap_1_0/polygon.rb +0 -2
  19. data/lib/rcap/cap_1_0/resource.rb +0 -3
  20. data/lib/rcap/cap_1_1/alert.rb +4 -6
  21. data/lib/rcap/cap_1_1/area.rb +0 -2
  22. data/lib/rcap/cap_1_1/circle.rb +0 -1
  23. data/lib/rcap/cap_1_1/info.rb +120 -129
  24. data/lib/rcap/cap_1_1/parameter.rb +0 -2
  25. data/lib/rcap/cap_1_1/point.rb +0 -1
  26. data/lib/rcap/cap_1_1/polygon.rb +0 -2
  27. data/lib/rcap/cap_1_1/resource.rb +32 -34
  28. data/lib/rcap/cap_1_2/alert.rb +4 -6
  29. data/lib/rcap/cap_1_2/area.rb +0 -2
  30. data/lib/rcap/cap_1_2/circle.rb +0 -1
  31. data/lib/rcap/cap_1_2/info.rb +114 -121
  32. data/lib/rcap/cap_1_2/parameter.rb +0 -2
  33. data/lib/rcap/cap_1_2/point.rb +0 -1
  34. data/lib/rcap/cap_1_2/polygon.rb +1 -3
  35. data/lib/rcap/cap_1_2/resource.rb +32 -34
  36. data/lib/rcap/config.rb +3 -3
  37. data/lib/rcap/custom_validators.rb +72 -76
  38. data/lib/rcap/extensions/array.rb +1 -1
  39. data/lib/rcap/extensions/date.rb +2 -2
  40. data/lib/rcap/extensions/date_time.rb +3 -3
  41. data/lib/rcap/extensions/string.rb +3 -3
  42. data/lib/rcap/extensions/time.rb +2 -3
  43. data/lib/rcap/info.rb +4 -5
  44. data/lib/rcap/utilities.rb +24 -25
  45. data/lib/rcap/validation.rb +19 -20
  46. data/lib/rcap/version.rb +1 -1
  47. data/spec/alert_spec.rb +141 -141
  48. data/spec/cap_1_0/alert_spec.rb +92 -94
  49. data/spec/cap_1_0/area_spec.rb +86 -86
  50. data/spec/cap_1_0/circle_spec.rb +39 -39
  51. data/spec/cap_1_0/event_code_spec.rb +15 -15
  52. data/spec/cap_1_0/geocode_spec.rb +16 -16
  53. data/spec/cap_1_0/info_spec.rb +175 -177
  54. data/spec/cap_1_0/parameter_spec.rb +27 -27
  55. data/spec/cap_1_0/point_spec.rb +18 -18
  56. data/spec/cap_1_0/polygon_spec.rb +38 -38
  57. data/spec/cap_1_0/resource_spec.rb +63 -64
  58. data/spec/cap_1_1/alert_spec.rb +107 -109
  59. data/spec/cap_1_1/area_spec.rb +90 -90
  60. data/spec/cap_1_1/circle_spec.rb +34 -34
  61. data/spec/cap_1_1/event_code_spec.rb +15 -16
  62. data/spec/cap_1_1/geocode_spec.rb +16 -16
  63. data/spec/cap_1_1/info_spec.rb +194 -196
  64. data/spec/cap_1_1/parameter_spec.rb +17 -17
  65. data/spec/cap_1_1/point_spec.rb +18 -18
  66. data/spec/cap_1_1/polygon_spec.rb +36 -36
  67. data/spec/cap_1_1/resource_spec.rb +106 -107
  68. data/spec/cap_1_2/alert_spec.rb +98 -99
  69. data/spec/cap_1_2/area_spec.rb +90 -90
  70. data/spec/cap_1_2/circle_spec.rb +43 -43
  71. data/spec/cap_1_2/event_code_spec.rb +19 -20
  72. data/spec/cap_1_2/geocode_spec.rb +20 -20
  73. data/spec/cap_1_2/info_spec.rb +196 -198
  74. data/spec/cap_1_2/parameter_spec.rb +19 -19
  75. data/spec/cap_1_2/point_spec.rb +21 -21
  76. data/spec/cap_1_2/polygon_spec.rb +47 -47
  77. data/spec/cap_1_2/resource_spec.rb +96 -97
  78. data/spec/extensions_spec.rb +29 -29
  79. data/spec/info_spec.rb +15 -15
  80. data/spec/spec_helper.rb +3 -3
  81. data/spec/validations_spec.rb +73 -73
  82. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7884d08e11ec38241caeca586cff0238fb81bd54
4
- data.tar.gz: b2ab2aa464a5d69917b75bf7f3550a2c0d795f0a
3
+ metadata.gz: 2b2493782c58861730c554776d6e5848e9b15728
4
+ data.tar.gz: ab89552659a96fc8130ff839734c61a255403030
5
5
  SHA512:
6
- metadata.gz: 8dc12b2de19db14730392366b7e6b6e27a22158e939d28582aef7e7ac81691ae8bea37b83ba79aa8a0f6360f603c4d10a6101c8b8481a894b25f76e8fd15bf4c
7
- data.tar.gz: 2027150b43a529be6ab6c778bf39b8d4ff4939894fa443cfa428c946a2749b247d27897c0ebdea04997ea3bf27b8303666b66e13ba1cd728fa1ccdce3d37202d
6
+ metadata.gz: c27d24fa3f957feb2f36ef8400d33a741c6e163ab79508a7c614f5dbaf7daf051bc81072132c04f985c31529fef475676165dddd4a386df5dc96b3e249e8cf00
7
+ data.tar.gz: d220753d7b59abb718ce149ba7e25fe74e12bd7bf0b73b9b3f8e0170a801d0d476ba596ec66c879eb3d5051268e371c3d8ba6e97fa55b0ab6ce130fa1c9dc15d
@@ -1,6 +1,10 @@
1
1
  Change Log
2
2
  ==========
3
3
 
4
+ ## 2.5.0 - 3 November 2014
5
+
6
+ * Allow the values of parameters, area codes and event codes to be empty.
7
+
4
8
  ## 2.4.1 - 7 August 2013
5
9
 
6
10
  * Further integration of validations.
@@ -1,11 +1,10 @@
1
1
  module RCAP
2
2
  module Alert
3
+ XMLNS_KEY = 'xmlns'
4
+ YAML_CAP_VERSION_KEY = 'CAP Version'
5
+ JSON_CAP_VERSION_KEY = 'cap_version'
3
6
 
4
- XMLNS_KEY = "xmlns"
5
- YAML_CAP_VERSION_KEY = "CAP Version"
6
- JSON_CAP_VERSION_KEY = "cap_version"
7
-
8
- CAP_NAMESPACES = [ RCAP::CAP_1_0::Alert::XMLNS, RCAP::CAP_1_1::Alert::XMLNS, RCAP::CAP_1_2::Alert::XMLNS ]
7
+ CAP_NAMESPACES = [RCAP::CAP_1_0::Alert::XMLNS, RCAP::CAP_1_1::Alert::XMLNS, RCAP::CAP_1_2::Alert::XMLNS]
9
8
 
10
9
  # Initialise a RCAP Alert from a XML document.
11
10
  #
@@ -14,19 +13,19 @@ module RCAP
14
13
  # the namespace of the document is inspected and a CAP_1_0::Alert, CAP_1_1::Alert
15
14
  # or CAP_1_2::Alert is instantiated accordingly. If no namespace can be detected
16
15
  # a CAP 1.2 message will be assumed.
17
- # @return [ RCAP::CAP_1_0::Alert, RCAP::CAP_1_1::Alert, RCAP::CAP_1_2::Alert ]
18
- def self.from_xml( xml, namespace_key = nil )
19
- xml_document = REXML::Document.new( xml )
16
+ # @return [RCAP::CAP_1_0::Alert, RCAP::CAP_1_1::Alert, RCAP::CAP_1_2::Alert ]
17
+ def self.from_xml(xml, namespace_key = nil)
18
+ xml_document = REXML::Document.new(xml)
20
19
  document_namespaces = xml_document.root.namespaces.invert
21
- namespace = namespace_key || CAP_NAMESPACES.find{ |namepsace| document_namespaces[ namepsace ]}
20
+ namespace = namespace_key || CAP_NAMESPACES.find { |namepsace| document_namespaces[namepsace] }
22
21
 
23
22
  case namespace
24
23
  when CAP_1_0::Alert::XMLNS
25
- CAP_1_0::Alert.from_xml_document( xml_document )
24
+ CAP_1_0::Alert.from_xml_document(xml_document)
26
25
  when CAP_1_1::Alert::XMLNS
27
- CAP_1_1::Alert.from_xml_document( xml_document )
26
+ CAP_1_1::Alert.from_xml_document(xml_document)
28
27
  else
29
- CAP_1_2::Alert.from_xml_document( xml_document )
28
+ CAP_1_2::Alert.from_xml_document(xml_document)
30
29
  end
31
30
  end
32
31
 
@@ -35,17 +34,17 @@ module RCAP
35
34
  # CAP 1.0, 1.1 or 1.2 Alert is instantiated accordingly.
36
35
  #
37
36
  # @param [IO, String] yaml CAP Alert in YAML format. Can be a String or any IO object.
38
- # @return [ RCAP::CAP_1_0::Alert, RCAP::CAP_1_1::Alert, RCAP::CAP_1_2::Alert ]
39
- def self.from_yaml( yaml )
40
- yaml_data = YAML.load( yaml )
37
+ # @return [RCAP::CAP_1_0::Alert, RCAP::CAP_1_1::Alert, RCAP::CAP_1_2::Alert ]
38
+ def self.from_yaml(yaml)
39
+ yaml_data = YAML.load(yaml)
41
40
 
42
- case yaml_data[ YAML_CAP_VERSION_KEY ]
41
+ case yaml_data[YAML_CAP_VERSION_KEY]
43
42
  when CAP_1_0::Alert::CAP_VERSION
44
- CAP_1_0::Alert.from_yaml_data( yaml_data )
43
+ CAP_1_0::Alert.from_yaml_data(yaml_data)
45
44
  when CAP_1_1::Alert::CAP_VERSION
46
- CAP_1_1::Alert.from_yaml_data( yaml_data )
45
+ CAP_1_1::Alert.from_yaml_data(yaml_data)
47
46
  else
48
- CAP_1_2::Alert.from_yaml_data( yaml_data )
47
+ CAP_1_2::Alert.from_yaml_data(yaml_data)
49
48
  end
50
49
  end
51
50
 
@@ -54,10 +53,10 @@ module RCAP
54
53
  # CAP 1.0, 1.1 or 1.2 Alert is instantiated accordingly.
55
54
  #
56
55
  # @param [#to_s] json Alert in JSON format
57
- # @return [ RCAP::CAP_1_0::Alert, RCAP::CAP_1_1::Alert, RCAP::CAP_1_2::Alert ]
58
- def self.from_json( json )
59
- json_hash = JSON.parse( json.to_s )
60
- self.from_h( json_hash )
56
+ # @return [RCAP::CAP_1_0::Alert, RCAP::CAP_1_1::Alert, RCAP::CAP_1_2::Alert ]
57
+ def self.from_json(json)
58
+ json_hash = JSON.parse(json.to_s)
59
+ from_h(json_hash)
61
60
  end
62
61
 
63
62
  # Initialise a RCAP Alert from a Ruby hash produced by
@@ -65,15 +64,15 @@ module RCAP
65
64
  # CAP 1.0, 1.1 or 1.2 Alert is instantiated accordingly.
66
65
  #
67
66
  # @param [Hash] hash Alert as a Ruby hash.
68
- # @return [ RCAP::CAP_1_0::Alert, RCAP::CAP_1_1::Alert, RCAP::CAP_1_2::Alert ]
69
- def self.from_h( hash )
70
- case hash[ JSON_CAP_VERSION_KEY ]
67
+ # @return [RCAP::CAP_1_0::Alert, RCAP::CAP_1_1::Alert, RCAP::CAP_1_2::Alert ]
68
+ def self.from_h(hash)
69
+ case hash[JSON_CAP_VERSION_KEY]
71
70
  when CAP_1_0::Alert::CAP_VERSION
72
- CAP_1_0::Alert.from_h( hash )
71
+ CAP_1_0::Alert.from_h(hash)
73
72
  when CAP_1_1::Alert::CAP_VERSION
74
- CAP_1_1::Alert.from_h( hash )
73
+ CAP_1_1::Alert.from_h(hash)
75
74
  else
76
- CAP_1_2::Alert.from_h( hash )
75
+ CAP_1_2::Alert.from_h(hash)
77
76
  end
78
77
  end
79
78
  end
@@ -3,71 +3,71 @@ module RCAP
3
3
  class Alert
4
4
  include Validation
5
5
 
6
- STATUS_ACTUAL = "Actual"
7
- STATUS_EXERCISE = "Exercise"
8
- STATUS_SYSTEM = "System"
9
- STATUS_TEST = "Test"
6
+ STATUS_ACTUAL = 'Actual'
7
+ STATUS_EXERCISE = 'Exercise'
8
+ STATUS_SYSTEM = 'System'
9
+ STATUS_TEST = 'Test'
10
10
  # Valid values for status
11
- VALID_STATUSES = [ STATUS_ACTUAL, STATUS_EXERCISE, STATUS_SYSTEM, STATUS_TEST ]
11
+ VALID_STATUSES = [STATUS_ACTUAL, STATUS_EXERCISE, STATUS_SYSTEM, STATUS_TEST]
12
12
 
13
- MSG_TYPE_ALERT = "Alert"
14
- MSG_TYPE_UPDATE = "Update"
15
- MSG_TYPE_CANCEL = "Cancel"
16
- MSG_TYPE_ACK = "Ack"
17
- MSG_TYPE_ERROR = "Error"
13
+ MSG_TYPE_ALERT = 'Alert'
14
+ MSG_TYPE_UPDATE = 'Update'
15
+ MSG_TYPE_CANCEL = 'Cancel'
16
+ MSG_TYPE_ACK = 'Ack'
17
+ MSG_TYPE_ERROR = 'Error'
18
18
  # Valid values for msg_type
19
- VALID_MSG_TYPES = [ MSG_TYPE_ALERT, MSG_TYPE_UPDATE, MSG_TYPE_CANCEL, MSG_TYPE_ACK, MSG_TYPE_ERROR ]
19
+ VALID_MSG_TYPES = [MSG_TYPE_ALERT, MSG_TYPE_UPDATE, MSG_TYPE_CANCEL, MSG_TYPE_ACK, MSG_TYPE_ERROR]
20
20
 
21
- SCOPE_PUBLIC = "Public"
22
- SCOPE_RESTRICTED = "Restricted"
23
- SCOPE_PRIVATE = "Private"
21
+ SCOPE_PUBLIC = 'Public'
22
+ SCOPE_RESTRICTED = 'Restricted'
23
+ SCOPE_PRIVATE = 'Private'
24
24
  # Valid values for scope
25
- VALID_SCOPES = [ SCOPE_PUBLIC, SCOPE_PRIVATE, SCOPE_RESTRICTED ]
25
+ VALID_SCOPES = [SCOPE_PUBLIC, SCOPE_PRIVATE, SCOPE_RESTRICTED]
26
26
 
27
27
  # @return [String] If not set a UUID will be set by default on object initialisation
28
- attr_accessor( :identifier)
28
+ attr_accessor(:identifier)
29
29
  # @return [String]
30
- attr_accessor( :sender )
30
+ attr_accessor(:sender)
31
31
  # @return [DateTime] If not set will be time of creation.
32
- attr_accessor( :sent )
32
+ attr_accessor(:sent)
33
33
  # @return [String] Can only be one of {VALID_STATUSES}
34
- attr_accessor( :status )
34
+ attr_accessor(:status)
35
35
  # @return [String] Can only be one of {VALID_MSG_TYPES}
36
- attr_accessor( :msg_type )
36
+ attr_accessor(:msg_type)
37
37
  # @return [String] Can only be one of {VALID_SCOPES}
38
- attr_accessor( :scope )
38
+ attr_accessor(:scope)
39
39
  # @return [String]
40
- attr_accessor( :source )
40
+ attr_accessor(:source)
41
41
  # @return [String ] Required if scope is {SCOPE_RESTRICTED}
42
- attr_accessor( :restriction )
42
+ attr_accessor(:restriction)
43
43
  # @return [String]
44
- attr_accessor( :note )
44
+ attr_accessor(:note)
45
45
 
46
46
  # @return [Array<String>] Collection of address strings. Depends on scope being {SCOPE_PRIVATE}
47
- attr_reader( :addresses )
47
+ attr_reader(:addresses)
48
48
  # @return [Array<String>]
49
- attr_reader( :codes )
49
+ attr_reader(:codes)
50
50
  # @return [Array<String>] Collection of references to previous alerts
51
51
  # @see #to_reference
52
- attr_reader( :references)
52
+ attr_reader(:references)
53
53
  # @return [Array<String>] Collection of incident strings
54
- attr_reader( :incidents )
54
+ attr_reader(:incidents)
55
55
  # @return [Array<Info>]
56
- attr_reader( :infos )
56
+ attr_reader(:infos)
57
57
 
58
- validates_presence_of( :identifier, :sender, :sent, :status, :msg_type, :scope )
58
+ validates_presence_of(:identifier, :sender, :sent, :status, :msg_type, :scope)
59
59
 
60
- validates_inclusion_of( :status, :in => VALID_STATUSES )
61
- validates_inclusion_of( :msg_type, :in => VALID_MSG_TYPES )
62
- validates_inclusion_of( :scope, :in => VALID_SCOPES )
60
+ validates_inclusion_of(:status, in: VALID_STATUSES)
61
+ validates_inclusion_of(:msg_type, in: VALID_MSG_TYPES)
62
+ validates_inclusion_of(:scope, in: VALID_SCOPES)
63
63
 
64
- validates_format_of( :identifier, :with => ALLOWED_CHARACTERS )
65
- validates_format_of( :sender , :with => ALLOWED_CHARACTERS )
64
+ validates_format_of(:identifier, with: ALLOWED_CHARACTERS)
65
+ validates_format_of(:sender, with: ALLOWED_CHARACTERS)
66
66
 
67
- validates_conditional_presence_of( :addresses, :when => :scope, :is => SCOPE_PRIVATE )
68
- validates_conditional_presence_of( :restriction, :when => :scope, :is => SCOPE_RESTRICTED )
67
+ validates_conditional_presence_of(:addresses, when: :scope, is: SCOPE_PRIVATE)
68
+ validates_conditional_presence_of(:restriction, when: :scope, is: SCOPE_RESTRICTED)
69
69
 
70
- validates_collection_of( :infos )
70
+ validates_collection_of(:infos)
71
71
 
72
72
  # Initialises a new Alert object. Yields the initialised alert to a block.
73
73
  #
@@ -87,7 +87,7 @@ module RCAP
87
87
  @references = []
88
88
  @incidents = []
89
89
  @infos = []
90
- yield( self ) if block_given?
90
+ yield(self) if block_given?
91
91
  end
92
92
 
93
93
  # Creates a new {Info} object and adds it to the {#infos} array.
@@ -96,8 +96,8 @@ module RCAP
96
96
  # @yield [Info] The newly initialised Info object.
97
97
  # @return [Info] The initialised Info object after being yielded to the block
98
98
  def add_info
99
- self.info_class.new.tap do |info|
100
- yield( info ) if block_given?
99
+ info_class.new.tap do |info|
100
+ yield(info) if block_given?
101
101
  @infos << info
102
102
  end
103
103
  end
@@ -119,31 +119,31 @@ module RCAP
119
119
 
120
120
  # @return [REXML::Element]
121
121
  def to_xml_element
122
- xml_element = REXML::Element.new( XML_ELEMENT_NAME )
123
- xml_element.add_namespace( self.class::XMLNS )
124
- xml_element.add_element( IDENTIFIER_ELEMENT_NAME ).add_text( @identifier ) if @identifier
125
- xml_element.add_element( SENDER_ELEMENT_NAME ).add_text( @sender ) if @sender
126
- xml_element.add_element( SENT_ELEMENT_NAME ).add_text( @sent.to_s_for_cap ) if @sent
127
- xml_element.add_element( STATUS_ELEMENT_NAME ).add_text( @status ) if @status
128
- xml_element.add_element( MSG_TYPE_ELEMENT_NAME ).add_text( @msg_type ) if @msg_type
129
- xml_element.add_element( SOURCE_ELEMENT_NAME ).add_text( @source ) if @source
130
- xml_element.add_element( SCOPE_ELEMENT_NAME ).add_text( @scope ) if @scope
131
- xml_element.add_element( RESTRICTION_ELEMENT_NAME ).add_text( @restriction ) if @restriction
122
+ xml_element = REXML::Element.new(XML_ELEMENT_NAME)
123
+ xml_element.add_namespace(self.class::XMLNS)
124
+ xml_element.add_element(IDENTIFIER_ELEMENT_NAME).add_text(@identifier) if @identifier
125
+ xml_element.add_element(SENDER_ELEMENT_NAME).add_text(@sender) if @sender
126
+ xml_element.add_element(SENT_ELEMENT_NAME).add_text(@sent.to_s_for_cap) if @sent
127
+ xml_element.add_element(STATUS_ELEMENT_NAME).add_text(@status) if @status
128
+ xml_element.add_element(MSG_TYPE_ELEMENT_NAME).add_text(@msg_type) if @msg_type
129
+ xml_element.add_element(SOURCE_ELEMENT_NAME).add_text(@source) if @source
130
+ xml_element.add_element(SCOPE_ELEMENT_NAME).add_text(@scope) if @scope
131
+ xml_element.add_element(RESTRICTION_ELEMENT_NAME).add_text(@restriction) if @restriction
132
132
  if @addresses.any?
133
- xml_element.add_element( ADDRESSES_ELEMENT_NAME ).add_text( @addresses.to_s_for_cap )
133
+ xml_element.add_element(ADDRESSES_ELEMENT_NAME).add_text(@addresses.to_s_for_cap)
134
134
  end
135
135
  @codes.each do |code|
136
- xml_element.add_element( CODE_ELEMENT_NAME ).add_text( code )
136
+ xml_element.add_element(CODE_ELEMENT_NAME).add_text(code)
137
137
  end
138
- xml_element.add_element( NOTE_ELEMENT_NAME ).add_text( @note ) if @note
138
+ xml_element.add_element(NOTE_ELEMENT_NAME).add_text(@note) if @note
139
139
  if @references.any?
140
- xml_element.add_element( REFERENCES_ELEMENT_NAME ).add_text( @references.join( ' ' ))
140
+ xml_element.add_element(REFERENCES_ELEMENT_NAME).add_text(@references.join(' '))
141
141
  end
142
142
  if @incidents.any?
143
- xml_element.add_element( INCIDENTS_ELEMENT_NAME ).add_text( @incidents.join( ' ' ))
143
+ xml_element.add_element(INCIDENTS_ELEMENT_NAME).add_text(@incidents.join(' '))
144
144
  end
145
145
  @infos.each do |info|
146
- xml_element.add_element( info.to_xml_element )
146
+ xml_element.add_element(info.to_xml_element)
147
147
  end
148
148
  xml_element
149
149
  end
@@ -151,8 +151,8 @@ module RCAP
151
151
  # @return [REXML::Document]
152
152
  def to_xml_document
153
153
  xml_document = REXML::Document.new
154
- xml_document.add( REXML::XMLDecl.new )
155
- xml_document.add( self.to_xml_element )
154
+ xml_document.add(REXML::XMLDecl.new)
155
+ xml_document.add(to_xml_element)
156
156
  xml_document
157
157
  end
158
158
 
@@ -160,13 +160,13 @@ module RCAP
160
160
  #
161
161
  # @param [true,false] pretty_print Pretty print output
162
162
  # @return [String]
163
- def to_xml( pretty_print = false )
163
+ def to_xml(pretty_print = false)
164
164
  if pretty_print
165
- xml_document = ""
166
- RCAP::XML_PRETTY_PRINTER.write( self.to_xml_document, xml_document )
165
+ xml_document = ''
166
+ RCAP::XML_PRETTY_PRINTER.write(to_xml_document, xml_document)
167
167
  xml_document
168
168
  else
169
- self.to_xml_document.to_s
169
+ to_xml_document.to_s
170
170
  end
171
171
  end
172
172
 
@@ -175,29 +175,29 @@ module RCAP
175
175
  #
176
176
  # @return [String]
177
177
  def to_reference
178
- "#{ @sender },#{ @identifier },#{ RCAP.to_s_for_cap( @sent )}"
178
+ "#{ @sender },#{ @identifier },#{ RCAP.to_s_for_cap(@sent)}"
179
179
  end
180
180
 
181
181
  # @return [String]
182
182
  def inspect
183
- alert_inspect = [ "CAP Version: #{ self.class::CAP_VERSION }",
184
- "Identifier: #{ @identifier }",
185
- "Sender: #{ @sender }",
186
- "Sent: #{ @sent }",
187
- "Status: #{ @status }",
188
- "Message Type: #{ @msg_type }",
189
- "Source: #{ @source }",
190
- "Scope: #{ @scope }",
191
- "Restriction: #{ @restriction }",
192
- "Addresses: #{ @addresses.to_s_for_cap }",
193
- "Codes:",
194
- @codes.map{ |code| " " + code }.join("\n")+"",
195
- "Note: #{ @note }",
196
- "References: #{ @references.join( ' ' )}",
197
- "Incidents: #{ @incidents.join( ' ')}",
198
- "Information:",
199
- @infos.map{ |info| " " + info.to_s }.join( "\n" )].join( "\n" )
200
- RCAP.format_lines_for_inspect( 'ALERT', alert_inspect )
183
+ alert_inspect = ["CAP Version: #{ self.class::CAP_VERSION }",
184
+ "Identifier: #{ @identifier }",
185
+ "Sender: #{ @sender }",
186
+ "Sent: #{ @sent }",
187
+ "Status: #{ @status }",
188
+ "Message Type: #{ @msg_type }",
189
+ "Source: #{ @source }",
190
+ "Scope: #{ @scope }",
191
+ "Restriction: #{ @restriction }",
192
+ "Addresses: #{ @addresses.to_s_for_cap }",
193
+ 'Codes:',
194
+ @codes.map { |code| ' ' + code }.join("\n") + '',
195
+ "Note: #{ @note }",
196
+ "References: #{ @references.join(' ')}",
197
+ "Incidents: #{ @incidents.join(' ')}",
198
+ 'Information:',
199
+ @infos.map { |info| ' ' + info.to_s }.join("\n")].join("\n")
200
+ RCAP.format_lines_for_inspect('ALERT', alert_inspect)
201
201
  end
202
202
 
203
203
  # Returns a string representation of the alert of the form
@@ -206,7 +206,7 @@ module RCAP
206
206
  #
207
207
  # @return [String]
208
208
  def to_s
209
- "#{ @sender }/#{ @identifier }/#{ RCAP.to_s_for_cap( @sent )}"
209
+ "#{ @sender }/#{ @identifier }/#{ RCAP.to_s_for_cap(@sent)}"
210
210
  end
211
211
 
212
212
  XPATH = 'cap:alert'
@@ -226,129 +226,129 @@ module RCAP
226
226
 
227
227
  # @param [REXML::Element] alert_xml_element
228
228
  # @return [RCAP::CAP_1_0::Alert]
229
- def self.from_xml_element( alert_xml_element )
230
- self.new do |alert|
231
- alert.identifier = RCAP.xpath_text( alert_xml_element, IDENTIFIER_XPATH, alert.xmlns )
232
- alert.sender = RCAP.xpath_text( alert_xml_element, SENDER_XPATH, alert.xmlns )
233
- alert.sent = RCAP.parse_datetime( RCAP.xpath_text( alert_xml_element, SENT_XPATH, alert.xmlns ))
234
- alert.status = RCAP.xpath_text( alert_xml_element, STATUS_XPATH, alert.xmlns )
235
- alert.msg_type = RCAP.xpath_text( alert_xml_element, MSG_TYPE_XPATH, alert.xmlns )
236
- alert.source = RCAP.xpath_text( alert_xml_element, SOURCE_XPATH, alert.xmlns )
237
- alert.scope = RCAP.xpath_text( alert_xml_element, SCOPE_XPATH, alert.xmlns )
238
- alert.restriction = RCAP.xpath_text( alert_xml_element, RESTRICTION_XPATH, alert.xmlns )
239
-
240
- RCAP.unpack_if_given( RCAP.xpath_text( alert_xml_element, ADDRESSES_XPATH, alert.xmlns )).each do |address|
229
+ def self.from_xml_element(alert_xml_element)
230
+ new do |alert|
231
+ alert.identifier = RCAP.xpath_text(alert_xml_element, IDENTIFIER_XPATH, alert.xmlns)
232
+ alert.sender = RCAP.xpath_text(alert_xml_element, SENDER_XPATH, alert.xmlns)
233
+ alert.sent = RCAP.parse_datetime(RCAP.xpath_text(alert_xml_element, SENT_XPATH, alert.xmlns))
234
+ alert.status = RCAP.xpath_text(alert_xml_element, STATUS_XPATH, alert.xmlns)
235
+ alert.msg_type = RCAP.xpath_text(alert_xml_element, MSG_TYPE_XPATH, alert.xmlns)
236
+ alert.source = RCAP.xpath_text(alert_xml_element, SOURCE_XPATH, alert.xmlns)
237
+ alert.scope = RCAP.xpath_text(alert_xml_element, SCOPE_XPATH, alert.xmlns)
238
+ alert.restriction = RCAP.xpath_text(alert_xml_element, RESTRICTION_XPATH, alert.xmlns)
239
+
240
+ RCAP.unpack_if_given(RCAP.xpath_text(alert_xml_element, ADDRESSES_XPATH, alert.xmlns)).each do |address|
241
241
  alert.addresses << address.strip
242
242
  end
243
243
 
244
- RCAP.xpath_match( alert_xml_element, CODE_XPATH, alert.xmlns ).each do |element|
244
+ RCAP.xpath_match(alert_xml_element, CODE_XPATH, alert.xmlns).each do |element|
245
245
  alert.codes << element.text.strip
246
246
  end
247
247
 
248
- alert.note = RCAP.xpath_text( alert_xml_element, NOTE_XPATH, alert.xmlns )
248
+ alert.note = RCAP.xpath_text(alert_xml_element, NOTE_XPATH, alert.xmlns)
249
249
 
250
- RCAP.unpack_if_given( RCAP.xpath_text( alert_xml_element, REFERENCES_XPATH, alert.xmlns )).each do |reference|
250
+ RCAP.unpack_if_given(RCAP.xpath_text(alert_xml_element, REFERENCES_XPATH, alert.xmlns)).each do |reference|
251
251
  alert.references << reference.strip
252
252
  end
253
253
 
254
- RCAP.unpack_if_given( RCAP.xpath_text( alert_xml_element, INCIDENTS_XPATH, alert.xmlns )).each do |incident|
254
+ RCAP.unpack_if_given(RCAP.xpath_text(alert_xml_element, INCIDENTS_XPATH, alert.xmlns)).each do |incident|
255
255
  alert.incidents << incident.strip
256
256
  end
257
257
 
258
- RCAP.xpath_match( alert_xml_element, Info::XPATH, alert.xmlns ).each do |element|
259
- alert.infos << alert.info_class.from_xml_element( element )
258
+ RCAP.xpath_match(alert_xml_element, Info::XPATH, alert.xmlns).each do |element|
259
+ alert.infos << alert.info_class.from_xml_element(element)
260
260
  end
261
261
  end
262
262
  end
263
263
 
264
264
  # @param [REXML::Document] xml_document
265
265
  # @return [Alert]
266
- def self.from_xml_document( xml_document )
267
- self.from_xml_element( xml_document.root )
266
+ def self.from_xml_document(xml_document)
267
+ from_xml_element(xml_document.root)
268
268
  end
269
269
 
270
270
  # Initialise an Alert object from an XML string. Any object that is a subclass of IO (e.g. File) can be passed in.
271
271
  #
272
272
  # @param [String] xml
273
273
  # @return [Alert]
274
- def self.from_xml( xml )
275
- self.from_xml_document( REXML::Document.new( xml ))
274
+ def self.from_xml(xml)
275
+ from_xml_document(REXML::Document.new(xml))
276
276
  end
277
277
 
278
- CAP_VERSION_YAML = "CAP Version"
279
- IDENTIFIER_YAML = "Identifier"
280
- SENDER_YAML = "Sender"
281
- SENT_YAML = "Sent"
282
- STATUS_YAML = "Status"
283
- MSG_TYPE_YAML = "Message Type"
284
- SOURCE_YAML = "Source"
285
- SCOPE_YAML = "Scope"
286
- RESTRICTION_YAML = "Restriction"
287
- ADDRESSES_YAML = "Addresses"
288
- CODES_YAML = "Codes"
289
- NOTE_YAML = "Note"
290
- REFERENCES_YAML = "References"
291
- INCIDENTS_YAML = "Incidents"
292
- INFOS_YAML = "Information"
278
+ CAP_VERSION_YAML = 'CAP Version'
279
+ IDENTIFIER_YAML = 'Identifier'
280
+ SENDER_YAML = 'Sender'
281
+ SENT_YAML = 'Sent'
282
+ STATUS_YAML = 'Status'
283
+ MSG_TYPE_YAML = 'Message Type'
284
+ SOURCE_YAML = 'Source'
285
+ SCOPE_YAML = 'Scope'
286
+ RESTRICTION_YAML = 'Restriction'
287
+ ADDRESSES_YAML = 'Addresses'
288
+ CODES_YAML = 'Codes'
289
+ NOTE_YAML = 'Note'
290
+ REFERENCES_YAML = 'References'
291
+ INCIDENTS_YAML = 'Incidents'
292
+ INFOS_YAML = 'Information'
293
293
 
294
294
  # Returns a string containing the YAML representation of the alert.
295
295
  #
296
296
  # @return [String]
297
- def to_yaml( options = {} )
298
- RCAP.attribute_values_to_hash( [ CAP_VERSION_YAML, self.class::CAP_VERSION ],
299
- [ IDENTIFIER_YAML, @identifier ],
300
- [ SENDER_YAML, @sender ],
301
- [ SENT_YAML, @sent ],
302
- [ STATUS_YAML, @status ],
303
- [ MSG_TYPE_YAML, @msg_type ],
304
- [ SOURCE_YAML, @source ],
305
- [ SCOPE_YAML, @scope ],
306
- [ RESTRICTION_YAML, @restriction ],
307
- [ ADDRESSES_YAML, @addresses ],
308
- [ CODES_YAML, @codes ],
309
- [ NOTE_YAML, @note ],
310
- [ REFERENCES_YAML, @references ],
311
- [ INCIDENTS_YAML, @incidents ],
312
- [ INFOS_YAML, @infos.map( &:to_yaml_data )]).to_yaml( options )
297
+ def to_yaml(options = {})
298
+ RCAP.attribute_values_to_hash([CAP_VERSION_YAML, self.class::CAP_VERSION],
299
+ [IDENTIFIER_YAML, @identifier],
300
+ [SENDER_YAML, @sender],
301
+ [SENT_YAML, @sent],
302
+ [STATUS_YAML, @status],
303
+ [MSG_TYPE_YAML, @msg_type],
304
+ [SOURCE_YAML, @source],
305
+ [SCOPE_YAML, @scope],
306
+ [RESTRICTION_YAML, @restriction],
307
+ [ADDRESSES_YAML, @addresses],
308
+ [CODES_YAML, @codes],
309
+ [NOTE_YAML, @note],
310
+ [REFERENCES_YAML, @references],
311
+ [INCIDENTS_YAML, @incidents],
312
+ [INFOS_YAML, @infos.map(&:to_yaml_data)]).to_yaml(options)
313
313
  end
314
314
 
315
315
  # Initialise an Alert object from a YAML string. Any object that is a subclass of IO (e.g. File) can be passed in.
316
316
  #
317
317
  # @param [String] yaml
318
318
  # @return [Alert]
319
- def self.from_yaml( yaml )
320
- self.from_yaml_data( YAML.load( yaml ))
319
+ def self.from_yaml(yaml)
320
+ from_yaml_data(YAML.load(yaml))
321
321
  end
322
322
 
323
323
  # Initialise an Alert object from a hash reutrned from YAML.load.
324
324
  #
325
325
  # @param [hash] alert_yaml_data
326
326
  # @return [Alert]
327
- def self.from_yaml_data( alert_yaml_data )
328
- self.new do |alert|
329
- alert.identifier = RCAP.strip_if_given( alert_yaml_data[ IDENTIFIER_YAML ])
330
- alert.sender = RCAP.strip_if_given( alert_yaml_data[ SENDER_YAML ])
331
- alert.sent = RCAP.parse_datetime( alert_yaml_data[ SENT_YAML ])
332
- alert.status = RCAP.strip_if_given( alert_yaml_data[ STATUS_YAML ])
333
- alert.msg_type = RCAP.strip_if_given( alert_yaml_data[ MSG_TYPE_YAML ])
334
- alert.source = RCAP.strip_if_given( alert_yaml_data[ SOURCE_YAML ])
335
- alert.scope = RCAP.strip_if_given( alert_yaml_data[ SCOPE_YAML ])
336
- alert.restriction = RCAP.strip_if_given( alert_yaml_data[ RESTRICTION_YAML ])
337
- Array( alert_yaml_data[ ADDRESSES_YAML ]).each do |address|
327
+ def self.from_yaml_data(alert_yaml_data)
328
+ new do |alert|
329
+ alert.identifier = RCAP.strip_if_given(alert_yaml_data[ IDENTIFIER_YAML])
330
+ alert.sender = RCAP.strip_if_given(alert_yaml_data[ SENDER_YAML])
331
+ alert.sent = RCAP.parse_datetime(alert_yaml_data[ SENT_YAML])
332
+ alert.status = RCAP.strip_if_given(alert_yaml_data[ STATUS_YAML])
333
+ alert.msg_type = RCAP.strip_if_given(alert_yaml_data[ MSG_TYPE_YAML])
334
+ alert.source = RCAP.strip_if_given(alert_yaml_data[ SOURCE_YAML])
335
+ alert.scope = RCAP.strip_if_given(alert_yaml_data[ SCOPE_YAML])
336
+ alert.restriction = RCAP.strip_if_given(alert_yaml_data[ RESTRICTION_YAML])
337
+ Array(alert_yaml_data[ ADDRESSES_YAML]).each do |address|
338
338
  alert.addresses << address.strip
339
339
  end
340
- Array( alert_yaml_data[ CODES_YAML ]).each do |code|
340
+ Array(alert_yaml_data[ CODES_YAML]).each do |code|
341
341
  alert.codes << code.strip
342
342
  end
343
- alert.note = alert_yaml_data[ NOTE_YAML ]
344
- Array( alert_yaml_data[ REFERENCES_YAML ]).each do |reference|
343
+ alert.note = alert_yaml_data[ NOTE_YAML]
344
+ Array(alert_yaml_data[ REFERENCES_YAML]).each do |reference|
345
345
  alert.references << reference.strip
346
346
  end
347
- Array( alert_yaml_data[ INCIDENTS_YAML ]).each do |incident|
347
+ Array(alert_yaml_data[ INCIDENTS_YAML]).each do |incident|
348
348
  alert.incidents << incident.strip
349
349
  end
350
- Array( alert_yaml_data[ INFOS_YAML ]).each do |info_yaml_data|
351
- alert.infos << alert.info_class.from_yaml_data( info_yaml_data )
350
+ Array(alert_yaml_data[ INFOS_YAML]).each do |info_yaml_data|
351
+ alert.infos << alert.info_class.from_yaml_data(info_yaml_data)
352
352
  end
353
353
  end
354
354
  end
@@ -373,54 +373,54 @@ module RCAP
373
373
  #
374
374
  # @return [Hash]
375
375
  def to_h
376
- RCAP.attribute_values_to_hash( [ CAP_VERSION_KEY, self.class::CAP_VERSION ],
377
- [ IDENTIFIER_KEY, @identifier ],
378
- [ SENDER_KEY, @sender ],
379
- [ SENT_KEY, RCAP.to_s_for_cap( @sent )],
380
- [ STATUS_KEY, @status ],
381
- [ MSG_TYPE_KEY, @msg_type ],
382
- [ SOURCE_KEY, @source ],
383
- [ SCOPE_KEY, @scope ],
384
- [ RESTRICTION_KEY, @restriction ],
385
- [ ADDRESSES_KEY, @addresses ],
386
- [ CODES_KEY, @codes ],
387
- [ NOTE_KEY, @note ],
388
- [ REFERENCES_KEY, @references ],
389
- [ INCIDENTS_KEY, @incidents ],
390
- [ INFOS_KEY, @infos.map{ |info| info.to_h }])
376
+ RCAP.attribute_values_to_hash([CAP_VERSION_KEY, self.class::CAP_VERSION],
377
+ [IDENTIFIER_KEY, @identifier],
378
+ [SENDER_KEY, @sender],
379
+ [SENT_KEY, RCAP.to_s_for_cap(@sent)],
380
+ [STATUS_KEY, @status],
381
+ [MSG_TYPE_KEY, @msg_type],
382
+ [SOURCE_KEY, @source],
383
+ [SCOPE_KEY, @scope],
384
+ [RESTRICTION_KEY, @restriction],
385
+ [ADDRESSES_KEY, @addresses],
386
+ [CODES_KEY, @codes],
387
+ [NOTE_KEY, @note],
388
+ [REFERENCES_KEY, @references],
389
+ [INCIDENTS_KEY, @incidents],
390
+ [INFOS_KEY, @infos.map { |info| info.to_h }])
391
391
  end
392
392
 
393
393
  # Initialises an Alert object from a Hash produced by Alert#to_h
394
394
  #
395
395
  # @param [Hash] alert_hash
396
396
  # @return [RCAP::CAP_1_0::Alert]
397
- def self.from_h( alert_hash )
398
- self.new do |alert|
399
- alert.identifier = RCAP.strip_if_given( alert_hash[ IDENTIFIER_KEY ])
400
- alert.sender = RCAP.strip_if_given( alert_hash[ SENDER_KEY ])
401
- alert.sent = RCAP.parse_datetime( alert_hash[ SENT_KEY ])
402
- alert.status = RCAP.strip_if_given( alert_hash[ STATUS_KEY ])
403
- alert.msg_type = RCAP.strip_if_given( alert_hash[ MSG_TYPE_KEY ])
404
- alert.source = RCAP.strip_if_given( alert_hash[ SOURCE_KEY ])
405
- alert.scope = RCAP.strip_if_given( alert_hash[ SCOPE_KEY ])
406
- alert.restriction = RCAP.strip_if_given( alert_hash[ RESTRICTION_KEY ])
407
- Array( alert_hash[ ADDRESSES_KEY ]).each do |address|
397
+ def self.from_h(alert_hash)
398
+ new do |alert|
399
+ alert.identifier = RCAP.strip_if_given(alert_hash[IDENTIFIER_KEY])
400
+ alert.sender = RCAP.strip_if_given(alert_hash[SENDER_KEY])
401
+ alert.sent = RCAP.parse_datetime(alert_hash[SENT_KEY])
402
+ alert.status = RCAP.strip_if_given(alert_hash[STATUS_KEY])
403
+ alert.msg_type = RCAP.strip_if_given(alert_hash[MSG_TYPE_KEY])
404
+ alert.source = RCAP.strip_if_given(alert_hash[SOURCE_KEY])
405
+ alert.scope = RCAP.strip_if_given(alert_hash[SCOPE_KEY])
406
+ alert.restriction = RCAP.strip_if_given(alert_hash[RESTRICTION_KEY])
407
+ Array(alert_hash[ ADDRESSES_KEY]).each do |address|
408
408
  alert.addresses << address.strip
409
409
  end
410
- Array( alert_hash[ CODES_KEY ]).each do |code|
410
+ Array(alert_hash[ CODES_KEY]).each do |code|
411
411
  alert.codes << code.strip
412
412
  end
413
- alert.note = alert_hash[ NOTE_KEY ]
414
- Array( alert_hash[ REFERENCES_KEY ]).each do |reference|
413
+ alert.note = alert_hash[ NOTE_KEY]
414
+ Array(alert_hash[ REFERENCES_KEY]).each do |reference|
415
415
  alert.references << reference.strip
416
416
  end
417
417
 
418
- Array( alert_hash[ INCIDENTS_KEY ]).each do |incident|
418
+ Array(alert_hash[ INCIDENTS_KEY]).each do |incident|
419
419
  alert.incidents << incident.strip
420
420
  end
421
421
 
422
- Array( alert_hash[ INFOS_KEY ]).each do |info_hash|
423
- alert.infos << alert.info_class.from_h( info_hash )
422
+ Array(alert_hash[ INFOS_KEY]).each do |info_hash|
423
+ alert.infos << alert.info_class.from_h(info_hash)
424
424
  end
425
425
  end
426
426
  end
@@ -429,11 +429,11 @@ module RCAP
429
429
  #
430
430
  # @param [true,false] pretty_print
431
431
  # @return [String]
432
- def to_json( pretty_print = false )
432
+ def to_json(pretty_print = false)
433
433
  if pretty_print
434
- JSON.pretty_generate( self.to_h )
434
+ JSON.pretty_generate(to_h)
435
435
  else
436
- self.to_h.to_json
436
+ to_h.to_json
437
437
  end
438
438
  end
439
439
 
@@ -441,8 +441,8 @@ module RCAP
441
441
  #
442
442
  # @param [String] json_string
443
443
  # @return [Alert]
444
- def self.from_json( json_string )
445
- self.from_h( JSON.parse( json_string ))
444
+ def self.from_json(json_string)
445
+ from_h(JSON.parse(json_string))
446
446
  end
447
447
  end
448
448
  end