rcap 1.3.1 → 2.0.0

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.
Files changed (88) hide show
  1. data/CHANGELOG.md +5 -0
  2. data/README.md +78 -151
  3. data/Rakefile +1 -1
  4. data/lib/rcap/alert.rb +2 -2
  5. data/lib/rcap/base/alert.rb +446 -0
  6. data/lib/rcap/base/area.rb +228 -0
  7. data/lib/rcap/base/circle.rb +121 -0
  8. data/lib/rcap/base/event_code.rb +6 -0
  9. data/lib/rcap/base/geocode.rb +6 -0
  10. data/lib/rcap/base/info.rb +498 -0
  11. data/lib/rcap/base/parameter.rb +88 -0
  12. data/lib/rcap/base/point.rb +87 -0
  13. data/lib/rcap/base/polygon.rb +120 -0
  14. data/lib/rcap/base/resource.rb +168 -0
  15. data/lib/rcap/cap_1_0/alert.rb +59 -342
  16. data/lib/rcap/cap_1_0/area.rb +13 -188
  17. data/lib/rcap/cap_1_0/circle.rb +2 -100
  18. data/lib/rcap/cap_1_0/event_code.rb +8 -3
  19. data/lib/rcap/cap_1_0/geocode.rb +8 -3
  20. data/lib/rcap/cap_1_0/info.rb +16 -468
  21. data/lib/rcap/cap_1_0/parameter.rb +9 -67
  22. data/lib/rcap/cap_1_0/point.rb +2 -61
  23. data/lib/rcap/cap_1_0/polygon.rb +5 -95
  24. data/lib/rcap/cap_1_0/resource.rb +4 -144
  25. data/lib/rcap/cap_1_1/alert.rb +7 -412
  26. data/lib/rcap/cap_1_1/area.rb +13 -188
  27. data/lib/rcap/cap_1_1/circle.rb +2 -100
  28. data/lib/rcap/cap_1_1/event_code.rb +8 -3
  29. data/lib/rcap/cap_1_1/geocode.rb +7 -3
  30. data/lib/rcap/cap_1_1/info.rb +127 -386
  31. data/lib/rcap/cap_1_1/parameter.rb +4 -76
  32. data/lib/rcap/cap_1_1/point.rb +2 -61
  33. data/lib/rcap/cap_1_1/polygon.rb +5 -95
  34. data/lib/rcap/cap_1_1/resource.rb +37 -143
  35. data/lib/rcap/cap_1_2/alert.rb +8 -413
  36. data/lib/rcap/cap_1_2/area.rb +13 -188
  37. data/lib/rcap/cap_1_2/circle.rb +2 -100
  38. data/lib/rcap/cap_1_2/event_code.rb +8 -3
  39. data/lib/rcap/cap_1_2/geocode.rb +8 -3
  40. data/lib/rcap/cap_1_2/info.rb +132 -419
  41. data/lib/rcap/cap_1_2/parameter.rb +4 -76
  42. data/lib/rcap/cap_1_2/point.rb +2 -61
  43. data/lib/rcap/cap_1_2/polygon.rb +5 -92
  44. data/lib/rcap/cap_1_2/resource.rb +31 -134
  45. data/lib/rcap/config.rb +3 -0
  46. data/lib/{extensions → rcap/extensions}/array.rb +1 -1
  47. data/lib/rcap/extensions/date.rb +11 -0
  48. data/lib/{extensions → rcap/extensions}/date_time.rb +2 -5
  49. data/lib/{extensions → rcap/extensions}/string.rb +1 -1
  50. data/lib/{extensions → rcap/extensions}/time.rb +2 -4
  51. data/lib/rcap/utilities.rb +11 -11
  52. data/lib/rcap/validations.rb +7 -2
  53. data/lib/rcap/version.rb +1 -1
  54. data/lib/rcap.rb +21 -4
  55. data/spec/alert_spec.rb +69 -37
  56. data/spec/cap_1_0/alert_spec.rb +46 -61
  57. data/spec/cap_1_0/area_spec.rb +77 -37
  58. data/spec/cap_1_0/circle_spec.rb +26 -6
  59. data/spec/cap_1_0/event_code_spec.rb +10 -3
  60. data/spec/cap_1_0/geocode_spec.rb +11 -4
  61. data/spec/cap_1_0/info_spec.rb +74 -77
  62. data/spec/cap_1_0/parameter_spec.rb +18 -5
  63. data/spec/cap_1_0/point_spec.rb +9 -2
  64. data/spec/cap_1_0/polygon_spec.rb +52 -9
  65. data/spec/cap_1_0/resource_spec.rb +28 -21
  66. data/spec/cap_1_1/alert_spec.rb +47 -60
  67. data/spec/cap_1_1/area_spec.rb +66 -43
  68. data/spec/cap_1_1/circle_spec.rb +29 -6
  69. data/spec/cap_1_1/event_code_spec.rb +11 -3
  70. data/spec/cap_1_1/geocode_spec.rb +11 -3
  71. data/spec/cap_1_1/info_spec.rb +262 -118
  72. data/spec/cap_1_1/parameter_spec.rb +12 -3
  73. data/spec/cap_1_1/point_spec.rb +8 -2
  74. data/spec/cap_1_1/polygon_spec.rb +57 -18
  75. data/spec/cap_1_1/resource_spec.rb +70 -20
  76. data/spec/cap_1_2/alert_spec.rb +97 -110
  77. data/spec/cap_1_2/area_spec.rb +59 -41
  78. data/spec/cap_1_2/circle_spec.rb +15 -8
  79. data/spec/cap_1_2/event_code_spec.rb +11 -3
  80. data/spec/cap_1_2/geocode_spec.rb +11 -3
  81. data/spec/cap_1_2/info_spec.rb +266 -119
  82. data/spec/cap_1_2/parameter_spec.rb +11 -3
  83. data/spec/cap_1_2/point_spec.rb +10 -3
  84. data/spec/cap_1_2/polygon_spec.rb +25 -10
  85. data/spec/cap_1_2/resource_spec.rb +33 -28
  86. data/spec/{utilities_spec.rb → extensions_spec.rb} +0 -0
  87. data/spec/validations_spec.rb +18 -2
  88. metadata +20 -46
@@ -1,86 +1,14 @@
1
1
  module RCAP
2
2
  module CAP_1_2
3
+
3
4
  # A Parameter object is valid if
4
5
  # * it has a name
5
6
  # * it has a value
6
- class Parameter
7
- include Validation
8
-
9
- validates_presence_of( :name, :value )
7
+ class Parameter < RCAP::Base::Parameter
10
8
 
11
9
  # @return [String]
12
- attr_accessor( :name )
13
- # @return [String]
14
- attr_accessor( :value )
15
-
16
- XML_ELEMENT_NAME = "parameter"
17
- NAME_ELEMENT_NAME = "valueName"
18
- VALUE_ELEMENT_NAME = "value"
19
-
20
- XPATH = "cap:#{ XML_ELEMENT_NAME }"
21
- NAME_XPATH = "cap:#{ NAME_ELEMENT_NAME }"
22
- VALUE_XPATH = "cap:#{ VALUE_ELEMENT_NAME }"
23
-
24
- # @param [Hash] attributes
25
- # @option attributes [Symbol] :name Parameter name
26
- # @option attributes [Symbol] :value Parameter value
27
- def initialize( attributes = {} )
28
- @name = attributes[ :name ]
29
- @value = attributes[ :value ]
30
- end
31
-
32
- # @return [REXML::Element]
33
- def to_xml_element
34
- xml_element = REXML::Element.new( self.class::XML_ELEMENT_NAME )
35
- xml_element.add_element( self.class::NAME_ELEMENT_NAME ).add_text( @name )
36
- xml_element.add_element( self.class::VALUE_ELEMENT_NAME ).add_text( @value )
37
- xml_element
38
- end
39
-
40
- # @return [String]
41
- def to_xml
42
- self.to_xml_element.to_s
43
- end
44
-
45
- # @return [String]
46
- def inspect
47
- "#{ @name }: #{ @value }"
48
- end
49
-
50
- # Returns a string representation of the parameter of the form
51
- # name: value
52
- #
53
- # @return [String]
54
- def to_s
55
- self.inspect
56
- end
57
-
58
- # @param [REXML::Element] parameter_xml_element
59
- # @return [Parameter]
60
- def self.from_xml_element( parameter_xml_element ) # :nodoc:
61
- self.new( :name => RCAP.xpath_text( parameter_xml_element, self::NAME_XPATH, Alert::XMLNS ),
62
- :value => RCAP.xpath_text( parameter_xml_element, self::VALUE_XPATH, Alert::XMLNS ))
63
- end
64
-
65
- # Two parameters are equivalent if they have the same name and value.
66
- #
67
- # @param [Parameter] other
68
- # @return [true, false]
69
- def ==( other )
70
- [ @name, @value ] == [ other.name, other.value ]
71
- end
72
-
73
- # @return [Hash]
74
- def to_h
75
- RCAP.attribute_values_to_hash(
76
- [ @name, @value ])
77
- end
78
-
79
- # @param [Hash] hash
80
- # @return [Parameter]
81
- def self.from_h( hash )
82
- key = hash.keys.first
83
- self.new( :name => key, :value => hash[ key ])
10
+ def xmlns
11
+ Alert::XMLNS
84
12
  end
85
13
  end
86
14
  end
@@ -1,69 +1,10 @@
1
1
  module RCAP
2
2
  module CAP_1_2
3
+
3
4
  # A Point object is valid if
4
5
  # * it has a lattitude within the minimum and maximum lattitude values
5
6
  # * it has a longitude within the minimum and maximum longitude values
6
- class Point
7
- include Validation
8
-
9
- MAX_LONGITUDE = 180
10
- MIN_LONGITUDE = -180
11
- MAX_LATTITUDE = 90
12
- MIN_LATTITUDE= -90
13
-
14
- # @return [Numeric]
15
- attr_accessor( :lattitude )
16
- # @return [Numeric]
17
- attr_accessor( :longitude )
18
-
19
- validates_numericality_of( :lattitude, :longitude )
20
- validates_inclusion_of( :lattitude, :in => MIN_LATTITUDE..MAX_LATTITUDE )
21
- validates_inclusion_of( :longitude, :in => MIN_LONGITUDE..MAX_LONGITUDE)
22
-
23
- # @param [Hash] attributes
24
- # @option attributes [Numeric] :lattitude
25
- # @option attributes [Numeric] :longitude
26
- def initialize( attributes = {} )
27
- @lattitude = attributes[ :lattitude ]
28
- @longitude = attributes[ :longitude ]
29
- end
30
-
31
- # Returns a string representation of the point of the form
32
- # lattitude,longitude
33
- #
34
- # @return [String]
35
- def to_s
36
- "#{ @lattitude },#{ @longitude }"
37
- end
38
-
39
- # @return [String]
40
- def inspect
41
- '('+self.to_s+')'
42
- end
43
-
44
- # Two points are equivalent if they have the same lattitude and longitude
45
- #
46
- # @param [Point] other
47
- # @return [true, false]
48
- def ==( other )
49
- [ @lattitude, @longitude ] == [ other.lattitude, other.longitude ]
50
- end
51
-
52
- LATTITUDE_KEY = 'lattitude'
53
- LONGITUDE_KEY = 'longitude'
54
-
55
- # @return [Hash]
56
- def to_h
57
- RCAP.attribute_values_to_hash(
58
- [ LATTITUDE_KEY, @lattitude ],
59
- [ LONGITUDE_KEY, @longitude ])
60
- end
61
-
62
- # @param [Hash] point_hash
63
- # @return [Point]
64
- def self.from_h( point_hash )
65
- self.new( :lattitude => point_hash[ LATTITUDE_KEY ], :longitude => point_hash[ LONGITUDE_KEY ])
66
- end
7
+ class Point < RCAP::Base::Point
67
8
  end
68
9
  end
69
10
  end
@@ -1,103 +1,16 @@
1
1
  module RCAP
2
2
  module CAP_1_2
3
+
3
4
  # A Polygon object is valid if
4
5
  # * it has a minimum of three points
5
6
  # * each Point object in the points collection is valid
6
- class Polygon
7
- include Validation
8
-
9
- # @return [Array<Point>] Collection of {Point} objects.
10
- attr_reader( :points )
7
+ class Polygon < RCAP::Base::Polygon
11
8
 
12
- validates_collection_of( :points )
13
9
  validates_length_of( :points, :minimum => 4 )
14
- validates_equality_of_first_and_last( :points )
15
-
16
- XML_ELEMENT_NAME = 'polygon'
17
- XPATH = "cap:#{ XML_ELEMENT_NAME }"
18
-
19
- # @param [Hash] attributes
20
- # @option attributes [Array<Point>] :points Collection of {Point} objects
21
- def initialize( attributes = {})
22
- @points = Array( attributes[ :points ])
23
- end
24
-
25
- # Creates a new Point object and adds it to the points array.
26
- #
27
- # @see Point#initialize
28
- def add_point( point_attributes = {})
29
- point = Point.new( point_attributes )
30
- @points << point
31
- point
32
- end
33
-
34
- # Returns a string representation of the polygon of the form
35
- # points[0] points[1] points[2] ...
36
- # where each point is formatted with Point#to_s
37
- def to_s
38
- @points.join( ' ' )
39
- end
40
-
41
- # @return [String]
42
- def inspect
43
- "(#{ @points.map{|point| point.inspect}.join(', ')})"
44
- end
45
-
46
- # @return [REXML::Element]
47
- def to_xml_element
48
- xml_element = REXML::Element.new( XML_ELEMENT_NAME )
49
- xml_element.add_text( self.to_s )
50
- xml_element
51
- end
52
-
53
- # @return [String]
54
- def to_xml
55
- self.to_xml_element.to_s
56
- end
57
-
58
- # Two polygons are equivalent if their collection of points is equivalent.
59
- #
60
- # @return [true,false]
61
- def ==( other )
62
- @points == other.points
63
- end
64
-
65
- # @return [Array<Array(Numeric,Numeric)>]
66
- def self.parse_polygon_string( polygon_string )
67
- polygon_string.split( ' ' ).map{ |coordinate_string| coordinate_string.split( ',' ).map{|coordinate| coordinate.to_f }}
68
- end
69
-
70
- # @return [Polygon]
71
- def self.from_xml_element( polygon_xml_element )
72
- if polygon_xml_element.text && !polygon_xml_element.text.empty?
73
- coordinates = self.parse_polygon_string( polygon_xml_element.text )
74
- points = coordinates.map{ |lattitude, longitude| Point.new( :lattitude => lattitude, :longitude => longitude )}
75
- polygon = self.new( :points => points )
76
- else
77
- self.new
78
- end
79
- end
80
-
81
- # @return [String]
82
- def to_yaml( options = {} )
83
- @points.map{ |point| [ point.lattitude, point.longitude ]}.to_yaml( options )
84
- end
85
-
86
- # @return [Polygon]
87
- def self.from_yaml_data( polygon_yaml_data )
88
- self.new( :points => Array( polygon_yaml_data ).map{ |lattitude, longitude| Point.new( :lattitude => lattitude, :longitude => longitude )})
89
- end
90
-
91
- POINTS_KEY = 'points'
92
-
93
- # @return [Hash]
94
- def to_h
95
- { POINTS_KEY => @points.map{ |point| point.to_h }}
96
- end
97
10
 
98
- # @return [Polygon]
99
- def self.from_h( polygon_hash )
100
- self.new( :points => polygon_hash[ POINTS_KEY ].map{ |point_hash| Point.from_h( point_hash )})
11
+ # @return [Class]
12
+ def point_class
13
+ Point
101
14
  end
102
15
  end
103
16
  end
@@ -4,59 +4,20 @@ module RCAP
4
4
  # A Resource object is valid if
5
5
  # * it has a resource description
6
6
  # * it has a mime_type
7
- class Resource
8
- include Validation
7
+ class Resource < RCAP::Base::Resource
9
8
 
10
- # @return [String] Resource description
11
- attr_accessor( :resource_desc )
12
- # @return [String] MIME type as described in RFC 2046
13
- attr_accessor( :mime_type )
14
- # @return [Integer] Expressed in bytes
15
- attr_accessor( :size )
16
- # @return [String] Resource location
17
- attr_accessor( :uri )
18
- # @return [String] Dereferenced URI - contents of URI Base64 encoded
9
+ # @return [String] Dereferenced URI - contents of URI Base64 encoded
19
10
  attr_accessor( :deref_uri )
20
- # @return [String] SHA-1 hash of contents of resource
21
- attr_accessor( :digest )
22
11
 
23
- validates_presence_of( :resource_desc )
24
12
  validates_presence_of( :mime_type )
25
13
 
26
- XML_ELEMENT_NAME = 'resource'
27
- MIME_TYPE_ELEMENT_NAME = 'mimeType'
28
- SIZE_ELEMENT_NAME = 'size'
29
- URI_ELEMENT_NAME = 'uri'
30
- DEREF_URI_ELEMENT_NAME = 'derefUri'
31
- DIGEST_ELEMENT_NAME = 'digest'
32
- RESOURCE_DESC_ELEMENT_NAME = 'resourceDesc'
14
+ DEREF_URI_ELEMENT_NAME = 'derefUri'
33
15
 
34
- XPATH = "cap:#{ XML_ELEMENT_NAME }"
35
- MIME_TYPE_XPATH = "cap:#{ MIME_TYPE_ELEMENT_NAME }"
36
- SIZE_XPATH = "cap:#{ SIZE_ELEMENT_NAME }"
37
- URI_XPATH = "cap:#{ URI_ELEMENT_NAME }"
38
- DEREF_URI_XPATH = "cap:#{ DEREF_URI_ELEMENT_NAME }"
39
- DIGEST_XPATH = "cap:#{ DIGEST_ELEMENT_NAME }"
40
- RESOURCE_DESC_XPATH = "cap:#{ RESOURCE_DESC_ELEMENT_NAME }"
41
-
42
- # @param [Hash{Symbol => Object}] attributes
43
- # @option attributes [String] :mime_type
44
- # @option attributes [Numeric] :size Size in bytes
45
- # @option attributes [String] :uri
46
- # @option attributes [String] :deref_uri
47
- # @option attributes [String] :digest
48
- # @option attributes [String] :resource_desc
49
- def initialize( attributes = {} )
50
- @mime_type = attributes[ :mime_type ]
51
- @size = attributes[ :size ]
52
- @uri = attributes[ :uri ]
53
- @deref_uri = attributes[ :deref_uri ]
54
- @digest = attributes[ :digest ]
55
- @resource_desc = attributes[ :resource_desc ]
56
- end
16
+ DEREF_URI_XPATH = "cap:#{ DEREF_URI_ELEMENT_NAME }"
57
17
 
58
18
  # @return [REXML::Element]
59
- def to_xml_element
19
+ def to_xml_element
20
+ xml_element = super
60
21
  xml_element = REXML::Element.new( XML_ELEMENT_NAME )
61
22
  xml_element.add_element( RESOURCE_DESC_ELEMENT_NAME ).add_text( @resource_desc )
62
23
  xml_element.add_element( MIME_TYPE_ELEMENT_NAME ).add_text( @mime_type ) if @mime_type
@@ -67,32 +28,6 @@ module RCAP
67
28
  xml_element
68
29
  end
69
30
 
70
- # If size is defined returns the size in kilobytes
71
- # @return [Float]
72
- def size_in_kb
73
- if @size
74
- @size.to_f/1024
75
- end
76
- end
77
-
78
- # @return [String]
79
- def to_xml
80
- self.to_xml_element.to_s
81
- end
82
-
83
- # @return [String]
84
- def inspect
85
- [ @resource_desc, @uri, @mime_type, @size ? format( "%.1fKB", @size_in_kb ) : nil ].compact.join(' - ')
86
- end
87
-
88
- # Returns a string representation of the resource of the form
89
- # resource_desc
90
- #
91
- # @return [String]
92
- def to_s
93
- @resource_desc
94
- end
95
-
96
31
  # Retrieves the content at uri and stores it in deref_uri as Base64 encoded text. It will also
97
32
  # calculate the {#digest} on the encoded data using SHA1 and set the {#size}.
98
33
  #
@@ -109,47 +44,24 @@ module RCAP
109
44
  self.calculate_hash_and_size
110
45
  end
111
46
 
112
- # Calculates the SHA-1 hash and size of the contents of {#deref_uri}.
113
- # Returns an array containing the size (in bytes) and SHA-1 hash if
114
- # {#deref_uri} is present otherwise returns nil.
115
- #
116
- # @return [nil,Array(Integer,String)]
117
- def calculate_hash_and_size
118
- if @deref_uri
119
- @digest = Digest::SHA1.hexdigest( @deref_uri )
120
- @size = @deref_uri.bytesize
121
- [ @size, @digest ]
122
- end
123
- end
124
-
125
- # The decoded contents of {#deref_uri} if present otherwise nil.
126
- #
127
- # @return [nil,String]
128
- def decoded_deref_uri
129
- Base64.decode64( @deref_uri ) if @deref_uri
47
+ # @return [String]
48
+ def xmlns
49
+ Alert::XMLNS
130
50
  end
131
51
 
132
52
  # @param [REXML::Element] resource_xml_element
133
53
  # @return [Resource]
134
- def self.from_xml_element( resource_xml_element )
135
- resource = self.new( :resource_desc => RCAP.xpath_text( resource_xml_element, RESOURCE_DESC_XPATH, Alert::XMLNS ),
136
- :uri => RCAP.xpath_text( resource_xml_element, URI_XPATH, Alert::XMLNS ),
137
- :mime_type => RCAP.xpath_text( resource_xml_element, MIME_TYPE_XPATH, Alert::XMLNS ),
138
- :deref_uri => RCAP.xpath_text( resource_xml_element, DEREF_URI_XPATH, Alert::XMLNS ),
139
- :size => RCAP.xpath_text( resource_xml_element, SIZE_XPATH, Alert::XMLNS ).to_i,
140
- :digest => RCAP.xpath_text( resource_xml_element, DIGEST_XPATH, Alert::XMLNS ))
54
+ def self.from_xml_element( resource_xml_element )
55
+ super.tap do |resource|
56
+ resource.deref_uri = RCAP.xpath_text( resource_xml_element, DEREF_URI_XPATH, resource.xmlns )
57
+ end
141
58
  end
142
59
 
143
- RESOURCE_DESC_YAML = "Resource Description"
144
- URI_YAML = "URI"
145
- MIME_TYPE_YAML = "Mime Type"
146
- DEREF_URI_YAML = "Derefrenced URI Data"
147
- SIZE_YAML = "Size"
148
- DIGEST_YAML = "Digest"
60
+ DEREF_URI_YAML = "Derefrenced URI Data"
149
61
 
150
62
  # @param [Hash] options
151
63
  # @return [String]
152
- def to_yaml( options = {} )
64
+ def to_yaml( options = {} )
153
65
  RCAP.attribute_values_to_hash(
154
66
  [ RESOURCE_DESC_YAML, @resource_desc ],
155
67
  [ URI_YAML, @uri ],
@@ -161,45 +73,30 @@ module RCAP
161
73
 
162
74
  # @param [Hash] resource_yaml_data
163
75
  # @return [Resource]
164
- def self.from_yaml_data( resource_yaml_data )
165
- self.new(
166
- :resource_desc => reource_yaml_data[ RESOURCE_DESC_YAML ],
167
- :uri => reource_yaml_data[ URI_YAML ],
168
- :mime_type => reource_yaml_data[ MIME_TYPE_YAML ],
169
- :deref_uri => reource_yaml_data[ DEREF_URI_YAML ],
170
- :size => reource_yaml_data[ SIZE_YAML ],
171
- :digest => reource_yaml_data[ DIGEST_YAML ]
172
- )
76
+ def self.from_yaml_data( resource_yaml_data )
77
+ super.tap do |resource|
78
+ resource.deref_uri = resource_yaml_data[ DEREF_URI_YAML ]
79
+ end
173
80
  end
174
81
 
175
- RESOURCE_DESC_KEY = 'resource_desc'
176
- URI_KEY = 'uri'
177
- MIME_TYPE_KEY = 'mime_type'
178
- DEREF_URI_KEY = 'deref_uri'
179
- SIZE_KEY = 'size'
180
- DIGEST_KEY = 'digest'
82
+ DEREF_URI_KEY = 'deref_uri'
181
83
 
182
84
  # @return [Hash]
183
- def to_h
184
- RCAP.attribute_values_to_hash(
185
- [ RESOURCE_DESC_KEY, @resource_desc ],
186
- [ URI_KEY, @uri],
187
- [ MIME_TYPE_KEY, @mime_type],
188
- [ DEREF_URI_KEY, @deref_uri],
189
- [ SIZE_KEY, @size ],
190
- [ DIGEST_KEY, @digest ])
85
+ def to_h
86
+ RCAP.attribute_values_to_hash( [ RESOURCE_DESC_KEY, @resource_desc ],
87
+ [ URI_KEY, @uri],
88
+ [ MIME_TYPE_KEY, @mime_type],
89
+ [ DEREF_URI_KEY, @deref_uri],
90
+ [ SIZE_KEY, @size ],
91
+ [ DIGEST_KEY, @digest ])
191
92
  end
192
93
 
193
94
  # @param [Hash] resource_hash
194
95
  # @return [Resource]
195
- def self.from_h( resource_hash )
196
- self.new(
197
- :resource_desc => resource_hash[ RESOURCE_DESC_KEY ],
198
- :uri => resource_hash[ URI_KEY ],
199
- :mime_type => resource_hash[ MIME_TYPE_KEY ],
200
- :deref_uri => resource_hash[ DEREF_URI_KEY ],
201
- :size => resource_hash[ SIZE_KEY ],
202
- :digest => resource_hash[ DIGEST_KEY ])
96
+ def self.from_h( resource_hash )
97
+ super.tap do |resource|
98
+ resource.deref_uri = resource_hash[ DEREF_URI_KEY ]
99
+ end
203
100
  end
204
101
  end
205
102
  end
data/lib/rcap/config.rb CHANGED
@@ -1,4 +1,7 @@
1
1
  module RCAP
2
2
  XML_PRETTY_PRINTER = REXML::Formatters::Pretty.new( 2 )
3
3
  XML_PRETTY_PRINTER.compact = true
4
+
5
+ RCAP_TIME_FORMAT = "%Y-%m-%dT%H:%M:%S"
6
+ RCAP_ZONE_FORMAT = "%+03i:00"
4
7
  end
@@ -1,4 +1,4 @@
1
- class Array
1
+ class Array
2
2
  # Formats an array into a string suitable for a CAP message.
3
3
  #
4
4
  # @return [String]
@@ -0,0 +1,11 @@
1
+ class Date
2
+ alias inspect to_s
3
+
4
+ # Returns a string representaion of the time suitable for CAP.
5
+ # @return [String]
6
+ # @example
7
+ # Date.today.to_s_for_cap # => "2011-10-26T00:00:00+00:00"
8
+ def to_s_for_cap
9
+ self.to_datetime.to_s_for_cap
10
+ end
11
+ end
@@ -1,15 +1,12 @@
1
1
  class DateTime
2
- RCAP_TIME_FORMAT = "%Y-%m-%dT%H:%M:%S"
3
- RCAP_ZONE_FORMAT = "%+03i:00"
4
-
5
2
  alias inspect to_s
6
3
 
7
4
  # Returns a string representaion of the time suitable for CAP.
8
5
  # @return [String]
9
- # @example
6
+ # @example
10
7
  # DateTime.now.to_s_for_cap # => "2011-10-26T21:45:00+02:00"
11
8
  def to_s_for_cap
12
- t = self.strftime( RCAP_TIME_FORMAT ) + format( RCAP_ZONE_FORMAT , utc_hours_offset )
9
+ t = self.strftime( RCAP::RCAP_TIME_FORMAT ) + format( RCAP::RCAP_ZONE_FORMAT , utc_hours_offset )
13
10
  t.sub(/\+(00:\d\d)$/, '-\1')
14
11
  end
15
12
 
@@ -2,7 +2,7 @@ class String
2
2
  CAP_LIST_REGEX = Regexp.new( '"([\w\s]+)"|(\S+)' )
3
3
  WHITESPACE_REGEX = Regexp.new('^\s+$')
4
4
 
5
- # Reformats string for a CAP list. If the string contains whitespace it will
5
+ # Reformats string for a CAP list. If the string contains whitespace it will
6
6
  # enclose the contents in quotation marks.
7
7
  #
8
8
  # @return [String]
@@ -1,13 +1,11 @@
1
1
  class Time
2
- RCAP_TIME_FORMAT = "%Y-%m-%dT%H:%M:%S"
3
- RCAP_ZONE_FORMAT = "%+03i:00"
4
2
 
5
3
  # Returns a string representaion of the time suitable for CAP.
6
4
  # @return [String]
7
- # @example
5
+ # @example
8
6
  # Time.now.to_s_for_cap # => "2011-10-26T21:45:00+02:00"
9
7
  def to_s_for_cap
10
- t = self.strftime( RCAP_TIME_FORMAT ) + format( RCAP_ZONE_FORMAT , utc_hours_offset )
8
+ t = self.strftime( RCAP::RCAP_TIME_FORMAT ) + format( RCAP::RCAP_ZONE_FORMAT , utc_hours_offset )
11
9
  t.sub(/\+(00:\d\d)$/, '-\1')
12
10
  end
13
11
 
@@ -1,6 +1,6 @@
1
- ALLOWED_CHARACTERS = /[^\s&<]+/
1
+ ALLOWED_CHARACTERS = /[^\s&<]+/
2
2
 
3
- module RCAP
3
+ module RCAP
4
4
  # Returns a randomly generated UUID string
5
5
  #
6
6
  # @return [String] UUID string
@@ -13,8 +13,8 @@ module RCAP
13
13
  #
14
14
  # @param [REXML::Element] xml_element Element to start matching from.
15
15
  # @param [String] xpath XPath expression
16
- # @param [String] namepsace Namespace in which to do the matching
17
- # @return [String,nil] Text content of element matching XPath query or nil
16
+ # @param [String] namespace Namespace in which to do the matching
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
20
  element.text if element
@@ -24,7 +24,7 @@ module RCAP
24
24
  #
25
25
  # @param [REXML::Element] xml_element Element to start matching from.
26
26
  # @param [String] xpath XPath expression
27
- # @param [String] namepsace Namespace in which to do the matching
27
+ # @param [String] namespace Namespace in which to do the matching
28
28
  # @return [REXML::Element,nil] Element matching XPath query or nil
29
29
  def self.xpath_first( xml_element, xpath, namespace )
30
30
  REXML::XPath.first( xml_element, xpath, { 'cap' => namespace })
@@ -34,8 +34,8 @@ module RCAP
34
34
  #
35
35
  # @param [REXML::Element] xml_element Element to start matching from.
36
36
  # @param [String] xpath XPath expression
37
- # @param [String] namepsace Namespace in which to do the matching
38
- # @return [Array<REXML::Element>] Collection of elements matching XPath query
37
+ # @param [String] namespace Namespace in which to do the matching
38
+ # @return [Array<REXML::Element>] Collection of elements matching XPath query
39
39
  def self.xpath_match( xml_element, xpath, namespace )
40
40
  REXML::XPath.match( xml_element, xpath, { 'cap' => namespace })
41
41
  end
@@ -47,7 +47,7 @@ module RCAP
47
47
  # @return [String] Formatted output for inspect
48
48
  #
49
49
  # @example
50
- # RCAP.format_lines_for_inspect( 'Test', 'one\ntwo\nthree' )
50
+ # RCAP.format_lines_for_inspect( 'Test', 'one\ntwo\nthree' )
51
51
  # # returns
52
52
  # # .-------.
53
53
  # # | Test |
@@ -67,7 +67,7 @@ module RCAP
67
67
 
68
68
  # Converts an array of key value pairs into a hash, excluding any value that is nil or empty
69
69
  #
70
- # @param [Array<Array(Object,Object)>] attribte_values An array of arrays of key/value pairs
70
+ # @param [Array<Array(Object,Object)>] attribute_values An array of arrays of key/value pairs
71
71
  # @return [Hash] Hash of attributes
72
72
  #
73
73
  # @example
@@ -78,7 +78,7 @@ module RCAP
78
78
 
79
79
  # Calls #to_s_for_cap on the object it it responds to that otherwise just calls #to_s
80
80
  #
81
- # @param [#to_s, #to_s_for_cap]
81
+ # @param [#to_s, #to_s_for_cap] object
82
82
  # @return [String]
83
83
  def self.to_s_for_cap( object )
84
84
  if object
@@ -92,7 +92,7 @@ module RCAP
92
92
 
93
93
  # If the parameter is a string 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 String to parse
96
96
  # @return [String,nil]
97
97
  def self.parse_datetime( date_string )
98
98
  if date_string.is_a?( String )
@@ -1,9 +1,11 @@
1
- module Validation
2
- module ClassMethods
1
+ module Validation
2
+ module ClassMethods
3
3
 
4
4
  CAP_NUMBER_REGEX = Regexp.new( '^-{0,1}\d*\.{0,1}\d+$' )
5
5
  CAP_INTEGER_REGEX = Regexp.new( '\-{0,1}A[+-]?\d+\Z' )
6
6
 
7
+ # @example
8
+ # validates_inclusion_of( :status, :in => VALID_STATUSES )
7
9
  def validates_inclusion_of( *attributes )
8
10
  options = {
9
11
  :message => 'is not in the required range'
@@ -17,6 +19,9 @@ module Validation
17
19
  end
18
20
  end
19
21
 
22
+ # Will validate all members of a collection are found in a given collection.
23
+ # @example
24
+ # validates_inclusion_of_members_of( :categories, :in => VALID_CATEGORIES )
20
25
  def validates_inclusion_of_members_of( *attributes )
21
26
  options = {
22
27
  :message => 'contains members that are not valid'
data/lib/rcap/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module RCAP
2
- VERSION = '1.3.1'
2
+ VERSION = '2.0.0'
3
3
  end