rcap 0.3 → 0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.rdoc +6 -0
- data/README.rdoc +44 -10
- data/lib/rcap.rb +3 -1
- data/lib/rcap/alert.rb +165 -103
- data/lib/rcap/area.rb +28 -2
- data/lib/rcap/circle.rb +27 -19
- data/lib/rcap/info.rb +73 -3
- data/lib/rcap/parameter.rb +10 -0
- data/lib/rcap/point.rb +13 -0
- data/lib/rcap/polygon.rb +10 -0
- data/lib/rcap/resource.rb +31 -4
- data/lib/rcap/utilities.rb +46 -26
- data/lib/rcap/validations.rb +17 -17
- data/spec/alert_spec.rb +52 -13
- data/spec/area_spec.rb +79 -20
- data/spec/circle_spec.rb +39 -10
- data/spec/geocode_spec.rb +18 -6
- data/spec/info_spec.rb +172 -21
- data/spec/point_spec.rb +13 -1
- data/spec/polygon_spec.rb +24 -1
- data/spec/resource_spec.rb +88 -6
- data/spec/spec_helper.rb +2 -1
- data/spec/utilities_spec.rb +1 -1
- data/spec/validations_spec.rb +1 -1
- metadata +36 -12
data/lib/rcap/area.rb
CHANGED
@@ -103,10 +103,10 @@ EOF
|
|
103
103
|
POLYGONS_YAML = 'Polygons' # :nodoc:
|
104
104
|
|
105
105
|
def to_yaml( options = {} ) # :nodoc:
|
106
|
-
circles_yaml = self.circles.map{ |circle| [
|
106
|
+
circles_yaml = self.circles.map{ |circle| [ circle.lattitude, circle.longitude, circle.radius ]}
|
107
107
|
def circles_yaml.to_yaml_style; :inline; end
|
108
108
|
|
109
|
-
RCAP.
|
109
|
+
RCAP.attribute_values_to_hash(
|
110
110
|
[ AREA_DESC_YAML, self.area_desc ],
|
111
111
|
[ ALTITUDE_YAML, self.altitude ],
|
112
112
|
[ CEILING_YAML, self.ceiling ],
|
@@ -126,5 +126,31 @@ EOF
|
|
126
126
|
:polygons => Array( area_yaml_data[ POLYGONS_YAML ]).map{ |polyon_yaml_data| RCAP::Polygon.from_yaml_data( polyon_yaml_data )}
|
127
127
|
)
|
128
128
|
end
|
129
|
+
|
130
|
+
AREA_DESC_KEY = 'area_desc' # :nodoc:
|
131
|
+
ALTITUDE_KEY = 'altitude' # :nodoc:
|
132
|
+
CEILING_KEY = 'ceiling' # :nodoc:
|
133
|
+
CIRCLES_KEY = 'circles' # :nodoc:
|
134
|
+
GEOCODES_KEY = 'geocodes' # :nodoc:
|
135
|
+
POLYGONS_KEY = 'polygons' # :nodoc:
|
136
|
+
|
137
|
+
def to_h # :nodoc:
|
138
|
+
{ AREA_DESC_KEY => self.area_desc,
|
139
|
+
ALTITUDE_KEY => self.altitude,
|
140
|
+
CEILING_KEY => self.ceiling,
|
141
|
+
CIRCLES_KEY => self.circles.map{ |circle| circle.to_h },
|
142
|
+
GEOCODES_KEY => self.geocodes.map{ |geocode| geocode.to_h },
|
143
|
+
POLYGONS_KEY => self.polygons.map{ |polygon| polygon.to_h }}
|
144
|
+
end
|
145
|
+
|
146
|
+
def self.from_h( area_hash ) # :nodoc:
|
147
|
+
self.new(
|
148
|
+
:area_desc => area_hash[ AREA_DESC_KEY ],
|
149
|
+
:altitude => area_hash[ ALTITUDE_KEY ],
|
150
|
+
:ceiling => area_hash[ CEILING_KEY ],
|
151
|
+
:circles => area_hash[ CIRCLES_KEY ].map{ |circle_hash| RCAP::Circle.from_h( circle_hash )},
|
152
|
+
:geocodes => area_hash[ GEOCODES_KEY ].map{ |geocode_hash| RCAP::Geocode.from_h( geocode_hash )},
|
153
|
+
:polygons => area_hash[ POLYGONS_KEY ].map{ |polygon_hash| RCAP::Polygon.from_h( polygon_hash )})
|
154
|
+
end
|
129
155
|
end
|
130
156
|
end
|
data/lib/rcap/circle.rb
CHANGED
@@ -1,36 +1,33 @@
|
|
1
1
|
module RCAP
|
2
2
|
# A Circle object is valid if
|
3
|
-
|
3
|
+
# * it has a valid lattitude and longitude
|
4
4
|
# * it has a radius with a value greater than zero
|
5
|
-
class Circle
|
5
|
+
class Circle < Point
|
6
6
|
include Validation
|
7
7
|
|
8
|
-
# Instance of Point class
|
9
|
-
attr_accessor( :point )
|
10
8
|
# Expresed in kilometers
|
11
9
|
attr_accessor( :radius )
|
12
10
|
|
13
|
-
validates_presence_of( :
|
11
|
+
validates_presence_of( :radius )
|
14
12
|
validates_numericality_of( :radius , :greater_than => 0 )
|
15
|
-
validates_validity_of( :point )
|
16
13
|
|
17
14
|
XML_ELEMENT_NAME = 'circle' # :nodoc:
|
18
15
|
|
19
16
|
XPATH = 'cap:circle' # :nodoc:
|
20
17
|
|
21
18
|
def initialize( attributes = {} )
|
22
|
-
|
19
|
+
super( attributes )
|
23
20
|
@radius = attributes[ :radius ]
|
24
21
|
end
|
25
22
|
|
26
23
|
# Returns a string representation of the circle of the form
|
27
|
-
#
|
24
|
+
# lattitude,longitude,radius
|
28
25
|
def to_s # :nodoc:
|
29
|
-
"#{ self.
|
26
|
+
"#{ self.lattitude },#{ self.longitude },#{ self.radius }"
|
30
27
|
end
|
31
28
|
|
32
29
|
def inspect # :nodoc:
|
33
|
-
"(#{ self.
|
30
|
+
"(#{ self.lattitude},#{ self.longitude },#{ self.radius })"
|
34
31
|
end
|
35
32
|
|
36
33
|
def to_xml_element # :nodoc:
|
@@ -44,27 +41,38 @@ module RCAP
|
|
44
41
|
end
|
45
42
|
|
46
43
|
def self.parse_circle_string( circle_string ) # :nodoc:
|
47
|
-
|
48
|
-
lattitude, longitude = coordinates.split( ',' )
|
44
|
+
lattitude, longitude, radius = circle_string.split( ',' )
|
49
45
|
[ lattitude, longitude, radius ].map{ |e| e.to_f }
|
50
46
|
end
|
51
47
|
|
52
48
|
def self.from_xml_element( circle_xml_element ) # :nodoc:
|
53
49
|
lattitude, longitude, radius = self.parse_circle_string( circle_xml_element.text )
|
54
|
-
|
55
|
-
|
50
|
+
circle = self.new( :lattitude => lattitude,
|
51
|
+
:longitude => longitude,
|
56
52
|
:radius => radius )
|
57
53
|
end
|
58
54
|
|
59
|
-
# Two circles are equivalent if their
|
55
|
+
# Two circles are equivalent if their lattitude, longitude and radius are equal.
|
60
56
|
def ==( other )
|
61
|
-
self.
|
57
|
+
[ self.lattitude, self.longitude, self.radius ] == [ other.lattitude, other.longitude, other.radius ]
|
62
58
|
end
|
63
59
|
|
64
60
|
def self.from_yaml_data( circle_yaml_data ) # :nodoc:
|
65
|
-
|
66
|
-
self.new( :
|
67
|
-
|
61
|
+
lattitude, longitude,radius = circle_yaml_data
|
62
|
+
self.new( :lattitude => lattitude, :longitude => longitude, :radius => radius )
|
63
|
+
end
|
64
|
+
|
65
|
+
RADIUS_KEY = 'radius' # :nodoc:
|
66
|
+
LATTITUDE_KEY = 'lattitude' # :nodoc:
|
67
|
+
LONGITUDE_KEY = 'longitude' # :nodoc:
|
68
|
+
def to_h # :nodoc:
|
69
|
+
RCAP.attribute_values_to_hash( [ RADIUS_KEY, self.radius ],
|
70
|
+
[ LATTITUDE_KEY, self.lattitude ],
|
71
|
+
[ LONGITUDE_KEY, self.longitude ])
|
72
|
+
end
|
73
|
+
|
74
|
+
def self.from_h( circle_hash ) # :nodoc:
|
75
|
+
self.new( :radius => circle_hash[ RADIUS_KEY ], :lattitude => circle_hash[ LATTITUDE_KEY ], :longitude => circle_hash[ LONGITUDE_KEY ])
|
68
76
|
end
|
69
77
|
end
|
70
78
|
end
|
data/lib/rcap/info.rb
CHANGED
@@ -113,8 +113,8 @@ module RCAP
|
|
113
113
|
validates_presence_of( :event, :urgency, :severity, :certainty )
|
114
114
|
validates_length_of( :categories, :minimum => 1 )
|
115
115
|
validates_inclusion_of( :certainty, :allow_nil => true, :in => VALID_CERTAINTIES, :message => "can only be assigned the following values: #{ VALID_CERTAINTIES.join(', ') }")
|
116
|
-
validates_inclusion_of( :severity, :allow_nil => true, :in => VALID_SEVERITIES,
|
117
|
-
validates_inclusion_of( :urgency, :allow_nil => true, :in => VALID_URGENCIES,
|
116
|
+
validates_inclusion_of( :severity, :allow_nil => true, :in => VALID_SEVERITIES, :message => "can only be assigned the following values: #{ VALID_SEVERITIES.join(', ') }" )
|
117
|
+
validates_inclusion_of( :urgency, :allow_nil => true, :in => VALID_URGENCIES, :message => "can only be assigned the following values: #{ VALID_URGENCIES.join(', ') }" )
|
118
118
|
validates_inclusion_of_members_of( :response_types, :in => VALID_RESPONSE_TYPES, :allow_blank => true )
|
119
119
|
validates_inclusion_of_members_of( :categories, :in => VALID_CATEGORIES, :allow_blank => true )
|
120
120
|
validates_collection_of( :resources, :areas )
|
@@ -313,7 +313,7 @@ EOF
|
|
313
313
|
|
314
314
|
parameter_to_hash = lambda{ |hash, parameter| hash.merge( parameter.name => parameter.value )}
|
315
315
|
|
316
|
-
RCAP.
|
316
|
+
RCAP.attribute_values_to_hash(
|
317
317
|
[ LANGUAGE_YAML, self.language ],
|
318
318
|
[ CATEGORIES_YAML, categories_yaml ],
|
319
319
|
[ EVENT_YAML, self.event ],
|
@@ -363,5 +363,75 @@ EOF
|
|
363
363
|
:areas => Array( info_yaml_data [ AREAS_YAML ]).map{ |area_yaml_data| RCAP::Area.from_yaml_data( area_yaml_data )}
|
364
364
|
)
|
365
365
|
end
|
366
|
+
|
367
|
+
LANGUAGE_KEY = 'language' # :nodoc:
|
368
|
+
CATEGORIES_KEY = 'categories' # :nodoc:
|
369
|
+
EVENT_KEY = 'event' # :nodoc:
|
370
|
+
RESPONSE_TYPES_KEY = 'response_types' # :nodoc:
|
371
|
+
URGENCY_KEY = 'urgency' # :nodoc:
|
372
|
+
SEVERITY_KEY = 'severity' # :nodoc:
|
373
|
+
CERTAINTY_KEY = 'certainty' # :nodoc:
|
374
|
+
AUDIENCE_KEY = 'audience' # :nodoc:
|
375
|
+
EFFECTIVE_KEY = 'effective' # :nodoc:
|
376
|
+
ONSET_KEY = 'onset' # :nodoc:
|
377
|
+
EXPIRES_KEY = 'expires' # :nodoc:
|
378
|
+
SENDER_NAME_KEY = 'sender_name' # :nodoc:
|
379
|
+
HEADLINE_KEY = 'headline' # :nodoc:
|
380
|
+
DESCRIPTION_KEY = 'description' # :nodoc:
|
381
|
+
INSTRUCTION_KEY = 'instruction' # :nodoc:
|
382
|
+
WEB_KEY = 'web' # :nodoc:
|
383
|
+
CONTACT_KEY = 'contact' # :nodoc:
|
384
|
+
RESOURCES_KEY = 'resources' # :nodoc:
|
385
|
+
EVENT_CODES_KEY = 'event_codes' # :nodoc:
|
386
|
+
PARAMETERS_KEY = 'parameters' # :nodoc:
|
387
|
+
AREAS_KEY = 'areas' # :nodoc:
|
388
|
+
|
389
|
+
def to_h # :nodoc:
|
390
|
+
RCAP.attribute_values_to_hash( [ LANGUAGE_KEY, self.language ],
|
391
|
+
[ CATEGORIES_KEY, self.categories ],
|
392
|
+
[ EVENT_KEY, self.event ],
|
393
|
+
[ RESPONSE_TYPES_KEY, self.response_types ],
|
394
|
+
[ URGENCY_KEY, self.urgency ],
|
395
|
+
[ SEVERITY_KEY, self.severity ],
|
396
|
+
[ CERTAINTY_KEY, self.certainty ],
|
397
|
+
[ AUDIENCE_KEY, self.audience ],
|
398
|
+
[ EFFECTIVE_KEY, RCAP.to_s_for_cap( self.effective )],
|
399
|
+
[ ONSET_KEY, RCAP.to_s_for_cap( self.onset )],
|
400
|
+
[ EXPIRES_KEY, RCAP.to_s_for_cap( self.expires )],
|
401
|
+
[ SENDER_NAME_KEY, self.sender_name ],
|
402
|
+
[ HEADLINE_KEY, self.headline ],
|
403
|
+
[ DESCRIPTION_KEY, self.description ],
|
404
|
+
[ INSTRUCTION_KEY, self.instruction ],
|
405
|
+
[ WEB_KEY, self.web ],
|
406
|
+
[ CONTACT_KEY, self.contact ],
|
407
|
+
[ RESOURCES_KEY, self.resources.map{ |resource| resource.to_h } ],
|
408
|
+
[ EVENT_CODES_KEY, self.event_codes.map{ |event_code| event_code.to_h } ],
|
409
|
+
[ PARAMETERS_KEY, self.parameters.map{ |parameter| parameter.to_h } ],
|
410
|
+
[ AREAS_KEY, self.areas.map{ |area| area.to_h }])
|
411
|
+
end
|
412
|
+
|
413
|
+
def self.from_h( info_hash ) # :nodoc:
|
414
|
+
self.new( :language => info_hash[ LANGUAGE_KEY ],
|
415
|
+
:categories => info_hash[ CATEGORIES_KEY ],
|
416
|
+
:event => info_hash[ EVENT_KEY ],
|
417
|
+
:response_types => info_hash[ RESPONSE_TYPES_KEY ],
|
418
|
+
:urgency => info_hash[ URGENCY_KEY ],
|
419
|
+
:severity => info_hash[ SEVERITY_KEY ],
|
420
|
+
:certainty => info_hash[ CERTAINTY_KEY ],
|
421
|
+
:audience => info_hash[ AUDIENCE_KEY ],
|
422
|
+
:effective => RCAP.parse_datetime( info_hash[ EFFECTIVE_KEY ]),
|
423
|
+
:onset => RCAP.parse_datetime( info_hash[ ONSET_KEY ]),
|
424
|
+
:expires => RCAP.parse_datetime( info_hash[ EXPIRES_KEY ]),
|
425
|
+
:sender_name => info_hash[ SENDER_NAME_KEY ],
|
426
|
+
:headline => info_hash[ HEADLINE_KEY ],
|
427
|
+
:description => info_hash[ DESCRIPTION_KEY ],
|
428
|
+
:instruction => info_hash[ INSTRUCTION_KEY ],
|
429
|
+
:web => info_hash[ WEB_KEY ],
|
430
|
+
:contact => info_hash[ CONTACT_KEY ],
|
431
|
+
:resources => Array( info_hash[ RESOURCES_KEY ]).map{ |resource_hash| RCAP::Resource.from_h( resource_hash ) },
|
432
|
+
:event_codes => Array( info_hash[ EVENT_CODES_KEY ]).map{ |event_code_hash| RCAP::EventCode.from_h( event_code_hash )},
|
433
|
+
:parameters => Array( info_hash[ PARAMETERS_KEY ]).map{ |parameter_hash| RCAP::Parameter.from_h( parameter_hash )},
|
434
|
+
:areas => Array( info_hash[ AREAS_KEY ]).map{ |area_hash| RCAP::Area.from_h( area_hash )})
|
435
|
+
end
|
366
436
|
end
|
367
437
|
end
|
data/lib/rcap/parameter.rb
CHANGED
@@ -52,5 +52,15 @@ module RCAP
|
|
52
52
|
def ==( other )
|
53
53
|
[ self.name, self.value ] == [ other.name, other.value ]
|
54
54
|
end
|
55
|
+
|
56
|
+
def to_h # :nodoc:
|
57
|
+
RCAP.attribute_values_to_hash(
|
58
|
+
[ @name, @value ])
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.from_h( hash ) # :nodoc:
|
62
|
+
key = hash.keys.first
|
63
|
+
self.new( :name => key, :value => hash[ key ])
|
64
|
+
end
|
55
65
|
end
|
56
66
|
end
|
data/lib/rcap/point.rb
CHANGED
@@ -36,5 +36,18 @@ module RCAP
|
|
36
36
|
def ==( other )
|
37
37
|
[ self.lattitude, self.longitude ] == [ other.lattitude, other.longitude ]
|
38
38
|
end
|
39
|
+
|
40
|
+
LATTITUDE_KEY = 'lattitude_hash' # :nodoc:
|
41
|
+
LONGITUDE_KEY = 'longitude_hash' # :nodoc:
|
42
|
+
|
43
|
+
def to_h # :nodoc:
|
44
|
+
RCAP.attribute_values_to_hash(
|
45
|
+
[ LATTITUDE_KEY, self.lattitude ],
|
46
|
+
[ LONGITUDE_KEY, self.longitude ])
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.from_h( point_hash ) # :nodoc:
|
50
|
+
self.new( :lattitude => point_hash[ LATTITUDE_KEY ], :longitude => point_hash[ LONGITUDE_KEY ])
|
51
|
+
end
|
39
52
|
end
|
40
53
|
end
|
data/lib/rcap/polygon.rb
CHANGED
@@ -63,5 +63,15 @@ module RCAP
|
|
63
63
|
:points => Array( polygon_yaml_data ).map{ |lattitude, longitude| Point.new( :lattitude => lattitude, :longitude => longitude )}
|
64
64
|
)
|
65
65
|
end
|
66
|
+
|
67
|
+
POINTS_KEY = 'points' # :nodoc:
|
68
|
+
|
69
|
+
def to_h # :nodoc:
|
70
|
+
{ POINTS_KEY => self.points.map{ |point| point.to_h }}
|
71
|
+
end
|
72
|
+
|
73
|
+
def self.from_h( polygon_hash ) # :nodoc:
|
74
|
+
self.new( :points => polygon_hash[ POINTS_KEY ].map{ |point_hash| Point.from_h( point_hash )})
|
75
|
+
end
|
66
76
|
end
|
67
77
|
end
|
data/lib/rcap/resource.rb
CHANGED
@@ -47,10 +47,10 @@ module RCAP
|
|
47
47
|
xml_element = REXML::Element.new( XML_ELEMENT_NAME )
|
48
48
|
xml_element.add_element( RESOURCE_DESC_ELEMENT_NAME ).add_text( self.resource_desc )
|
49
49
|
xml_element.add_element( MIME_TYPE_ELEMENT_NAME ).add_text( self.mime_type ) if self.mime_type
|
50
|
-
xml_element.add_element( SIZE_ELEMENT_NAME ).add_text( self.size )
|
51
|
-
xml_element.add_element( URI_ELEMENT_NAME ).add_text( self.uri )
|
50
|
+
xml_element.add_element( SIZE_ELEMENT_NAME ).add_text( self.size ) if self.size
|
51
|
+
xml_element.add_element( URI_ELEMENT_NAME ).add_text( self.uri ) if self.uri
|
52
52
|
xml_element.add_element( DEREF_URI_ELEMENT_NAME ).add_text( self.deref_uri ) if self.deref_uri
|
53
|
-
xml_element.add_element( DIGEST_ELEMENT_NAME ).add_text( self.digest )
|
53
|
+
xml_element.add_element( DIGEST_ELEMENT_NAME ).add_text( self.digest ) if self.digest
|
54
54
|
xml_element
|
55
55
|
end
|
56
56
|
|
@@ -92,7 +92,7 @@ module RCAP
|
|
92
92
|
DIGEST_YAML = "Digest" # :nodoc:
|
93
93
|
|
94
94
|
def to_yaml( options ) # :nodoc:
|
95
|
-
RCAP.
|
95
|
+
RCAP.attribute_values_to_hash(
|
96
96
|
[ RESOURCE_DESC_YAML, self.resource_desc ],
|
97
97
|
[ URI_YAML, self.uri ],
|
98
98
|
[ MIME_TYPE_YAML, self.mime_type ],
|
@@ -112,5 +112,32 @@ module RCAP
|
|
112
112
|
:digest => reource_yaml_data[ DIGEST_YAML ]
|
113
113
|
)
|
114
114
|
end
|
115
|
+
|
116
|
+
RESOURCE_DESC_KEY = 'resource_desc' # :nodoc:
|
117
|
+
URI_KEY = 'uri' # :nodoc:
|
118
|
+
MIME_TYPE_KEY = 'mime_type' # :nodoc:
|
119
|
+
DEREF_URI_KEY = 'deref_uri' # :nodoc:
|
120
|
+
SIZE_KEY = 'size' # :nodoc:
|
121
|
+
DIGEST_KEY = 'digest' # :nodoc:
|
122
|
+
|
123
|
+
def to_h # :nodoc:
|
124
|
+
RCAP.attribute_values_to_hash(
|
125
|
+
[ RESOURCE_DESC_KEY, self.resource_desc ],
|
126
|
+
[ URI_KEY, self.uri],
|
127
|
+
[ MIME_TYPE_KEY, self.mime_type],
|
128
|
+
[ DEREF_URI_KEY, self.deref_uri],
|
129
|
+
[ SIZE_KEY, self.size ],
|
130
|
+
[ DIGEST_KEY, self.digest ])
|
131
|
+
end
|
132
|
+
|
133
|
+
def self.from_h( resource_hash ) # :nodoc:
|
134
|
+
self.new(
|
135
|
+
:resource_desc => resource_hash[ RESOURCE_DESC_KEY ],
|
136
|
+
:uri => resource_hash[ URI_KEY ],
|
137
|
+
:mime_type => resource_hash[ MIME_TYPE_KEY ],
|
138
|
+
:deref_uri => resource_hash[ DEREF_URI_KEY ],
|
139
|
+
:size => resource_hash[ SIZE_KEY ],
|
140
|
+
:digest => resource_hash[ DIGEST_KEY ])
|
141
|
+
end
|
115
142
|
end
|
116
143
|
end
|
data/lib/rcap/utilities.rb
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
ALLOWED_CHARACTERS = /[^\s&<]+/ # :nodoc:
|
2
2
|
|
3
|
-
class Array # :nodoc:
|
4
|
-
|
5
|
-
|
3
|
+
class Array # :nodoc:
|
4
|
+
def to_s_for_cap
|
5
|
+
self.map{ |element| element.to_s.for_cap_list }.join( ' ' )
|
6
|
+
end
|
6
7
|
end
|
7
|
-
end
|
8
8
|
|
9
9
|
class String # :nodoc:
|
10
|
-
CAP_LIST_REGEX =
|
11
|
-
WHITESPACE_REGEX =
|
10
|
+
CAP_LIST_REGEX = Regexp.new( '"([\w\s]+)"|(\S+)' )
|
11
|
+
WHITESPACE_REGEX = Regexp.new('^\s+$')
|
12
12
|
|
13
13
|
def for_cap_list
|
14
14
|
if self =~ /\s/
|
@@ -24,43 +24,63 @@ class String # :nodoc:
|
|
24
24
|
end
|
25
25
|
|
26
26
|
class DateTime # :nodoc:
|
27
|
-
|
27
|
+
alias inspect to_s
|
28
28
|
alias to_s_for_cap to_s
|
29
29
|
end
|
30
30
|
|
31
31
|
class Time # :nodoc:
|
32
|
-
|
33
|
-
|
32
|
+
RCAP_TIME_FORMAT = "%Y-%m-%dT%H:%M:%S"
|
33
|
+
RCAP_ZONE_FORMAT = "%+03i:00"
|
34
|
+
|
35
|
+
def to_s_for_cap
|
36
|
+
self.strftime( RCAP_TIME_FORMAT ) + format( RCAP_ZONE_FORMAT , self.utc_hours_offset )
|
37
|
+
end
|
34
38
|
|
35
|
-
|
36
|
-
|
39
|
+
def utc_hours_offset
|
40
|
+
self.utc_offset/3600
|
37
41
|
end
|
38
42
|
end
|
39
43
|
|
40
44
|
module RCAP # :nodoc:
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
+
def self.xpath_text( xml_element, xpath )
|
46
|
+
element = self.xpath_first( xml_element, xpath )
|
47
|
+
element.text if element
|
48
|
+
end
|
45
49
|
|
46
|
-
|
47
|
-
|
48
|
-
|
50
|
+
def self.xpath_first( xml_element, xpath )
|
51
|
+
REXML::XPath.first( xml_element, xpath, { 'cap' => RCAP::XMLNS })
|
52
|
+
end
|
49
53
|
|
50
|
-
|
51
|
-
|
52
|
-
|
54
|
+
def self.xpath_match( xml_element, xpath )
|
55
|
+
REXML::XPath.match( xml_element, xpath, { 'cap' => RCAP::XMLNS })
|
56
|
+
end
|
53
57
|
|
54
58
|
def self.format_lines_for_inspect( header, inspect_string )
|
55
59
|
max_line_length = inspect_string.lines.inject( 0 ){ |max_length, line| line.chomp.length > max_length ? line.chomp.length : max_length }
|
56
60
|
"\n." + '-' * max_line_length + ".\n"+
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
+
'|' + header.ljust( max_line_length ) + "|\n"+
|
62
|
+
'|' + '-' * max_line_length + "|\n"+
|
63
|
+
inspect_string.lines.map{ |line| '|' + line.chomp.ljust( max_line_length ) +'|'}.join( "\n" ) + "\n" +
|
64
|
+
"'" + '-' * max_line_length + "'\n"
|
61
65
|
end
|
62
66
|
|
63
|
-
def self.
|
67
|
+
def self.attribute_values_to_hash( *attribute_values )
|
64
68
|
Hash[ *attribute_values.reject{ |key, value| value.blank? }.flatten( 1 )]
|
65
69
|
end
|
70
|
+
|
71
|
+
def self.to_s_for_cap( object )
|
72
|
+
if object
|
73
|
+
if object.respond_to?( :to_s_for_cap )
|
74
|
+
object.to_s_for_cap
|
75
|
+
else
|
76
|
+
object.to_s
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def self.parse_datetime( date_string )
|
82
|
+
if date_string.is_a?( String )
|
83
|
+
DateTime.parse( date_string )
|
84
|
+
end
|
85
|
+
end
|
66
86
|
end
|
data/lib/rcap/validations.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
module Validation # :nodoc:
|
2
2
|
module ClassMethods # :nodoc:
|
3
3
|
|
4
|
-
|
5
|
-
|
4
|
+
CAP_NUMBER_REGEX = Regexp.new( '^-{0,1}\d*\.{0,1}\d+$' )
|
5
|
+
CAP_INTEGER_REGEX = Regexp.new( '\-{0,1}A[+-]?\d+\Z' )
|
6
6
|
|
7
7
|
def validates_inclusion_of( *attributes )
|
8
8
|
options = {
|
@@ -83,21 +83,21 @@ module Validation # :nodoc:
|
|
83
83
|
end
|
84
84
|
end
|
85
85
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
86
|
+
def validates_numericality_of( *attributes )
|
87
|
+
options = {
|
88
|
+
:message => 'is not a number',
|
89
|
+
}.merge!(attributes.extract_options!)
|
90
|
+
|
91
|
+
re = options[:only_integer] ? CAP_INTEGER_REGEX : CAP_NUMBER_REGEX
|
92
|
+
|
93
|
+
validates_each( *attributes ) do |object, attribute, value|
|
94
|
+
next if (value.nil? && options[ :allow_nil ]) || (value.blank? && options[ :allow_blank ])
|
95
|
+
unless ( value.to_s =~ re ) &&
|
96
|
+
( options[ :greater_than ].nil? || value && value > options[ :greater_than ])
|
97
|
+
object.errors[ attribute ] << options[ :message ]
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
101
|
|
102
102
|
|
103
103
|
def validates_responsiveness_of( *attributes )
|