rcap 1.3.0 → 1.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/{CHANGELOG.rdoc → CHANGELOG.md} +26 -20
- data/README.md +259 -0
- data/Rakefile +8 -7
- data/lib/extensions/array.rb +7 -1
- data/lib/extensions/date_time.rb +5 -1
- data/lib/extensions/string.rb +14 -1
- data/lib/extensions/time.rb +5 -1
- data/lib/rcap.rb +1 -1
- data/lib/rcap/alert.rb +24 -10
- data/lib/rcap/cap_1_0/alert.rb +232 -166
- data/lib/rcap/cap_1_0/area.rb +100 -67
- data/lib/rcap/cap_1_0/circle.rb +47 -22
- data/lib/rcap/cap_1_0/event_code.rb +3 -2
- data/lib/rcap/cap_1_0/geocode.rb +3 -2
- data/lib/rcap/cap_1_0/info.rb +265 -202
- data/lib/rcap/cap_1_0/parameter.rb +43 -20
- data/lib/rcap/cap_1_0/point.rb +23 -9
- data/lib/rcap/cap_1_0/polygon.rb +37 -19
- data/lib/rcap/cap_1_0/resource.rb +77 -55
- data/lib/rcap/cap_1_1/alert.rb +222 -156
- data/lib/rcap/cap_1_1/area.rb +100 -67
- data/lib/rcap/cap_1_1/circle.rb +49 -22
- data/lib/rcap/cap_1_1/event_code.rb +3 -2
- data/lib/rcap/cap_1_1/geocode.rb +3 -2
- data/lib/rcap/cap_1_1/info.rb +281 -217
- data/lib/rcap/cap_1_1/parameter.rb +35 -16
- data/lib/rcap/cap_1_1/point.rb +23 -9
- data/lib/rcap/cap_1_1/polygon.rb +38 -20
- data/lib/rcap/cap_1_1/resource.rb +106 -65
- data/lib/rcap/cap_1_2/alert.rb +224 -158
- data/lib/rcap/cap_1_2/area.rb +100 -67
- data/lib/rcap/cap_1_2/circle.rb +49 -24
- data/lib/rcap/cap_1_2/event_code.rb +3 -2
- data/lib/rcap/cap_1_2/geocode.rb +3 -2
- data/lib/rcap/cap_1_2/info.rb +285 -219
- data/lib/rcap/cap_1_2/parameter.rb +39 -19
- data/lib/rcap/cap_1_2/point.rb +23 -9
- data/lib/rcap/cap_1_2/polygon.rb +37 -20
- data/lib/rcap/cap_1_2/resource.rb +107 -67
- data/lib/rcap/config.rb +4 -0
- data/lib/rcap/utilities.rb +55 -2
- data/lib/rcap/validations.rb +2 -2
- data/lib/rcap/version.rb +1 -1
- data/rcap.gemspec +2 -2
- data/spec/cap_1_0/parameter_spec.rb +5 -1
- data/spec/cap_1_1/resource_spec.rb +6 -0
- data/spec/cap_1_2/alert_spec.rb +8 -0
- data/spec/cap_1_2/resource_spec.rb +8 -2
- metadata +11 -10
- data/README.rdoc +0 -247
- data/lib/config.rb +0 -2
@@ -1,50 +1,56 @@
|
|
1
|
-
|
1
|
+
Change Log
|
2
|
+
==========
|
2
3
|
|
3
|
-
|
4
|
+
## 1.3.1 - 25 February 2012
|
5
|
+
|
6
|
+
* Moved XML_PRETTY_PRINTER into RCAP namespace to avoid namespace issues
|
7
|
+
* Added Resource#decoded_deref_uri
|
8
|
+
* RCAP::Alert.from_xml can load from a String or IO object
|
9
|
+
|
10
|
+
## 1.3.0 - 15 December 2011
|
4
11
|
|
5
12
|
* Removed Resource#deref_uri= in 1.1 and 1.2 API.
|
6
|
-
* Added Resource#
|
7
|
-
* Hash and size of
|
13
|
+
* Added Resource#calculate_hash_and_size to calculate size and SHA1 hash of deref_uri if present
|
14
|
+
* Hash and size of dereferenced URI are also calculated on an explicit call to Resource#dereference_uri!
|
8
15
|
|
9
|
-
|
16
|
+
## 1.2.4 - 4 December 2011
|
10
17
|
|
11
18
|
* Fixed parsing bug when creating Parameter and subclasses from XML
|
12
19
|
|
13
|
-
|
20
|
+
## 1.2.3 - 10 November 2011
|
14
21
|
|
15
22
|
* Fixed Time#utc_hours_offset to use localtime to preserve TZ info (Earle Clubb)
|
16
23
|
|
17
|
-
|
24
|
+
## 1.2.2 - 29 October 2011
|
18
25
|
|
19
|
-
* Fixed polygon parsing and made it
|
26
|
+
* Fixed polygon parsing and made it consistent across versions
|
20
27
|
|
21
|
-
|
28
|
+
## 1.2.1 - 27 October 2011
|
22
29
|
|
23
30
|
* Fixed bug initialising 'audience' attribute in Info class
|
24
31
|
* Added CAP/Atom files from Google CAP Library project into specs
|
25
32
|
* Organised extension code into separate file
|
26
|
-
* Modifed RCAP::Alert.from_xml to auto-detect the XML namespace and CAP version. Will default to CAP 1.2 if no
|
27
|
-
namespace is found.
|
33
|
+
* Modifed RCAP::Alert.from_xml to auto-detect the XML namespace and CAP version. Will default to CAP 1.2 if no namespace is found.
|
28
34
|
|
29
|
-
|
35
|
+
## 1.2.0 - 17 July 2011
|
30
36
|
|
31
37
|
* CAP 1.1 and CAP 1.2 - Resource#dereference_uri! fetches data from Resource#uri and sets Resource#deref_uri
|
32
38
|
* Resource#deref_uri= will also automatically set size and calculate SHA1 digest
|
33
39
|
* Resources correctly deal with size on import and export
|
34
40
|
|
35
|
-
|
41
|
+
## 1.1.1 - 25 June 2011
|
36
42
|
|
37
43
|
* Documentation and code cleanup
|
38
44
|
|
39
|
-
|
45
|
+
## 1.1.0 - 20 June 2011
|
40
46
|
|
41
47
|
* Added CAP 1.0 Support
|
42
48
|
|
43
|
-
|
49
|
+
## 1.0.1 - 22 April 2011
|
44
50
|
|
45
51
|
* Added RCAP::Alert.from_h
|
46
52
|
|
47
|
-
|
53
|
+
## 1.0.0 - 6 April 2011
|
48
54
|
|
49
55
|
* Added CAP 1.2 Support
|
50
56
|
* Added namespaces (RCAP::CAP_1_1 and RCAP::CAP_1_2) to seperate CAP 1.1 and CAP 1.2 classes
|
@@ -53,22 +59,22 @@ namespace is found.
|
|
53
59
|
* Added factory methods to RCAP::Alert module to parse in files and return the correct RCAP objects
|
54
60
|
* Pretty print XML and JSON output
|
55
61
|
|
56
|
-
|
62
|
+
## 0.4 - 6th March 2011
|
57
63
|
|
58
64
|
* Implemented Hash generation and parsing
|
59
65
|
* Implemented JSON generation and parsing
|
60
66
|
* Circle is now a subclass of Point
|
61
67
|
|
62
|
-
|
68
|
+
## 0.3 - 26th November 2009
|
63
69
|
|
64
70
|
* Bugfix release
|
65
71
|
|
66
|
-
|
72
|
+
## 0.2 - 20th November 2009
|
67
73
|
|
68
74
|
* Implemented to_s/inspect methods for all classes
|
69
75
|
* Implemented YAML generation and parsing
|
70
76
|
* Documentation improvements
|
71
77
|
|
72
|
-
|
78
|
+
## 0.1 - 5th November 2009
|
73
79
|
|
74
80
|
* Initial release
|
data/README.md
ADDED
@@ -0,0 +1,259 @@
|
|
1
|
+
RCAP - Common Alerting Protocol for Ruby
|
2
|
+
========================================
|
3
|
+
|
4
|
+
Overview
|
5
|
+
--------
|
6
|
+
|
7
|
+
The Common Alerting Protocol is a lightweight standard to facilitate the distribution of alerting data. RCAP is an implementation of the CAP document protocol in Ruby. It allows for the creation of RCAP messages from Ruby applications and the parsing of external messages.
|
8
|
+
|
9
|
+
RCAP currently supports CAP 1.0, 1.1 and 1.2.
|
10
|
+
|
11
|
+
Version
|
12
|
+
-------
|
13
|
+
|
14
|
+
1.3.0
|
15
|
+
|
16
|
+
Dependencies
|
17
|
+
------------
|
18
|
+
|
19
|
+
RCAP depends on the following gems
|
20
|
+
|
21
|
+
* [Assistance](http://assistance.rubyforge.org)
|
22
|
+
* [UUIDTools](http://uuidtools.rubyforge.org)
|
23
|
+
* [JSON](http://json.rubyforge.org)
|
24
|
+
|
25
|
+
RCAP uses the REXML API, included in Ruby, to parse and generate XML.
|
26
|
+
|
27
|
+
Installation
|
28
|
+
------------
|
29
|
+
|
30
|
+
RCAP is distributed as a Ruby gem and is available from [Rubygems.org](http://rubygems.org). From the command line you can install it with the gem command:
|
31
|
+
|
32
|
+
$ gem install rcap
|
33
|
+
|
34
|
+
The gem is also available for download and manual installation at [www.aimred.com/gems](http://www.aimred.com/gems).
|
35
|
+
|
36
|
+
Web resources
|
37
|
+
-------------
|
38
|
+
|
39
|
+
* The RCAP project page can be found at [http://www.aimred.com/projects/rcap](http://www.aimred.com/projects/rcap)
|
40
|
+
* The RCAP API docs can be found at [http://www.aimred.com/projects/rcap/api](http://www.aimred.com/projects/rcap/api)
|
41
|
+
* A public git repository can be found at [git://github.com/farrel/RCAP.git](git://github.com/farrel/RCAP.git)
|
42
|
+
|
43
|
+
Usage
|
44
|
+
-----
|
45
|
+
|
46
|
+
To include RCAP into your application add the following require
|
47
|
+
|
48
|
+
require 'rcap'
|
49
|
+
|
50
|
+
All RCAP classes reside in the RCAP namespace but including the RCAP module makes the classes available at the top level without the RCAP prefix.
|
51
|
+
|
52
|
+
include RCAP:CAP_1_2 # Include RCAP:CAP_1_2 module into namespace
|
53
|
+
alert = Alert.new
|
54
|
+
|
55
|
+
### Creating an Alert
|
56
|
+
|
57
|
+
alert = Alert.new( sender: 'cape_town_disaster_relief@capetown.municipal.za',
|
58
|
+
status: Alert::STATUS_ACTUAL,
|
59
|
+
msg_type: Alert::MSG_TYPE_ALERT,
|
60
|
+
scope: Alert::SCOPE_PUBLIC )
|
61
|
+
|
62
|
+
alert.add_info( event: 'Liquid Petroleoum Tanker Fire',
|
63
|
+
language: 'en-ZA',
|
64
|
+
categories: [ Info::CATEGORY_TRANSPORT, Info::CATEGORY_FIRE ],
|
65
|
+
urgency: Info::URGENCY_IMMEDIATE,
|
66
|
+
severity: Info::SEVERITY_SEVERE,
|
67
|
+
certainty: Info::CERTAINTY_OBSERVED,
|
68
|
+
headline: 'LIQUID PETROLEOUM TANKER FIRE ON N2 INCOMING FREEWAY',
|
69
|
+
description: 'A liquid petroleoum tanker has caught fire on the N2 incoming freeway 1km
|
70
|
+
after the R300 interchange. Municipal fire fighting crews have been dispatched.
|
71
|
+
Traffic control officers are on the scene and have diverted traffic onto
|
72
|
+
alternate routes.' )
|
73
|
+
|
74
|
+
alert.infos.first.add_area( area_desc: 'N2 Highway/R300 Interchange' ).add_geocode( name: 'Intersection', value: 'N2-15' )
|
75
|
+
|
76
|
+
# Accessing attributes
|
77
|
+
alert.status # "Actual"
|
78
|
+
alert.infos[0].language # "en-ZA"
|
79
|
+
alert.infos[0].categories.join( ', ' ) # "Transport, Fire"
|
80
|
+
alert.infos[0].areas[0] # "N2 Highway/R300 Interchange"
|
81
|
+
|
82
|
+
### Parsing an Alert From An External Source
|
83
|
+
|
84
|
+
RCAP can parse a CAP alert from a varierty of file formats. Besides the [standard XML](http://www.oasis-emergency.org/cap) representation, [YAML](http://yaml.org) and [JSON](http://json.org) support is also included.
|
85
|
+
|
86
|
+
To ensure the correct RCAP Alert object (RCAP::CAP_1_1::Alert or RCAP::CAP_1_2::Alert) is returned from an external source, a number of factories are defined in the RCAP::Alert module. If the version of the document to be parsed can not be ascertained a CAP 1.2 document will be assumed.
|
87
|
+
|
88
|
+
#### From XML
|
89
|
+
|
90
|
+
RCAP allows for the parsing of a CAP XML string
|
91
|
+
|
92
|
+
alert = RCAP::Alert.from_xml( xml_string )
|
93
|
+
|
94
|
+
#### From YAML
|
95
|
+
|
96
|
+
Alert messgaes can be read in from text files containing data formatted in [YAML](http://yaml.org) as generated by Alert#to_yaml.
|
97
|
+
|
98
|
+
alert = RCAP::Alert.from_yaml( yaml_string )
|
99
|
+
|
100
|
+
#### From JSON
|
101
|
+
|
102
|
+
An Alert can also be initialised from a [JSON](http://json.org) string produced by Alert#to_json
|
103
|
+
|
104
|
+
alert = RCAP::Alert.from_json( json_string )
|
105
|
+
|
106
|
+
### Validating an alert
|
107
|
+
|
108
|
+
The RCAP API aims to codify as many of the rules of the CAP XML format into validation rules that can be checked using the Assistance API. The following Info object has two attributes ('severity' and 'certainty') set to incorrect values.
|
109
|
+
|
110
|
+
info = Info.new( event: 'Liquid Petroleoum Tanker Fire',
|
111
|
+
language: 'en-ZA',
|
112
|
+
categories: [ Info::CATEGORY_TRANSPORT, Info::CATEGORY_FIRE ],
|
113
|
+
urgency: Info::URGENCY_IMMEDIATE,
|
114
|
+
severity: nil, # Severity is not assigned
|
115
|
+
certainty: 'Incorrect Certainty' ) # Certainty is assigned an incorrect value
|
116
|
+
|
117
|
+
puts "Is info valid: #{ info.valid? }"
|
118
|
+
info.errors.full_messages.each{ |message| puts "Error: #{ message }" }
|
119
|
+
|
120
|
+
Will produce the following output:
|
121
|
+
|
122
|
+
Is info valid: false
|
123
|
+
Error: severity is not present
|
124
|
+
Error: certainty can only be assigned the following values: Observed, Likely, Possible, Unlikely, Unknown
|
125
|
+
|
126
|
+
All RCAP classes include the Validation module.
|
127
|
+
|
128
|
+
A full spec suite using [RSpec](http://www.rspec.info) was used to test the validations and currently numbers over 1000 tests.
|
129
|
+
|
130
|
+
### Exporting an Alert
|
131
|
+
|
132
|
+
#### To XML
|
133
|
+
|
134
|
+
Using the alert message created above
|
135
|
+
|
136
|
+
puts alert.to_xml # Print out CAP XML message
|
137
|
+
|
138
|
+
Will print the following CAP XML
|
139
|
+
|
140
|
+
<?xml version='1.0'?>
|
141
|
+
<alert xmlns='urn:oasis:names:tc:emergency:cap:1.2'>
|
142
|
+
<identifier>494207a7-f86b-4060-8318-a4b2a3ce565e</identifier>
|
143
|
+
<sender>cape_town_disaster_relief@capetown.municipal.za</sender>
|
144
|
+
<sent>2009-10-26T21:04:51+02:00</sent>
|
145
|
+
<status>Actual</status>
|
146
|
+
<msgType>Alert</msgType>
|
147
|
+
<scope>Public</scope>
|
148
|
+
<info>
|
149
|
+
<language>en-ZA</language>
|
150
|
+
<category>Transport</category>
|
151
|
+
<category>Fire</category>
|
152
|
+
<event>Liquid Petroleoum Tanker Fire</event>
|
153
|
+
<urgency>Immediate</urgency>
|
154
|
+
<severity>Severe</severity>
|
155
|
+
<certainty>Observed</certainty>
|
156
|
+
<headline>LIQUID PETROLEOUM TANKER FIRE ON N2 INCOMING FREEWAY</headline>
|
157
|
+
<description>
|
158
|
+
A liquid petroleoum tanker has caught fire on the N2 incoming freeway 1km
|
159
|
+
after the R300 interchange. Municipal fire fighting crews have been
|
160
|
+
dispatched. Traffic control officers are on the scene and have diverted
|
161
|
+
traffic onto alternate routes.
|
162
|
+
</description>
|
163
|
+
</info>
|
164
|
+
</alert>
|
165
|
+
|
166
|
+
#### To YAML
|
167
|
+
|
168
|
+
YAML is a plain text serialization format designed to be easily readable and editable by both human and machine. RCAP has custom YAML generation and parsing methods to produce a YAML document that is as human friendly as possible. The following code
|
169
|
+
|
170
|
+
alert.to_yaml
|
171
|
+
|
172
|
+
will produce the following YAML document
|
173
|
+
|
174
|
+
---
|
175
|
+
CAP Version: 1.2
|
176
|
+
Identifier: 2a1ba96d-16e4-4f52-85ea-0258c1440bd5
|
177
|
+
Sender: cape_town_disaster_relief@capetown.municipal.za
|
178
|
+
Sent: 2009-11-19T02:41:29+02:00
|
179
|
+
Status: Actual
|
180
|
+
Message Type: Alert
|
181
|
+
Scope: Public
|
182
|
+
Information:
|
183
|
+
- Language: en-ZA
|
184
|
+
Categories: [Transport, Fire]
|
185
|
+
Event: Liquid Petroleoum Tanker Fire
|
186
|
+
Urgency: Immediate
|
187
|
+
Severity: Severe
|
188
|
+
Certainty: Observed
|
189
|
+
Headline: LIQUID PETROLEOUM TANKER FIRE ON N2 INCOMING FREEWAY
|
190
|
+
Description: |-
|
191
|
+
A liquid petroleoum tanker has caught fire on the N2 incoming freeway 1km
|
192
|
+
after the R300 interchange. Municipal fire fighting crews have been dispatched.
|
193
|
+
Traffic control officers are on the scene and have diverted traffic onto
|
194
|
+
alternate routes.
|
195
|
+
|
196
|
+
Note: If you use Ruby 1.8 the order of the attributes is jumbled due to hashes being unorderd (Ruby 1.9 implements ordered hashes). This does not affect the ability to parse documents generated from RCAP::Alert#to_yaml, it just makes things the output slightly messy.
|
197
|
+
|
198
|
+
#### To JSON
|
199
|
+
|
200
|
+
JSON is a text serialization format that can be easily loaded in a JavaScript environment.
|
201
|
+
|
202
|
+
alert.to_json
|
203
|
+
|
204
|
+
will produce the following JSON string
|
205
|
+
|
206
|
+
{"cap_version":"1.2",
|
207
|
+
"identifier":"0eb97e40-195b-437b-9a01-55fe89691def",
|
208
|
+
"sender":"cape_town_disaster_relief@capetown.municipal.za",
|
209
|
+
"sent":"2011-03-04T15:58:01+02:00",
|
210
|
+
"status":"Actual",
|
211
|
+
"msg_type":"Alert",
|
212
|
+
"scope":"Public",
|
213
|
+
"infos":[
|
214
|
+
{"language":"en-ZA",
|
215
|
+
"categories":["Transport","Fire"],
|
216
|
+
"event":"Liquid Petroleoum Tanker Fire",
|
217
|
+
"urgency":"Immediate",
|
218
|
+
"severity":"Severe",
|
219
|
+
"certainty":"Observed",
|
220
|
+
"headline":"LIQUID PETROLEOUM TANKER FIRE ON N2 INCOMING FREEWAY",
|
221
|
+
"description":"A liquid petroleoum tanker has caught fire on the N2 incoming freeway 1km
|
222
|
+
after the R300 interchange. Municipal fire fighting crews have been dispatched.
|
223
|
+
Traffic control officers are on the scene and have diverted traffic onto \nalternate routes."}]}
|
224
|
+
|
225
|
+
|
226
|
+
### DateTime and Time
|
227
|
+
|
228
|
+
It is highly recommended that when dealing with date and time fields (onset, expires etc) that the DateTime class is used to ensure the correct formatting of dates. The Time class can be used when generating a CAP alert XML message however any CAP alert that is parsed from an external XML source will use DateTime by default.
|
229
|
+
|
230
|
+
Authors
|
231
|
+
-------
|
232
|
+
|
233
|
+
* Farrel Lifson - farrel.lifson@aimred.com - http://www.aimred.com
|
234
|
+
|
235
|
+
### Contributors
|
236
|
+
|
237
|
+
* Earle Clubb - http://github.com/eclubb
|
238
|
+
|
239
|
+
Change Log
|
240
|
+
----------
|
241
|
+
|
242
|
+
[CHANGELOG](file.CHANGELOG.html)
|
243
|
+
|
244
|
+
License
|
245
|
+
-------
|
246
|
+
|
247
|
+
RCAP is released under the BSD License.
|
248
|
+
|
249
|
+
Copyright 2010 - 2011 AIMRED CC. All rights reserved.
|
250
|
+
|
251
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
252
|
+
|
253
|
+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
254
|
+
|
255
|
+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
256
|
+
|
257
|
+
THIS SOFTWARE IS PROVIDED BY AIMRED CC ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL AIMRED CC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
258
|
+
|
259
|
+
The views and conclusions contained in the software and documentation are those of the authors and should not be interpreted as representing official policies, either expressed or implied, of AIMRED CC.
|
data/Rakefile
CHANGED
@@ -1,18 +1,19 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'bundler'
|
3
|
-
|
4
|
-
require 'rdoc/task'
|
5
3
|
require 'rspec/core/rake_task'
|
4
|
+
require 'yard'
|
6
5
|
|
7
6
|
Bundler::GemHelper.install_tasks
|
8
7
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
8
|
+
YARD::Rake::YardocTask.new do |t|
|
9
|
+
t.files = ['lib/**/*.rb' ]
|
10
|
+
t.options += [ '--title', "RCAP Ruby API" ]
|
11
|
+
t.options += [ '--main', 'README.md']
|
12
|
+
t.options += [ '--files', 'CHANGELOG.md' ]
|
13
|
+
t.options += [ '--output-dir', 'doc' ]
|
14
14
|
end
|
15
15
|
|
16
|
+
|
16
17
|
RSpec::Core::RakeTask.new do |spec|
|
17
18
|
spec.rspec_opts = ['--options spec/spec.opts']
|
18
19
|
end
|
data/lib/extensions/array.rb
CHANGED
@@ -1,4 +1,10 @@
|
|
1
|
-
class Array
|
1
|
+
class Array
|
2
|
+
# Formats an array into a string suitable for a CAP message.
|
3
|
+
#
|
4
|
+
# @return [String]
|
5
|
+
# @example
|
6
|
+
# [ "one", "two words", "three" ].to_s_for_cap # => "one \"two words\" three"
|
7
|
+
# @see String#unpack_cap_list
|
2
8
|
def to_s_for_cap
|
3
9
|
self.map{ |element| element.to_s.for_cap_list }.join( ' ' )
|
4
10
|
end
|
data/lib/extensions/date_time.rb
CHANGED
@@ -1,9 +1,13 @@
|
|
1
|
-
class DateTime
|
1
|
+
class DateTime
|
2
2
|
RCAP_TIME_FORMAT = "%Y-%m-%dT%H:%M:%S"
|
3
3
|
RCAP_ZONE_FORMAT = "%+03i:00"
|
4
4
|
|
5
5
|
alias inspect to_s
|
6
6
|
|
7
|
+
# Returns a string representaion of the time suitable for CAP.
|
8
|
+
# @return [String]
|
9
|
+
# @example
|
10
|
+
# DateTime.now.to_s_for_cap # => "2011-10-26T21:45:00+02:00"
|
7
11
|
def to_s_for_cap
|
8
12
|
t = self.strftime( RCAP_TIME_FORMAT ) + format( RCAP_ZONE_FORMAT , utc_hours_offset )
|
9
13
|
t.sub(/\+(00:\d\d)$/, '-\1')
|
data/lib/extensions/string.rb
CHANGED
@@ -1,7 +1,14 @@
|
|
1
|
-
class String
|
1
|
+
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
|
6
|
+
# enclose the contents in quotation marks.
|
7
|
+
#
|
8
|
+
# @return [String]
|
9
|
+
# @example
|
10
|
+
# "one".for_cap_list # => "one"
|
11
|
+
# "two words".for_cap_list # => "\"two words\""
|
5
12
|
def for_cap_list
|
6
13
|
if self =~ /\s/
|
7
14
|
'"'+self+'"'
|
@@ -10,6 +17,12 @@ class String # :nodoc:
|
|
10
17
|
end
|
11
18
|
end
|
12
19
|
|
20
|
+
# Will unpack a string generated by {Array#to_s_for_cap}
|
21
|
+
#
|
22
|
+
# @return [Array<String>]
|
23
|
+
# @example
|
24
|
+
# "one \"two words\" three".unpack_cap_list # => [ "one", "two words", "three" ]
|
25
|
+
# @see Array#to_s_for_cap
|
13
26
|
def unpack_cap_list
|
14
27
|
self.split( CAP_LIST_REGEX ).reject{ |match| match == "" || match =~ WHITESPACE_REGEX }
|
15
28
|
end
|
data/lib/extensions/time.rb
CHANGED
@@ -1,7 +1,11 @@
|
|
1
|
-
class Time
|
1
|
+
class Time
|
2
2
|
RCAP_TIME_FORMAT = "%Y-%m-%dT%H:%M:%S"
|
3
3
|
RCAP_ZONE_FORMAT = "%+03i:00"
|
4
4
|
|
5
|
+
# Returns a string representaion of the time suitable for CAP.
|
6
|
+
# @return [String]
|
7
|
+
# @example
|
8
|
+
# Time.now.to_s_for_cap # => "2011-10-26T21:45:00+02:00"
|
5
9
|
def to_s_for_cap
|
6
10
|
t = self.strftime( RCAP_TIME_FORMAT ) + format( RCAP_ZONE_FORMAT , utc_hours_offset )
|
7
11
|
t.sub(/\+(00:\d\d)$/, '-\1')
|