rcap 2.1.0 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,6 +1,10 @@
1
1
  Change Log
2
2
  ==========
3
3
 
4
+ ## 2.2.0 - 2 February 2013
5
+
6
+ * Better whitespace handling on parsing
7
+
4
8
  ## 2.1.0 - 12 January 2013
5
9
 
6
10
  * RCAP.attribute_values_to_hash now does not filter out values that are empty, only nil.
@@ -230,26 +230,29 @@ module RCAP
230
230
  self.new do |alert|
231
231
  alert.identifier = RCAP.xpath_text( alert_xml_element, IDENTIFIER_XPATH, alert.xmlns )
232
232
  alert.sender = RCAP.xpath_text( alert_xml_element, SENDER_XPATH, alert.xmlns )
233
- alert.sent = (( sent = RCAP.xpath_first( alert_xml_element, SENT_XPATH, alert.xmlns )) ? DateTime.parse( sent.text ) : nil )
233
+ alert.sent = RCAP.parse_datetime( RCAP.xpath_text( alert_xml_element, SENT_XPATH, alert.xmlns ))
234
234
  alert.status = RCAP.xpath_text( alert_xml_element, STATUS_XPATH, alert.xmlns )
235
235
  alert.msg_type = RCAP.xpath_text( alert_xml_element, MSG_TYPE_XPATH, alert.xmlns )
236
236
  alert.source = RCAP.xpath_text( alert_xml_element, SOURCE_XPATH, alert.xmlns )
237
237
  alert.scope = RCAP.xpath_text( alert_xml_element, SCOPE_XPATH, alert.xmlns )
238
238
  alert.restriction = RCAP.xpath_text( alert_xml_element, RESTRICTION_XPATH, alert.xmlns )
239
239
 
240
- (( address = RCAP.xpath_text( alert_xml_element, ADDRESSES_XPATH, alert.xmlns )) ? address.unpack_cap_list : [] ).each do |address|
241
- alert.addresses << address
240
+ RCAP.unpack_if_given( RCAP.xpath_text( alert_xml_element, ADDRESSES_XPATH, alert.xmlns )).each do |address|
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
- alert.codes << element.text
245
+ alert.codes << element.text.strip
245
246
  end
246
- alert.note = RCAP.xpath_text( alert_xml_element, NOTE_XPATH, alert.xmlns )
247
- (( references = RCAP.xpath_text( alert_xml_element, REFERENCES_XPATH, alert.xmlns )) ? references.split( ' ' ) : []).each do |reference|
248
- alert.references << reference
247
+
248
+ alert.note = RCAP.xpath_text( alert_xml_element, NOTE_XPATH, alert.xmlns )
249
+
250
+ RCAP.unpack_if_given( RCAP.xpath_text( alert_xml_element, REFERENCES_XPATH, alert.xmlns )).each do |reference|
251
+ alert.references << reference.strip
249
252
  end
250
253
 
251
- (( incidents = RCAP.xpath_text( alert_xml_element, INCIDENTS_XPATH, alert.xmlns )) ? incidents.split( ' ' ) : []).each do |incident|
252
- alert.incidents << incident
254
+ RCAP.unpack_if_given( RCAP.xpath_text( alert_xml_element, INCIDENTS_XPATH, alert.xmlns )).each do |incident|
255
+ alert.incidents << incident.strip
253
256
  end
254
257
 
255
258
  RCAP.xpath_match( alert_xml_element, Info::XPATH, alert.xmlns ).each do |element|
@@ -323,26 +326,26 @@ module RCAP
323
326
  # @return [Alert]
324
327
  def self.from_yaml_data( alert_yaml_data )
325
328
  self.new do |alert|
326
- alert.identifier = alert_yaml_data[ IDENTIFIER_YAML ]
327
- alert.sender = alert_yaml_data[ SENDER_YAML ]
328
- alert.sent = ( sent = alert_yaml_data[ SENT_YAML ]).blank? ? nil : DateTime.parse( sent.to_s )
329
- alert.status = alert_yaml_data[ STATUS_YAML ]
330
- alert.msg_type = alert_yaml_data[ MSG_TYPE_YAML ]
331
- alert.source = alert_yaml_data[ SOURCE_YAML ]
332
- alert.scope = alert_yaml_data[ SCOPE_YAML ]
333
- alert.restriction = alert_yaml_data[ RESTRICTION_YAML ]
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 ])
334
337
  Array( alert_yaml_data[ ADDRESSES_YAML ]).each do |address|
335
- alert.addresses << address
338
+ alert.addresses << address.strip
336
339
  end
337
340
  Array( alert_yaml_data[ CODES_YAML ]).each do |code|
338
- alert.codes << code
341
+ alert.codes << code.strip
339
342
  end
340
343
  alert.note = alert_yaml_data[ NOTE_YAML ]
341
344
  Array( alert_yaml_data[ REFERENCES_YAML ]).each do |reference|
342
- alert.references << reference
345
+ alert.references << reference.strip
343
346
  end
344
347
  Array( alert_yaml_data[ INCIDENTS_YAML ]).each do |incident|
345
- alert.incidents << incident
348
+ alert.incidents << incident.strip
346
349
  end
347
350
  Array( alert_yaml_data[ INFOS_YAML ]).each do |info_yaml_data|
348
351
  alert.infos << alert.info_class.from_yaml_data( info_yaml_data )
@@ -393,27 +396,27 @@ module RCAP
393
396
  # @return [RCAP::CAP_1_0::Alert]
394
397
  def self.from_h( alert_hash )
395
398
  self.new do |alert|
396
- alert.identifier = alert_hash[ IDENTIFIER_KEY ]
397
- alert.sender = alert_hash[ SENDER_KEY ]
399
+ alert.identifier = RCAP.strip_if_given( alert_hash[ IDENTIFIER_KEY ])
400
+ alert.sender = RCAP.strip_if_given( alert_hash[ SENDER_KEY ])
398
401
  alert.sent = RCAP.parse_datetime( alert_hash[ SENT_KEY ])
399
- alert.status = alert_hash[ STATUS_KEY ]
400
- alert.msg_type = alert_hash[ MSG_TYPE_KEY ]
401
- alert.source = alert_hash[ SOURCE_KEY ]
402
- alert.scope = alert_hash[ SCOPE_KEY ]
403
- alert.restriction = alert_hash[ RESTRICTION_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 ])
404
407
  Array( alert_hash[ ADDRESSES_KEY ]).each do |address|
405
- alert.addresses << address
408
+ alert.addresses << address.strip
406
409
  end
407
410
  Array( alert_hash[ CODES_KEY ]).each do |code|
408
- alert.codes << code
411
+ alert.codes << code.strip
409
412
  end
410
413
  alert.note = alert_hash[ NOTE_KEY ]
411
414
  Array( alert_hash[ REFERENCES_KEY ]).each do |reference|
412
- alert.references << reference
415
+ alert.references << reference.strip
413
416
  end
414
417
 
415
418
  Array( alert_hash[ INCIDENTS_KEY ]).each do |incident|
416
- alert.incidents << incident
419
+ alert.incidents << incident.strip
417
420
  end
418
421
 
419
422
  Array( alert_hash[ INFOS_KEY ]).each do |info_hash|
@@ -78,8 +78,8 @@ module RCAP
78
78
  def self.from_xml_element( area_xml_element )
79
79
  self.new do |area|
80
80
  area.area_desc = RCAP.xpath_text( area_xml_element, AREA_DESC_XPATH, area.xmlns )
81
- area.altitude = (( alt = RCAP.xpath_text( area_xml_element, ALTITUDE_XPATH, area.xmlns )) ? alt.to_f : nil )
82
- area.ceiling = (( ceil = RCAP.xpath_text( area_xml_element, CEILING_XPATH, area.xmlns )) ? ceil.to_f : nil )
81
+ area.altitude = RCAP.to_f_if_given( RCAP.xpath_text( area_xml_element, ALTITUDE_XPATH, area.xmlns ))
82
+ area.ceiling = RCAP.to_f_if_given( RCAP.xpath_text( area_xml_element, CEILING_XPATH, area.xmlns ))
83
83
 
84
84
  RCAP.xpath_match( area_xml_element, area.circle_class::XPATH, area.xmlns ).each do |circle_element|
85
85
  area.circles << area.circle_class.from_xml_element( circle_element )
@@ -76,9 +76,9 @@ module RCAP
76
76
  def self.from_yaml_data( circle_yaml_data )
77
77
  lattitude, longitude, radius = circle_yaml_data
78
78
  self.new do |circle|
79
- circle.lattitude = lattitude
80
- circle.longitude = longitude
81
- circle.radius = radius
79
+ circle.lattitude = lattitude.to_f
80
+ circle.longitude = longitude.to_f
81
+ circle.radius = radius.to_f
82
82
  end
83
83
  end
84
84
 
@@ -94,9 +94,9 @@ module RCAP
94
94
  # @return [Circle]
95
95
  def self.from_h( circle_hash )
96
96
  self.new do |circle|
97
- circle.radius = circle_hash[ RADIUS_KEY ]
98
- circle.lattitude = circle_hash[ LATTITUDE_KEY ]
99
- circle.longitude = circle_hash[ LONGITUDE_KEY ]
97
+ circle.radius = circle_hash[ RADIUS_KEY ].to_f
98
+ circle.lattitude = circle_hash[ LATTITUDE_KEY ].to_f
99
+ circle.longitude = circle_hash[ LONGITUDE_KEY ].to_f
100
100
  end
101
101
  end
102
102
 
@@ -111,9 +111,9 @@ module RCAP
111
111
  # @return [Circle]
112
112
  def self.from_a( circle_array )
113
113
  self.new do |circle|
114
- circle.longitude = circle_array[ LONGITUDE_INDEX ]
115
- circle.lattitude = circle_array[ LATTITUDE_INDEX ]
116
- circle.radius = circle_array[ RADIUS_INDEX ]
114
+ circle.longitude = circle_array[ LONGITUDE_INDEX ].to_f
115
+ circle.lattitude = circle_array[ LATTITUDE_INDEX ].to_f
116
+ circle.radius = circle_array[ RADIUS_INDEX ].to_f
117
117
  end
118
118
  end
119
119
  end
@@ -369,32 +369,32 @@ module RCAP
369
369
  Array( info_yaml_data [ CATEGORIES_YAML ]).each do |category|
370
370
  info.categories << category
371
371
  end
372
- info.event = info_yaml_data [ EVENT_YAML ]
373
- info.urgency = info_yaml_data [ URGENCY_YAML ]
374
- info.severity = info_yaml_data [ SEVERITY_YAML ]
375
- info.certainty = info_yaml_data [ CERTAINTY_YAML ]
376
- info.audience = info_yaml_data [ AUDIENCE_YAML ]
372
+ info.event = RCAP.strip_if_given( info_yaml_data [ EVENT_YAML ])
373
+ info.urgency = RCAP.strip_if_given( info_yaml_data [ URGENCY_YAML ])
374
+ info.severity = RCAP.strip_if_given( info_yaml_data [ SEVERITY_YAML ])
375
+ info.certainty = RCAP.strip_if_given( info_yaml_data [ CERTAINTY_YAML ])
376
+ info.audience = RCAP.strip_if_given( info_yaml_data [ AUDIENCE_YAML ])
377
377
  info.effective = RCAP.parse_datetime( info_yaml_data[ EFFECTIVE_YAML ])
378
378
  info.onset = RCAP.parse_datetime( info_yaml_data[ ONSET_YAML ])
379
379
  info.expires = RCAP.parse_datetime( info_yaml_data[ EXPIRES_YAML ])
380
- info.sender_name = info_yaml_data [ SENDER_NAME_YAML ]
381
- info.headline = info_yaml_data [ HEADLINE_YAML ]
382
- info.description = info_yaml_data [ DESCRIPTION_YAML ]
383
- info.instruction = info_yaml_data [ INSTRUCTION_YAML ]
384
- info.web = info_yaml_data [ WEB_YAML ]
385
- info.contact = info_yaml_data [ CONTACT_YAML ]
380
+ info.sender_name = RCAP.strip_if_given( info_yaml_data [ SENDER_NAME_YAML ])
381
+ info.headline = RCAP.strip_if_given( info_yaml_data [ HEADLINE_YAML ])
382
+ info.description = RCAP.strip_if_given( info_yaml_data [ DESCRIPTION_YAML ])
383
+ info.instruction = RCAP.strip_if_given( info_yaml_data [ INSTRUCTION_YAML ])
384
+ info.web = RCAP.strip_if_given( info_yaml_data [ WEB_YAML ])
385
+ info.contact = RCAP.strip_if_given( info_yaml_data [ CONTACT_YAML ])
386
386
 
387
387
  Array( info_yaml_data [ EVENT_CODES_YAML ]).each do |name,value|
388
388
  info.add_event_code do |event_code|
389
- event_code.name = name
390
- event_code.value = value
389
+ event_code.name = RCAP.strip_if_given( name )
390
+ event_code.value = RCAP.strip_if_given( value )
391
391
  end
392
392
  end
393
393
 
394
394
  Array( info_yaml_data [ PARAMETERS_YAML ]).each do |name,value|
395
395
  info.add_parameter do |parameter|
396
- parameter.name = name
397
- parameter.value = value
396
+ parameter.name = RCAP.strip_if_given( name )
397
+ parameter.value = RCAP.strip_if_given( value )
398
398
  end
399
399
  end
400
400
 
@@ -459,22 +459,22 @@ module RCAP
459
459
  self.new do |info|
460
460
  info.language = info_hash[ LANGUAGE_KEY ]
461
461
  Array( info_hash[ CATEGORIES_KEY ]).each do |category|
462
- info.categories << category
462
+ info.categories << RCAP.strip_if_given( category )
463
463
  end
464
- info.event = info_hash[ EVENT_KEY ]
465
- info.urgency = info_hash[ URGENCY_KEY ]
466
- info.severity = info_hash[ SEVERITY_KEY ]
467
- info.certainty = info_hash[ CERTAINTY_KEY ]
468
- info.audience = info_hash[ AUDIENCE_KEY ]
464
+ info.event = RCAP.strip_if_given( info_hash[ EVENT_KEY ])
465
+ info.urgency = RCAP.strip_if_given( info_hash[ URGENCY_KEY ])
466
+ info.severity = RCAP.strip_if_given( info_hash[ SEVERITY_KEY ])
467
+ info.certainty = RCAP.strip_if_given( info_hash[ CERTAINTY_KEY ])
468
+ info.audience = RCAP.strip_if_given( info_hash[ AUDIENCE_KEY ])
469
469
  info.effective = RCAP.parse_datetime( info_hash[ EFFECTIVE_KEY ])
470
470
  info.onset = RCAP.parse_datetime( info_hash[ ONSET_KEY ])
471
471
  info.expires = RCAP.parse_datetime( info_hash[ EXPIRES_KEY ])
472
- info.sender_name = info_hash[ SENDER_NAME_KEY ]
473
- info.headline = info_hash[ HEADLINE_KEY ]
474
- info.description = info_hash[ DESCRIPTION_KEY ]
475
- info.instruction = info_hash[ INSTRUCTION_KEY ]
476
- info.web = info_hash[ WEB_KEY ]
477
- info.contact = info_hash[ CONTACT_KEY ]
472
+ info.sender_name = RCAP.strip_if_given( info_hash[ SENDER_NAME_KEY ])
473
+ info.headline = RCAP.strip_if_given( info_hash[ HEADLINE_KEY ])
474
+ info.description = RCAP.strip_if_given( info_hash[ DESCRIPTION_KEY ])
475
+ info.instruction = RCAP.strip_if_given( info_hash[ INSTRUCTION_KEY ])
476
+ info.web = RCAP.strip_if_given( info_hash[ WEB_KEY ])
477
+ info.contact = RCAP.strip_if_given( info_hash[ CONTACT_KEY ])
478
478
 
479
479
  Array( info_hash[ RESOURCES_KEY ]).each do |resource_hash|
480
480
  info.resources << info.resource_class.from_h( resource_hash )
@@ -78,8 +78,8 @@ module RCAP
78
78
  def self.from_h( hash )
79
79
  key = hash.keys.first
80
80
  self.new do |parameter|
81
- parameter.name = key
82
- parameter.value = hash[ key ]
81
+ parameter.name = RCAP.strip_if_given( key )
82
+ parameter.value = RCAP.strip_if_given( hash[ key ])
83
83
  end
84
84
  end
85
85
  end
@@ -58,8 +58,8 @@ module RCAP
58
58
  # @return [Point]
59
59
  def self.from_h( point_hash )
60
60
  self.new do |point|
61
- point.lattitude = point_hash[ LATTITUDE_KEY ]
62
- point.longitude = point_hash[ LONGITUDE_KEY ]
61
+ point.lattitude = point_hash[ LATTITUDE_KEY ].to_f
62
+ point.longitude = point_hash[ LONGITUDE_KEY ].to_f
63
63
  end
64
64
  end
65
65
 
@@ -78,8 +78,8 @@ module RCAP
78
78
  # @return [Point]
79
79
  def self.from_a( point_array )
80
80
  self.new do |point|
81
- point.lattitude = point_array[ LATTITUDE_INDEX ]
82
- point.longitude = point_array[ LONGITUDE_INDEX ]
81
+ point.lattitude = point_array[ LATTITUDE_INDEX ].to_f
82
+ point.longitude = point_array[ LONGITUDE_INDEX ].to_f
83
83
  end
84
84
  end
85
85
  end
@@ -53,8 +53,8 @@ module RCAP
53
53
  self.new do |polygon|
54
54
  coordinates.each do |lattitude, longitude|
55
55
  polygon.add_point do |point|
56
- point.lattitude = lattitude
57
- point.longitude = longitude
56
+ point.lattitude = lattitude.to_f
57
+ point.longitude = longitude.to_f
58
58
  end
59
59
  end
60
60
  end
@@ -85,8 +85,8 @@ module RCAP
85
85
  self.new do |polygon|
86
86
  Array( polygon_yaml_data ).each do |lattitude, longitude|
87
87
  polygon.add_point do |point|
88
- point.lattitude = lattitude
89
- point.longitude = longitude
88
+ point.lattitude = lattitude.to_f
89
+ point.longitude = longitude.to_f
90
90
  end
91
91
  end
92
92
  end
@@ -104,8 +104,8 @@ module RCAP
104
104
  self.new do |polygon|
105
105
  Array( polygon_hash[ POINTS_KEY ]).each do |point_array|
106
106
  polygon.add_point do |point|
107
- point.lattitude = point_array[ Point::LATTITUDE_INDEX ]
108
- point.longitude = point_array[ Point::LONGITUDE_INDEX ]
107
+ point.lattitude = point_array[ Point::LATTITUDE_INDEX ].to_f
108
+ point.longitude = point_array[ Point::LONGITUDE_INDEX ].to_f
109
109
  end
110
110
  end
111
111
  end
@@ -156,11 +156,11 @@ module RCAP
156
156
  # @return [Resource]
157
157
  def self.from_h( resource_hash )
158
158
  self.new do |resource|
159
- resource.resource_desc = resource_hash[ RESOURCE_DESC_KEY ]
160
- resource.uri = resource_hash[ URI_KEY ]
161
- resource.mime_type = resource_hash[ MIME_TYPE_KEY ]
162
- resource.size = resource_hash[ SIZE_KEY ]
163
- resource.digest = resource_hash[ DIGEST_KEY ]
159
+ resource.resource_desc = RCAP.strip_if_given( resource_hash[ RESOURCE_DESC_KEY ])
160
+ resource.uri = RCAP.strip_if_given( resource_hash[ URI_KEY ])
161
+ resource.mime_type = RCAP.strip_if_given( resource_hash[ MIME_TYPE_KEY ])
162
+ resource.size = RCAP.to_i_if_given( resource_hash[ SIZE_KEY ])
163
+ resource.digest = RCAP.strip_if_given( resource_hash[ DIGEST_KEY ])
164
164
  end
165
165
  end
166
166
  end
@@ -124,7 +124,7 @@ module RCAP
124
124
  # @return [RCAP::CAP_1_0::Alert]
125
125
  def self.from_yaml_data( alert_yaml_data )
126
126
  super.tap do |alert|
127
- alert.password = alert_yaml_data[ PASSWORD_YAML ]
127
+ alert.password = RCAP.strip_if_given( alert_yaml_data[ PASSWORD_YAML ])
128
128
  end
129
129
  end
130
130
 
@@ -158,7 +158,7 @@ module RCAP
158
158
  # @return [RCAP::CAP_1_0::Alert]
159
159
  def self.from_h( alert_hash )
160
160
  super.tap do |alert|
161
- alert.password = alert_hash[ PASSWORD_KEY ]
161
+ alert.password = RCAP.strip_if_given( alert_hash[ PASSWORD_KEY ])
162
162
  end
163
163
  end
164
164
  end
@@ -17,8 +17,8 @@ module RCAP
17
17
  def self.from_xml_element( parameter_xml_element )
18
18
  parameter_hash = self.parse_parameter( parameter_xml_element.text )
19
19
  self.new do |parameter|
20
- parameter.name = parameter_hash[ :name ]
21
- parameter.value = parameter_hash[ :value ]
20
+ parameter.name = RCAP.strip_if_given( parameter_hash[ :name ])
21
+ parameter.value = RCAP.strip_if_given( parameter_hash[ :value ])
22
22
  end
23
23
  end
24
24
 
@@ -27,8 +27,8 @@ module RCAP
27
27
  def self.parse_parameter( parameter_string )
28
28
  name, value = parameter_string.split("=")
29
29
  if name && value
30
- { :name => name,
31
- :value => value }
30
+ { :name => RCAP.strip_if_given( name ),
31
+ :value => RCAP.strip_if_given( value )}
32
32
  end
33
33
  end
34
34
  end
@@ -144,7 +144,7 @@ module RCAP
144
144
  def self.from_xml_element( info_xml_element )
145
145
  super.tap do |info|
146
146
  RCAP.xpath_match( info_xml_element, RESPONSE_TYPE_XPATH, info.xmlns ).each do |element|
147
- info.response_types << element.text
147
+ info.response_types << RCAP.strip_if_given( element.text )
148
148
  end
149
149
  end
150
150
  end
@@ -222,7 +222,7 @@ module RCAP
222
222
  def self.from_yaml_data( info_yaml_data )
223
223
  super.tap do |info|
224
224
  Array( info_yaml_data [ RESPONSE_TYPES_YAML ] ).each do |response_type|
225
- info.response_types << response_type
225
+ info.response_types << RCAP.strip_if_given( response_type )
226
226
  end
227
227
  end
228
228
  end
@@ -259,7 +259,7 @@ module RCAP
259
259
  def self.from_h( info_hash )
260
260
  super.tap do |info|
261
261
  Array( info_hash[ RESPONSE_TYPES_KEY ]).each do |response_type|
262
- info.response_types << response_type
262
+ info.response_types << RCAP.strip_if_given( response_type )
263
263
  end
264
264
  end
265
265
  end
@@ -70,7 +70,7 @@ module RCAP
70
70
  # @return [Resource]
71
71
  def self.from_yaml_data( resource_yaml_data )
72
72
  super.tap do |resource|
73
- resource.deref_uri = resource_yaml_data[ DEREF_URI_YAML ]
73
+ resource.deref_uri = RCAP.strip_if_given( resource_yaml_data[ DEREF_URI_YAML ])
74
74
  end
75
75
  end
76
76
 
@@ -90,7 +90,7 @@ module RCAP
90
90
  # @return [Resource]
91
91
  def self.from_h( resource_hash )
92
92
  super.tap do |resource|
93
- resource.deref_uri = resource_hash[ DEREF_URI_KEY ]
93
+ resource.deref_uri = RCAP.strip_if_given( resource_hash[ DEREF_URI_KEY ])
94
94
  end
95
95
  end
96
96
  end
@@ -157,7 +157,7 @@ module RCAP
157
157
  def self.from_xml_element( info_xml_element )
158
158
  super.tap do |info|
159
159
  RCAP.xpath_match( info_xml_element, RESPONSE_TYPE_XPATH, Alert::XMLNS ).each do |element|
160
- info.response_types << element.text
160
+ info.response_types << RCAP.strip_if_given( element.text )
161
161
  end
162
162
  end
163
163
  end
@@ -196,7 +196,7 @@ module RCAP
196
196
  def self.from_yaml_data( info_yaml_data )
197
197
  super.tap do |info|
198
198
  Array( info_yaml_data [ RESPONSE_TYPES_YAML ]).each do |response_type|
199
- info.response_types << response_type
199
+ info.response_types << RCAP.strip_if_given( response_type )
200
200
  end
201
201
  end
202
202
  end
@@ -233,7 +233,7 @@ module RCAP
233
233
  def self.from_h( info_hash )
234
234
  super.tap do |info|
235
235
  Array( info_hash[ RESPONSE_TYPES_KEY ]).each do |response_type|
236
- info.response_types << response_type
236
+ info.response_types << RCAP.strip_if_given( response_type )
237
237
  end
238
238
  end
239
239
  end
@@ -75,7 +75,7 @@ module RCAP
75
75
  # @return [Resource]
76
76
  def self.from_yaml_data( resource_yaml_data )
77
77
  super.tap do |resource|
78
- resource.deref_uri = resource_yaml_data[ DEREF_URI_YAML ]
78
+ resource.deref_uri = resource_yaml_data[ DEREF_URI_YAML ].strip if resource_yaml_data[ DEREF_URI_YAML ]
79
79
  end
80
80
  end
81
81
 
@@ -95,7 +95,7 @@ module RCAP
95
95
  # @return [Resource]
96
96
  def self.from_h( resource_hash )
97
97
  super.tap do |resource|
98
- resource.deref_uri = resource_hash[ DEREF_URI_KEY ]
98
+ resource.deref_uri = resource_hash[ DEREF_URI_KEY ].strip if resource_hash[ DEREF_URI_KEY ]
99
99
  end
100
100
  end
101
101
  end
@@ -17,7 +17,7 @@ module RCAP
17
17
  # @return [String,nil] Text content of element matching XPath query or nil
18
18
  def self.xpath_text( xml_element, xpath, namespace )
19
19
  element = self.xpath_first( xml_element, xpath, namespace )
20
- element.text if element
20
+ element.text.strip if element && element.text
21
21
  end
22
22
 
23
23
  # Returns first descendent that matches the given XPath expression.
@@ -57,11 +57,11 @@ module RCAP
57
57
  # # | three |
58
58
  # # '-------'
59
59
  def self.format_lines_for_inspect( header, inspect_string )
60
- max_line_length = inspect_string.lines.map{ |line| line.chomp.length }.max
60
+ max_line_length = inspect_string.lines.map{ |line| line.strip.length }.max
61
61
  "\n." + '-' * (max_line_length + 2) + ".\n"+
62
62
  '| ' + header.ljust( max_line_length ) + " |\n"+
63
63
  '|' + '-' * ( max_line_length + 2 ) + "|\n"+
64
- inspect_string.lines.map{ |line| '| ' + line.chomp.ljust( max_line_length ) +' |'}.join( "\n" ) + "\n" +
64
+ inspect_string.lines.map{ |line| '| ' + line.strip.ljust( max_line_length ) +' |'}.join( "\n" ) + "\n" +
65
65
  "'" + '-' * ( max_line_length + 2 ) + "'\n"
66
66
  end
67
67
 
@@ -90,13 +90,77 @@ module RCAP
90
90
  end
91
91
  end
92
92
 
93
- # If the parameter is a string the datetime is parsed out of it, otherwise returns nil.
93
+ # If the parameter is a string and not empty the datetime is parsed out of it, otherwise returns nil.
94
94
  #
95
- # @param [String] date_string String to parse
95
+ # @param [String] date String to parse
96
96
  # @return [String,nil]
97
- def self.parse_datetime( date_string )
98
- if date_string.is_a?( String )
99
- DateTime.parse( date_string )
97
+ def self.parse_datetime( date )
98
+ case date
99
+ when String
100
+ if !date.empty?
101
+ DateTime.parse( date.strip )
102
+ end
103
+ when DateTime, Time, Date
104
+ date.to_datetime
105
+ end
106
+ end
107
+
108
+ # If the list is given will split it with the separator parameter otherwise
109
+ # retun an empty array.
110
+ #
111
+ # @param [String] list List to split
112
+ # @return [Array]
113
+ def RCAP.unpack_if_given( list )
114
+ if list
115
+ list.unpack_cap_list
116
+ else
117
+ []
118
+ end
119
+ end
120
+
121
+ # If the string given is not nil, String#strip is called otherwise nil is returned
122
+ #
123
+ # @param [String] string
124
+ # @return [String,nil]
125
+ def RCAP.strip_if_given( string )
126
+ if string
127
+ string.strip
128
+ end
129
+ end
130
+
131
+ # if the string is given, String#strip and then String#to_f are applied
132
+ # otherwise nil is returned.
133
+ #
134
+ # @param [String] string
135
+ # @return [Float,nil]
136
+ def RCAP.to_f_if_given( number )
137
+ if number
138
+ case number
139
+ when String
140
+ number.strip.to_f
141
+ when Numeric
142
+ number.to_f
143
+ else
144
+ number.to_s.strip.to_f
145
+ end
146
+ end
147
+ end
148
+
149
+ # if the string is given, String#strip and then String#to_i are applied
150
+ # otherwise nil is returned.
151
+ #
152
+ # @param [String] string
153
+ # @return [Integer,nil]
154
+ def RCAP.to_i_if_given( number )
155
+ if number
156
+ case number
157
+ when String
158
+ number.strip.to_i
159
+ when Numeric
160
+ number.to_i
161
+ else
162
+ number.to_s.to_i
163
+ end
100
164
  end
101
165
  end
102
166
  end
data/lib/rcap/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module RCAP
2
- VERSION = '2.1.0'
2
+ VERSION = '2.2.0'
3
3
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: rcap
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 2.1.0
5
+ version: 2.2.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Farrel Lifson
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2013-01-12 00:00:00 Z
13
+ date: 2013-02-03 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: assistance
@@ -217,7 +217,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
217
217
  requirements: []
218
218
 
219
219
  rubyforge_project: rcap
220
- rubygems_version: 1.8.10
220
+ rubygems_version: 1.8.19
221
221
  signing_key:
222
222
  specification_version: 3
223
223
  summary: CAP(Common Alerting Protocol) API