rcap 0.1

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.
@@ -0,0 +1,26 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe( RCAP::Geocode ) do
4
+ context( 'when initialised' ) do
5
+ context( 'from XML' ) do
6
+ before( :each ) do
7
+ @original_geocode = RCAP::Geocode.new( :name => 'name', :value => 'value' )
8
+ @alert = RCAP::Alert.new( :infos => RCAP::Info.new( :areas => RCAP::Area.new( :geocodes => @original_geocode )))
9
+ @xml_string = @alert.to_xml
10
+ @xml_document = REXML::Document.new( @xml_string )
11
+ @info_xml_element = RCAP.xpath_first( @xml_document.root, RCAP::Info::XPATH )
12
+ @area_xml_element = RCAP.xpath_first( @info_xml_element, RCAP::Area::XPATH )
13
+ @geocode_xml_element = RCAP.xpath_first( @area_xml_element, RCAP::Geocode::XPATH )
14
+ @geocode = RCAP::Geocode.from_xml_element( @geocode_xml_element )
15
+ end
16
+
17
+ it( 'should parse the name correctly' ) do
18
+ @geocode.name.should == @original_geocode.name
19
+ end
20
+
21
+ it( 'should parse the value correctly' ) do
22
+ @geocode.value.should == @original_geocode.value
23
+ end
24
+ end
25
+ end
26
+ end
data/spec/info_spec.rb ADDED
@@ -0,0 +1,119 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe( RCAP::Info ) do
4
+ context( 'on initialisation' ) do
5
+ before( :each ) do
6
+ @info = RCAP::Info.new
7
+ end
8
+
9
+ it( 'should have a default language of en-US' ) { @info.language.should == 'en-US' }
10
+ it( 'should have no categories' ) { @info.categories.should( be_empty )}
11
+ it( 'should have no event' ) { @info.event.should( be_nil )}
12
+ it( 'should have no response types' ) { @info.response_types.should( be_empty )}
13
+ it( 'should have no urgency' ) { @info.urgency.should( be_nil )}
14
+ it( 'should have no severity' ) { @info.severity.should( be_nil )}
15
+ it( 'should have no certainty' ) { @info.certainty.should( be_nil )}
16
+ it( 'should have no audience' ) { @info.audience.should( be_nil )}
17
+ it( 'should have no event_codes' ) { @info.event_codes.should( be_empty )}
18
+ it( 'should have no effective datetime' ) { @info.effective.should( be_nil )}
19
+ it( 'should have no onset datetime' ) { @info.onset.should( be_nil )}
20
+ it( 'should have no expires datetime' ) { @info.expires.should( be_nil )}
21
+ it( 'should have no sender name ' ) { @info.sender_name.should( be_nil )}
22
+ it( 'should have no headline' ) { @info.headline.should( be_nil )}
23
+ it( 'should have no description' ) { @info.description.should( be_nil )}
24
+ it( 'should have no instruction' ) { @info.instruction.should( be_nil )}
25
+ it( 'should have no web' ) { @info.web.should( be_nil )}
26
+ it( 'should have no contact' ) { @info.contact.should( be_nil )}
27
+ it( 'should have no parameters' ) { @info.parameters.should( be_empty )}
28
+
29
+ context( 'from XML' ) do
30
+ before( :each ) do
31
+ @original_info = RCAP::Info.new( :categories => [ RCAP::Info::CATEGORY_GEO, RCAP::Info::CATEGORY_FIRE ],
32
+ :event => 'Event Description',
33
+ :response_types => [ RCAP::Info::RESPONSE_TYPE_MONITOR, RCAP::Info::RESPONSE_TYPE_ASSESS ],
34
+ :urgency => RCAP::Info::URGENCY_IMMEDIATE,
35
+ :severity => RCAP::Info::SEVERITY_EXTREME,
36
+ :certainty => RCAP::Info::CERTAINTY_OBSERVED,
37
+ :audience => 'Audience',
38
+ :effective => DateTime.now,
39
+ :onset => DateTime.now + 1,
40
+ :expires => DateTime.now + 2,
41
+ :sender_name => 'Sender Name',
42
+ :headline => 'Headline',
43
+ :description => 'Description',
44
+ :instruction => 'Instruction',
45
+ :web => 'http://website',
46
+ :contact => 'contact@address',
47
+ :event_codes => [ RCAP::EventCode.new( :name => 'name1', :value => 'value1' ),
48
+ RCAP::EventCode.new( :name => 'name2', :value => 'value2' )],
49
+ :parameters => [ RCAP::Parameter.new( :name => 'name1', :value => 'value1' ),
50
+ RCAP::Parameter.new( :name => 'name2', :value => 'value2' )],
51
+ :areas => [ RCAP::Area.new( :area_desc => 'Area1' ),
52
+ RCAP::Area.new( :area_desc => 'Area2' )]
53
+ )
54
+ @alert = RCAP::Alert.new( :infos => @original_info )
55
+ @xml_string = @alert.to_xml
56
+ @xml_document = REXML::Document.new( @xml_string )
57
+ @info = RCAP::Info.from_xml_element( RCAP.xpath_first( @xml_document.root, RCAP::Info::XPATH ))
58
+ end
59
+ it( 'should parse categories correctly' ){ @info .categories.should == @original_info.categories }
60
+ it( 'should parse event correctly' ){ @info .event.should == @original_info.event }
61
+ it( 'should parse response_types correctly' ){ @info .response_types.should == @original_info.response_types }
62
+ it( 'should parse urgency correctly' ){ @info .urgency.should == @original_info.urgency }
63
+ it( 'should parse severity correctly' ){ @info .severity.should == @original_info.severity }
64
+ it( 'should parse certainty correctly' ){ @info .certainty.should == @original_info.certainty }
65
+ it( 'should parse audience correctly' ){ @info .audience.should == @original_info.audience }
66
+ it( 'should parse effective correctly' ){ @info .effective.should( be_close( @original_info.effective, Rational( 1, 86400 )))}
67
+ it( 'should parse onset correctly' ){ @info .onset.should( be_close( @original_info.onset, Rational( 1, 86400 )))}
68
+ it( 'should parse expires correctly' ){ @info .expires.should( be_close( @original_info.expires, Rational( 1, 86400 )))}
69
+ it( 'should parse sender_name correctly' ){ @info .sender_name.should == @original_info.sender_name }
70
+ it( 'should parse headline correctly' ){ @info .headline.should == @original_info.headline }
71
+ it( 'should parse description correctly' ){ @info .description.should == @original_info.description }
72
+ it( 'should parse instruction correctly' ){ @info .instruction.should == @original_info.instruction }
73
+ it( 'should parse web correctly' ){ @info .web.should == @original_info.web }
74
+ it( 'should parse contact correctly' ){ @info .contact.should == @original_info.contact }
75
+ it( 'should parse event_codes correctly' ){ @info .event_codes.should == @original_info.event_codes }
76
+ it( 'should parse parameters correctly' ){ @info .parameters.should == @original_info.parameters }
77
+ it( 'should parse resources correctly' ){ @info .resources.should == @original_info.resources }
78
+ it( 'should parse areas correctly' ){ @info .areas.should == @original_info.areas }
79
+ end
80
+ end
81
+
82
+ context( 'is not valid if it' ) do
83
+ before( :each ) do
84
+ @info = RCAP::Info.new( :event => 'Info Event',
85
+ :categories => RCAP::Info::CATEGORY_GEO,
86
+ :urgency => RCAP::Info::URGENCY_IMMEDIATE,
87
+ :severity => RCAP::Info::SEVERITY_EXTREME,
88
+ :certainty => RCAP::Info::CERTAINTY_OBSERVED )
89
+ @info.valid?
90
+ puts @info.errors.full_messages
91
+ @info.should( be_valid )
92
+ end
93
+
94
+ it( 'does not have an event' ) do
95
+ @info.event = nil
96
+ @info.should_not( be_valid )
97
+ end
98
+
99
+ it( 'does not have categories' ) do
100
+ @info.categories.clear
101
+ @info.should_not( be_valid )
102
+ end
103
+
104
+ it( 'does not have an urgency' ) do
105
+ @info.urgency = nil
106
+ @info.should_not( be_valid )
107
+ end
108
+
109
+ it( 'does not have an severity' ) do
110
+ @info.severity = nil
111
+ @info.should_not( be_valid )
112
+ end
113
+
114
+ it( 'does not have an certainty' ) do
115
+ @info.certainty = nil
116
+ @info.should_not( be_valid )
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe( RCAP::Point ) do
4
+ describe( 'is not valid if' ) do
5
+ before( :each ) do
6
+ @point = RCAP::Point.new( :lattitude => 0, :longitude => 0 )
7
+ @point.should( be_valid )
8
+ end
9
+
10
+ it( 'does not have a longitude defined' ) do
11
+ @point.longitude = nil
12
+ @point.should_not( be_valid )
13
+ end
14
+
15
+ it( 'does not have a valid longitude' ) do
16
+ @point.longitude = RCAP::Point::MAX_LONGITUDE + 1
17
+ @point.should_not( be_valid )
18
+ @point.longitude = RCAP::Point::MIN_LONGITUDE - 1
19
+ @point.should_not( be_valid )
20
+ end
21
+
22
+ it( 'does not have a lattitude defined' ) do
23
+ @point.lattitude = nil
24
+ @point.should_not( be_valid )
25
+ end
26
+
27
+ it( 'does not have a valid lattitude' ) do
28
+ @point.lattitude = RCAP::Point::MAX_LATTITUDE + 1
29
+ @point.should_not( be_valid )
30
+ @point.lattitude = RCAP::Point::MIN_LATTITUDE - 1
31
+ @point.should_not( be_valid )
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,45 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe( RCAP::Polygon ) do
4
+ describe( 'is not valid if it' ) do
5
+ before( :each ) do
6
+ @polygon = RCAP::Polygon.new
7
+ 3.times do
8
+ @polygon.points << RCAP::Point.new( :lattitude => 0, :longitude => 0 )
9
+ end
10
+ @polygon.should( be_valid )
11
+ end
12
+
13
+ it( 'does not have any points' ) do
14
+ @polygon.points.clear
15
+ @polygon.should_not( be_valid )
16
+ end
17
+
18
+ it( 'does not have a valid collection of points' ) do
19
+ @polygon.points.first.lattitude = nil
20
+ @polygon.should_not( be_valid )
21
+ end
22
+ end
23
+
24
+ context( 'on initialization' ) do
25
+ context( 'from XML' ) do
26
+ before( :each ) do
27
+ @original_polygon = RCAP::Polygon.new( :points => Array.new(3){|i| RCAP::Point.new( :lattitude => i, :longitude => i )})
28
+ @alert = RCAP::Alert.new( :infos => RCAP::Info.new( :areas => RCAP::Area.new( :polygons => @original_polygon )))
29
+ @xml_string = @alert.to_xml
30
+ @xml_document = REXML::Document.new( @xml_string )
31
+ @info_element = RCAP.xpath_first( @xml_document.root, RCAP::Info::XPATH )
32
+ @area_element = RCAP.xpath_first( @info_element, RCAP::Area::XPATH )
33
+ @polygon_element = RCAP.xpath_first( @area_element, RCAP::Polygon::XPATH )
34
+ @polygon = RCAP::Polygon.from_xml_element( @polygon_element )
35
+ end
36
+
37
+ it( 'should parse all the points' ) do
38
+ @polygon.points.zip( @original_polygon.points ).each do |point, original_point|
39
+ point.lattitude.should == original_point.lattitude
40
+ point.longitude.should == original_point.longitude
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,74 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe( RCAP::Resource ) do
4
+ context( 'on initialisation' ) do
5
+ before( :each ) do
6
+ @resource = RCAP::Resource.new
7
+ end
8
+
9
+ it( 'should have no mime_type' ){ @resource.mime_type.should( be_nil )}
10
+ it( 'should have no size' ){ @resource.size.should( be_nil )}
11
+ it( 'should have no uri' ){ @resource.uri.should( be_nil )}
12
+ it( 'should have no deref_uri' ){ @resource.deref_uri.should( be_nil )}
13
+ it( 'should have no digest' ){ @resource.digest.should( be_nil )}
14
+ it( 'should have no resource_desc' ){ @resource.resource_desc.should( be_nil )}
15
+
16
+ context( 'from XML' ) do
17
+ before( :each ) do
18
+ @original_resource = RCAP::Resource.new
19
+ @original_resource.resource_desc = "Image of incident"
20
+ @original_resource.uri = "http://capetown.gov.za/cap/resources/image.png"
21
+ @original_resource.mime_type = 'image/png'
22
+ @original_resource.deref_uri = "IMAGE DATA"
23
+ @original_resource.size = "20480"
24
+ @original_resource.digest = "2048"
25
+
26
+ @alert = RCAP::Alert.new( :infos => RCAP::Info.new( :resources => @original_resource ))
27
+ @xml_string = @alert.to_xml
28
+ @xml_document = REXML::Document.new( @xml_string )
29
+ @info_element = RCAP.xpath_first( @xml_document.root, RCAP::Info::XPATH )
30
+ @resource_element = RCAP.xpath_first( @info_element, RCAP::Resource::XPATH )
31
+ @resource_element.should_not( be_nil )
32
+ @resource = RCAP::Resource.from_xml_element( @resource_element )
33
+ end
34
+
35
+ it( 'should parse resource_desc correctly' ) do
36
+ @resource.resource_desc.should == @original_resource.resource_desc
37
+ end
38
+
39
+ it( 'should parse uri correctly' ) do
40
+ @resource.uri.should == @original_resource.uri
41
+ end
42
+
43
+ it( 'should parse mime_type correctly' ) do
44
+ @resource.mime_type.should == @original_resource.mime_type
45
+ end
46
+
47
+ it( 'should parse deref_uri correctly' ) do
48
+ @resource.deref_uri.should == @original_resource.deref_uri
49
+ end
50
+
51
+ it( 'should parse size correctly' ) do
52
+ @resource.size.should == @original_resource.size
53
+ end
54
+
55
+ it( 'should parse digest correctly' ) do
56
+ @resource.digest.should == @original_resource.digest
57
+ end
58
+ end
59
+ end
60
+
61
+ context( 'which is valid' ) do
62
+ before( :each ) do
63
+ @resource = RCAP::Resource.new( :resource_desc => 'Resource Description' )
64
+ @resource.should( be_valid )
65
+ end
66
+
67
+ describe( 'should not be valid if it' ) do
68
+ it( 'does not have a resource description (resource_desc)' ) do
69
+ @resource.resource_desc = nil
70
+ @resource.should_not( be_valid )
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,7 @@
1
+ require 'lib/rcap'
2
+
3
+ def random_circle_hash
4
+ { :lattitude => rand( 360 ) - 180,
5
+ :longitude => rand( 180 ) - 90,
6
+ :radius => rand( 50 ) }
7
+ end
@@ -0,0 +1,57 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe( Array ) do
4
+ describe( 'to_s_for_cap' ) do
5
+ context( 'with an element containing white space' ) do
6
+ before( :all ) do
7
+ @list = [ 'one', 'white space', 'three' ]
8
+ end
9
+
10
+ it( 'should format the list correctly' ) do
11
+ @list.to_s_for_cap.should == 'one "white space" three'
12
+ end
13
+ end
14
+
15
+ context( 'without an element containing white space' ) do
16
+ before( :all ) do
17
+ @list = [ 'one', 'two', 'three' ]
18
+ end
19
+ it( 'should format the list correctly' ) do
20
+ @list.to_s_for_cap.should == 'one two three'
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ describe( String ) do
27
+ describe( 'for_cap_list' ) do
28
+ context( 'with white space' ) do
29
+ before( :all ) do
30
+ @string = 'white space'
31
+ end
32
+
33
+ it( 'should format the string correctly' ) do
34
+ @string.for_cap_list.should == '"white space"'
35
+ end
36
+ end
37
+
38
+ context( 'without white space' ) do
39
+ before( :all ) do
40
+ @string= 'one'
41
+ end
42
+ it( 'should format the string correctly' ) do
43
+ @string.for_cap_list.should == 'one'
44
+ end
45
+ end
46
+ end
47
+
48
+ describe( 'unpack_cap_list' ) do
49
+ it( 'shoud unpack strings in quotes correctly' ) do
50
+ 'Item1 "Item 2" Item3'.unpack_cap_list.should == [ "Item1", "Item 2", "Item3" ]
51
+ end
52
+
53
+ it( 'should unpack strings correclty' ) do
54
+ 'Item1 Item2 Item3'.unpack_cap_list.should == [ "Item1", "Item2", "Item3" ]
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,95 @@
1
+ require 'spec/spec_helper'
2
+
3
+ class ObjectWithValidatesCollection
4
+ include Validation
5
+ attr_accessor( :collection )
6
+ validates_collection_of( :collection )
7
+ def initialize
8
+ @collection = []
9
+ end
10
+ end
11
+
12
+ class DependencyObject
13
+ include Validation
14
+ attr_accessor( :dependent_value, :contingent_value )
15
+ validates_dependency_of( :dependent_value, :on => :contingent_value )
16
+ end
17
+
18
+ class DependencyWithObject
19
+ include Validation
20
+ attr_accessor( :dependent_value, :contingent_value )
21
+ validates_dependency_of( :dependent_value, :on => :contingent_value, :with_value => true )
22
+ end
23
+
24
+ describe( Validation::ClassMethods ) do
25
+ describe( 'validates_collection_of' ) do
26
+ before( :each ) do
27
+ @object_with_collection = ObjectWithValidatesCollection.new
28
+ end
29
+
30
+ it( 'should be valid if all the members are valid' ) do
31
+ @object_with_collection.collection = Array.new(3){ RCAP::Point.new( :lattitude => 0, :longitude => 0 )}
32
+ @object_with_collection.should( be_valid )
33
+ end
34
+
35
+ it( 'should not be valid some of the members are invalid' ) do
36
+ @object_with_collection.collection = Array.new( 2 ){ RCAP::Point.new( :lattitude => 0, :longitude => 0 )} + [ RCAP::Point.new( :lattitude => "not a number", :longitude => 0)]
37
+ @object_with_collection.should_not( be_valid )
38
+ end
39
+ end
40
+
41
+ describe( 'validates_dependency_of' ) do
42
+ context( 'without :with_value' ) do
43
+ before( :each ) do
44
+ @object = DependencyObject.new
45
+ @object.dependent_value = true
46
+ @object.contingent_value = true
47
+ @object.should( be_valid )
48
+ end
49
+
50
+ it( 'should not be valid if the contigent value is nil' ) do
51
+ @object.contingent_value = nil
52
+ @object.should_not( be_valid )
53
+ end
54
+ it( 'should be valid if the dependent value is nil' ) do
55
+ @object.dependent_value = nil
56
+ @object.should( be_valid )
57
+ end
58
+ it( 'should be valid if both are nil' ) do
59
+ @object.dependent_value = nil
60
+ @object.contingent_value = nil
61
+ @object.should( be_valid )
62
+ end
63
+ end
64
+
65
+ context( 'with :with_value' ) do
66
+ before( :each ) do
67
+ @object = DependencyWithObject.new
68
+ @object.dependent_value = true
69
+ @object.contingent_value = true
70
+ @object.should( be_valid )
71
+ end
72
+
73
+ it( 'should not be valid if the contigent value is nil' ) do
74
+ @object.contingent_value = nil
75
+ @object.should_not( be_valid )
76
+ end
77
+
78
+ it( 'should not be valid if the contingent value is not the required value' ) do
79
+ @object.contingent_value = 0
80
+ @object.should_not( be_valid )
81
+ end
82
+
83
+ it( 'should be valid if the dependent value is nil' ) do
84
+ @object.dependent_value = nil
85
+ @object.should( be_valid )
86
+ end
87
+
88
+ it( 'should be valid if both are nil' ) do
89
+ @object.dependent_value = nil
90
+ @object.contingent_value = nil
91
+ @object.should( be_valid )
92
+ end
93
+ end
94
+ end
95
+ end