cosm-rb 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (122) hide show
  1. data/.gitignore +12 -0
  2. data/.rbenv-version +1 -0
  3. data/.rspec +2 -0
  4. data/.rvmrc +1 -0
  5. data/.travis.yml +10 -0
  6. data/CHANGELOG.md +91 -0
  7. data/Gemfile +4 -0
  8. data/MIT-LICENSE +20 -0
  9. data/README.md +195 -0
  10. data/Rakefile +35 -0
  11. data/ci/build_hudson.sh +24 -0
  12. data/cosm-rb.gemspec +40 -0
  13. data/init.rb +2 -0
  14. data/lib/cosm-rb/array_extensions.rb +6 -0
  15. data/lib/cosm-rb/base/instance_methods.rb +28 -0
  16. data/lib/cosm-rb/base.rb +52 -0
  17. data/lib/cosm-rb/client.rb +7 -0
  18. data/lib/cosm-rb/datapoint.rb +71 -0
  19. data/lib/cosm-rb/datastream.rb +136 -0
  20. data/lib/cosm-rb/feed.rb +108 -0
  21. data/lib/cosm-rb/hash_extensions.rb +16 -0
  22. data/lib/cosm-rb/helpers.rb +42 -0
  23. data/lib/cosm-rb/key.rb +98 -0
  24. data/lib/cosm-rb/nil_content.rb +15 -0
  25. data/lib/cosm-rb/object_extensions.rb +6 -0
  26. data/lib/cosm-rb/parsers/csv/datastream_defaults.rb +12 -0
  27. data/lib/cosm-rb/parsers/csv/feed_defaults.rb +38 -0
  28. data/lib/cosm-rb/parsers/defaults.rb +13 -0
  29. data/lib/cosm-rb/parsers/json/datapoint_defaults.rb +12 -0
  30. data/lib/cosm-rb/parsers/json/datastream_defaults.rb +53 -0
  31. data/lib/cosm-rb/parsers/json/feed_defaults.rb +103 -0
  32. data/lib/cosm-rb/parsers/json/key_defaults.rb +15 -0
  33. data/lib/cosm-rb/parsers/json/search_result_defaults.rb +18 -0
  34. data/lib/cosm-rb/parsers/json/trigger_defaults.rb +12 -0
  35. data/lib/cosm-rb/parsers/xml/datapoint_defaults.rb +20 -0
  36. data/lib/cosm-rb/parsers/xml/datastream_defaults.rb +77 -0
  37. data/lib/cosm-rb/parsers/xml/feed_defaults.rb +127 -0
  38. data/lib/cosm-rb/parsers/xml/key_defaults.rb +40 -0
  39. data/lib/cosm-rb/parsers/xml/trigger_defaults.rb +22 -0
  40. data/lib/cosm-rb/permission.rb +67 -0
  41. data/lib/cosm-rb/resource.rb +43 -0
  42. data/lib/cosm-rb/search_result.rb +63 -0
  43. data/lib/cosm-rb/string_extensions.rb +6 -0
  44. data/lib/cosm-rb/template.rb +32 -0
  45. data/lib/cosm-rb/templates/csv/datapoint_defaults.rb +22 -0
  46. data/lib/cosm-rb/templates/csv/datastream_defaults.rb +43 -0
  47. data/lib/cosm-rb/templates/csv/feed_defaults.rb +47 -0
  48. data/lib/cosm-rb/templates/defaults.rb +14 -0
  49. data/lib/cosm-rb/templates/json/datapoint_defaults.rb +15 -0
  50. data/lib/cosm-rb/templates/json/datastream_defaults.rb +66 -0
  51. data/lib/cosm-rb/templates/json/feed_defaults.rb +134 -0
  52. data/lib/cosm-rb/templates/json/key_defaults.rb +41 -0
  53. data/lib/cosm-rb/templates/json/search_result_defaults.rb +47 -0
  54. data/lib/cosm-rb/templates/json/trigger_defaults.rb +21 -0
  55. data/lib/cosm-rb/templates/xml/datapoint_defaults.rb +25 -0
  56. data/lib/cosm-rb/templates/xml/datastream_defaults.rb +66 -0
  57. data/lib/cosm-rb/templates/xml/feed_defaults.rb +110 -0
  58. data/lib/cosm-rb/templates/xml/search_result_defaults.rb +116 -0
  59. data/lib/cosm-rb/templates/xml_headers.rb +17 -0
  60. data/lib/cosm-rb/trigger.rb +65 -0
  61. data/lib/cosm-rb/validations.rb +9 -0
  62. data/lib/cosm-rb/version.rb +3 -0
  63. data/lib/cosm-rb.rb +29 -0
  64. data/spec/cosm-rb/array_extensions_spec.rb +9 -0
  65. data/spec/cosm-rb/base/instance_methods_spec.rb +109 -0
  66. data/spec/cosm-rb/base_spec.rb +56 -0
  67. data/spec/cosm-rb/client_spec.rb +7 -0
  68. data/spec/cosm-rb/datapoint_spec.rb +169 -0
  69. data/spec/cosm-rb/datastream_spec.rb +301 -0
  70. data/spec/cosm-rb/feed_spec.rb +298 -0
  71. data/spec/cosm-rb/hash_extensions_spec.rb +20 -0
  72. data/spec/cosm-rb/helpers_spec.rb +56 -0
  73. data/spec/cosm-rb/key_spec.rb +178 -0
  74. data/spec/cosm-rb/parsers/csv/datastream_defaults_spec.rb +12 -0
  75. data/spec/cosm-rb/parsers/csv/feed_defaults_spec.rb +35 -0
  76. data/spec/cosm-rb/parsers/json/datapoint_defaults_spec.rb +15 -0
  77. data/spec/cosm-rb/parsers/json/datastream_defaults_spec.rb +82 -0
  78. data/spec/cosm-rb/parsers/json/feed_defaults_spec.rb +18 -0
  79. data/spec/cosm-rb/parsers/json/key_defaults_spec.rb +8 -0
  80. data/spec/cosm-rb/parsers/json/search_result_defaults_spec.rb +12 -0
  81. data/spec/cosm-rb/parsers/json/trigger_defaults_spec.rb +16 -0
  82. data/spec/cosm-rb/parsers/xml/datapoint_defaults_spec.rb +19 -0
  83. data/spec/cosm-rb/parsers/xml/datastream_defaults_spec.rb +63 -0
  84. data/spec/cosm-rb/parsers/xml/feed_defaults_spec.rb +65 -0
  85. data/spec/cosm-rb/parsers/xml/key_defaults_spec.rb +16 -0
  86. data/spec/cosm-rb/parsers/xml/trigger_defaults_spec.rb +16 -0
  87. data/spec/cosm-rb/search_result_spec.rb +258 -0
  88. data/spec/cosm-rb/string_extensions_spec.rb +12 -0
  89. data/spec/cosm-rb/template_spec.rb +74 -0
  90. data/spec/cosm-rb/templates/csv/datapoint_defaults_spec.rb +41 -0
  91. data/spec/cosm-rb/templates/csv/datastream_defaults_spec.rb +131 -0
  92. data/spec/cosm-rb/templates/csv/feed_defaults_spec.rb +78 -0
  93. data/spec/cosm-rb/templates/json/datapoint_defaults_spec.rb +14 -0
  94. data/spec/cosm-rb/templates/json/datastream_defaults_spec.rb +170 -0
  95. data/spec/cosm-rb/templates/json/feed_defaults_spec.rb +397 -0
  96. data/spec/cosm-rb/templates/json/key_defaults_spec.rb +29 -0
  97. data/spec/cosm-rb/templates/json/search_result_defaults_spec.rb +37 -0
  98. data/spec/cosm-rb/templates/json/trigger_defaults_spec.rb +19 -0
  99. data/spec/cosm-rb/templates/xml/datapoint_defaults_spec.rb +14 -0
  100. data/spec/cosm-rb/templates/xml/datastream_defaults_spec.rb +113 -0
  101. data/spec/cosm-rb/templates/xml/feed_defaults_spec.rb +131 -0
  102. data/spec/cosm-rb/templates/xml/search_result_defaults_spec.rb +44 -0
  103. data/spec/cosm-rb/trigger_spec.rb +117 -0
  104. data/spec/fixtures/models.rb +81 -0
  105. data/spec/spec_helper.rb +17 -0
  106. data/spec/support/contain_datapoint_eeml_matcher.rb +16 -0
  107. data/spec/support/contain_datastream_eeml_matcher.rb +60 -0
  108. data/spec/support/contain_feed_eeml_matcher.rb +96 -0
  109. data/spec/support/datapoint_helper.rb +53 -0
  110. data/spec/support/datastream_helper.rb +300 -0
  111. data/spec/support/describe_eeml_matcher.rb +23 -0
  112. data/spec/support/feed_helper.rb +771 -0
  113. data/spec/support/fully_represent_datapoint_matcher.rb +30 -0
  114. data/spec/support/fully_represent_datastream_matcher.rb +92 -0
  115. data/spec/support/fully_represent_feed_matcher.rb +226 -0
  116. data/spec/support/fully_represent_key_matcher.rb +74 -0
  117. data/spec/support/fully_represent_search_result_matcher.rb +67 -0
  118. data/spec/support/fully_represent_trigger_matcher.rb +29 -0
  119. data/spec/support/key_helper.rb +74 -0
  120. data/spec/support/search_result_helper.rb +252 -0
  121. data/spec/support/trigger_helper.rb +51 -0
  122. metadata +363 -0
@@ -0,0 +1,113 @@
1
+ require 'spec_helper'
2
+
3
+ describe "default datastream xml templates" do
4
+ before(:each) do
5
+ @datastream = Cosm::Datastream.new(datastream_as_(:hash))
6
+ end
7
+
8
+ context "0.5.1 (used by API V2)" do
9
+ it "should be the default" do
10
+ @datastream.generate_xml("0.5.1").should == @datastream.to_xml
11
+ end
12
+
13
+ it "should represent Pachube EEML" do
14
+ xml = Nokogiri.parse(@datastream.generate_xml("0.5.1"))
15
+ xml.should describe_eeml_for_version("0.5.1")
16
+ xml.should contain_datastream_eeml_for_version("0.5.1")
17
+ end
18
+
19
+ it "should ignore datapoints_function if it is not set" do
20
+ @datastream.datapoints_function = nil
21
+ xml = @datastream.generate_xml("0.5.1")
22
+ Nokogiri.parse(xml).xpath("//xmlns:datapoints").first.attributes.should == {}
23
+ end
24
+
25
+ it "should use datapoints_function if it is set" do
26
+ @datastream.datapoints_function = 'average'
27
+ xml = @datastream.generate_xml("0.5.1")
28
+ Nokogiri.parse(xml).xpath("//xmlns:datapoints").first.attributes["function"].value.should == 'average'
29
+ end
30
+
31
+ it "should handle nil updated" do
32
+ @datastream.updated = nil
33
+ lambda {@datastream.generate_xml("0.5.1")}.should_not raise_error
34
+ end
35
+
36
+ it "should handle a lack of tags" do
37
+ @datastream.tags = nil
38
+ lambda {@datastream.generate_xml("0.5.1")}.should_not raise_error
39
+ end
40
+
41
+ it "should handle blank tags" do
42
+ @datastream.tags = ""
43
+ lambda {@datastream.generate_xml("0.5.1")}.should_not raise_error
44
+ end
45
+
46
+ it "should handle a lack of datapoints" do
47
+ @datastream.datapoints = []
48
+ xml = @datastream.generate_xml("0.5.1")
49
+ Nokogiri.parse(xml).xpath("//xmlns:datapoints").should be_empty
50
+ end
51
+
52
+ it "should ignore blank attributes" do
53
+ @datastream.unit_symbol = nil
54
+ @datastream.unit_label = "Woohoo"
55
+ @datastream.unit_type = "Type A"
56
+ Nokogiri.parse(@datastream.generate_xml("0.5.1")).at_xpath("//xmlns:unit").attributes["symbol"].should be_nil
57
+ end
58
+
59
+ it "should ignore nil max/min value" do
60
+ @datastream.max_value = nil
61
+ @datastream.min_value = nil
62
+ Nokogiri.parse(@datastream.generate_xml("0.5.1")).at_xpath("//xmlns:max_value").should be_nil
63
+ Nokogiri.parse(@datastream.generate_xml("0.5.1")).at_xpath("//xmlns:min_value").should be_nil
64
+ end
65
+
66
+ it "should ignore blank units" do
67
+ @datastream.unit_symbol = nil
68
+ @datastream.unit_label = nil
69
+ @datastream.unit_type = nil
70
+ Nokogiri.parse(@datastream.generate_xml("0.5.1")).at_xpath("//xmlns:unit").should be_nil
71
+ end
72
+ end
73
+
74
+ context "5 (used by API V1)" do
75
+ it "should represent Pachube EEML" do
76
+ xml = Nokogiri.parse(@datastream.generate_xml("5"))
77
+ xml.should describe_eeml_for_version("5")
78
+ xml.should contain_datastream_eeml_for_version("5")
79
+ end
80
+
81
+ it "should handle nil updated" do
82
+ @datastream.updated = nil
83
+ lambda {@datastream.generate_xml("5")}.should_not raise_error
84
+ end
85
+
86
+ it "should handle a lack of tags" do
87
+ @datastream.tags = nil
88
+ lambda {@datastream.generate_xml("5")}.should_not raise_error
89
+ end
90
+
91
+ it "should handle blank tags" do
92
+ @datastream.tags = ""
93
+ lambda {@datastream.generate_xml("5")}.should_not raise_error
94
+ end
95
+
96
+ it "should ignore blank attributes" do
97
+ @datastream.max_value = nil
98
+ @datastream.unit_symbol = nil
99
+ @datastream.unit_label = "Woohoo"
100
+ @datastream.unit_type = "Type A"
101
+ Nokogiri.parse(@datastream.generate_xml("5")).at_xpath("//xmlns:unit").attributes["symbol"].should be_nil
102
+ Nokogiri.parse(@datastream.generate_xml("5")).at_xpath("//xmlns:value").attributes["maxValue"].should be_nil
103
+ end
104
+
105
+ it "should ignore blank units" do
106
+ @datastream.unit_symbol = nil
107
+ @datastream.unit_label = nil
108
+ @datastream.unit_type = nil
109
+ Nokogiri.parse(@datastream.generate_xml("5")).at_xpath("//xmlns:unit").should be_nil
110
+ end
111
+ end
112
+ end
113
+
@@ -0,0 +1,131 @@
1
+ require 'spec_helper'
2
+
3
+ describe "default feed xml templates" do
4
+ before(:each) do
5
+ @feed = Cosm::Feed.new(feed_as_(:hash))
6
+ end
7
+
8
+ context "0.5.1 (used by API V2)" do
9
+ it "should be the default" do
10
+ @feed.should_receive(:generate_xml).with("0.5.1", {})
11
+ @feed.to_xml
12
+ end
13
+
14
+ it "should represent Pachube EEML" do
15
+ xml = Nokogiri.parse(@feed.generate_xml("0.5.1"))
16
+ xml.should describe_eeml_for_version("0.5.1")
17
+ xml.should contain_feed_eeml_for_version("0.5.1")
18
+ end
19
+
20
+ it "should handle a lack of updated" do
21
+ @feed.updated = nil
22
+ lambda {@feed.generate_xml("0.5.1")}.should_not raise_error
23
+ end
24
+
25
+ it "should handle a lack of tags" do
26
+ @feed.tags = nil
27
+ lambda {@feed.generate_xml("0.5.1")}.should_not raise_error
28
+ end
29
+
30
+ it "should handle blank tags" do
31
+ @feed.tags = ""
32
+ lambda {@feed.generate_xml("0.5.1")}.should_not raise_error
33
+ end
34
+
35
+ it "should ignore blank min/max values" do
36
+ @feed.datastreams.each do |ds|
37
+ ds.max_value = nil
38
+ ds.min_value = nil
39
+ end
40
+ Nokogiri.parse(@feed.generate_xml("0.5.1")).xpath("//xmlns:max_value").should be_empty
41
+ Nokogiri.parse(@feed.generate_xml("0.5.1")).xpath("//xmlns:min_value").should be_empty
42
+ end
43
+
44
+ it "should ignore blank attributes" do
45
+ @feed.datastreams.each do |ds|
46
+ ds.unit_symbol = nil
47
+ ds.unit_label = "Woohoo"
48
+ ds.unit_type = "Type A"
49
+ end
50
+ Nokogiri.parse(@feed.generate_xml("0.5.1")).xpath("//xmlns:unit").each do |unit_node|
51
+ unit_node.attributes["symbol"].should be_nil
52
+ end
53
+ end
54
+
55
+ %w(status feed description icon website email title auto_feed_url owner_login).each do |node|
56
+ it "should ignore blank '#{node}'" do
57
+ @feed.send("#{node}=", nil)
58
+ Nokogiri.parse(@feed.generate_xml("0.5.1")).xpath("//xmlns:#{node}").should be_blank
59
+ end
60
+ end
61
+
62
+ it "should ignore blank units" do
63
+ @feed.datastreams.each do |ds|
64
+ ds.unit_symbol = nil
65
+ ds.unit_label = nil
66
+ ds.unit_type = nil
67
+ end
68
+ Nokogiri.parse(@feed.generate_xml("0.5.1")).xpath("//xmlns:unit").should be_blank
69
+ end
70
+
71
+ end
72
+
73
+ context "5 (used by API V1)" do
74
+
75
+ it "should represent Pachube EEML" do
76
+ xml = Nokogiri.parse(@feed.generate_xml("5"))
77
+ xml.should describe_eeml_for_version("5")
78
+ xml.should contain_feed_eeml_for_version("5")
79
+ end
80
+
81
+ it "should handle a lack of updated" do
82
+ @feed.updated = nil
83
+ lambda {@feed.generate_xml("5")}.should_not raise_error
84
+ end
85
+
86
+ it "should ignore blank attributes" do
87
+ @feed.datastreams.each do |ds|
88
+ ds.max_value = nil
89
+ ds.unit_symbol = nil
90
+ ds.unit_label = "Woohoo"
91
+ ds.unit_type = "Type A"
92
+ end
93
+ Nokogiri.parse(@feed.generate_xml("5")).xpath("//xmlns:unit").each do |unit_node|
94
+ unit_node.attributes["symbol"].should be_nil
95
+ end
96
+ Nokogiri.parse(@feed.generate_xml("5")).xpath("//xmlns:value").each do |value_node|
97
+ value_node.attributes["maxValue"].should be_nil
98
+ end
99
+ end
100
+
101
+ it "should ignore blank units" do
102
+ @feed.datastreams.each do |ds|
103
+ ds.unit_symbol = nil
104
+ ds.unit_label = nil
105
+ ds.unit_type = nil
106
+ end
107
+ Nokogiri.parse(@feed.generate_xml("5")).xpath("//xmlns:unit").should be_blank
108
+ end
109
+
110
+ it "should ignore blank locations" do
111
+ @feed.location_disposition = nil
112
+ @feed.location_domain = nil
113
+ @feed.location_ele = nil
114
+ @feed.location_exposure = nil
115
+ @feed.location_lat = nil
116
+ @feed.location_lon = nil
117
+ @feed.location_name = nil
118
+ Nokogiri.parse(@feed.generate_xml("5")).xpath("//xmlns:location").should be_blank
119
+ end
120
+
121
+ %w(status feed description icon website email title).each do |node|
122
+ it "should ignore blank '#{node}'" do
123
+ @feed.send("#{node}=", nil)
124
+ Nokogiri.parse(@feed.generate_xml("5")).xpath("//xmlns:#{node}").should be_blank
125
+ end
126
+ end
127
+
128
+ end
129
+
130
+ end
131
+
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ describe "default feed xml templates" do
4
+ before(:each) do
5
+ @feed = Cosm::Feed.new(feed_as_(:hash))
6
+ @search_result = Cosm::SearchResult.new("totalResults" => 198, "startIndex" => 4, "itemsPerPage" => 15, "results" => [@feed])
7
+ end
8
+
9
+ context "0.5.1 (used by API V2)" do
10
+ it "should be the default" do
11
+ @search_result.should_receive(:generate_xml).with("0.5.1")
12
+ @search_result.to_xml
13
+ end
14
+
15
+ it "should represent Pachube EEML" do
16
+ xml = Nokogiri.parse(@search_result.generate_xml("0.5.1"))
17
+ xml.at_xpath("//opensearch:totalResults").content.should == "198"
18
+ xml.at_xpath("//opensearch:startIndex").content.should == "4"
19
+ xml.at_xpath("//opensearch:itemsPerPage").content.should == "15"
20
+ xml.should describe_eeml_for_version("0.5.1")
21
+ xml.xpath("//xmlns:environment").should_not be_empty
22
+ xml.xpath("//xmlns:environment").each do |feed_xml|
23
+ feed_xml.should contain_feed_eeml_for_version("0.5.1")
24
+ end
25
+ end
26
+ end
27
+
28
+ context "5 (used by API V1)" do
29
+
30
+ it "should represent Pachube EEML" do
31
+ xml = Nokogiri.parse(@search_result.generate_xml("5"))
32
+ xml.at_xpath("//opensearch:totalResults").content.should == "198"
33
+ xml.at_xpath("//opensearch:startIndex").content.should == "4"
34
+ xml.at_xpath("//opensearch:itemsPerPage").content.should == "15"
35
+ xml.should describe_eeml_for_version("5")
36
+ xml.xpath("//xmlns:environment").should_not be_empty
37
+ xml.xpath("//xmlns:environment").each do |feed_xml|
38
+ feed_xml.should contain_feed_eeml_for_version("5")
39
+ end
40
+ end
41
+ end
42
+
43
+ end
44
+
@@ -0,0 +1,117 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cosm::Trigger do
4
+
5
+ it "should have a constant that defines the allowed keys" do
6
+ Cosm::Trigger::ALLOWED_KEYS.should == %w(threshold_value user notified_at url trigger_type id environment_id stream_id)
7
+ end
8
+
9
+ describe "validation" do
10
+ before(:each) do
11
+ @trigger = Cosm::Trigger.new
12
+ end
13
+
14
+ %w(url stream_id environment_id user).each do |field|
15
+ it "should require a '#{field}'" do
16
+ @trigger.send("#{field}=".to_sym, nil)
17
+ @trigger.should_not be_valid
18
+ @trigger.errors[field.to_sym].should include("can't be blank")
19
+ end
20
+ end
21
+ end
22
+
23
+ describe "#initialize" do
24
+ it "should create a blank slate when passed no arguments" do
25
+ trigger = Cosm::Trigger.new
26
+ Cosm::Trigger::ALLOWED_KEYS.each do |attr|
27
+ trigger.attributes[attr.to_sym].should be_nil
28
+ end
29
+ end
30
+
31
+ it "should accept xml" do
32
+ trigger = Cosm::Trigger.new(trigger_as_(:xml))
33
+ trigger.url.should == "http://www.postbin.org/zc9sca"
34
+ end
35
+
36
+ it "should accept json" do
37
+ trigger = Cosm::Trigger.new(trigger_as_(:json))
38
+ trigger.url.should == "http://www.postbin.org/zc9sca"
39
+ end
40
+
41
+ it "should accept a hash of attributes" do
42
+ trigger = Cosm::Trigger.new(trigger_as_(:hash))
43
+ trigger.url.should == "http://www.postbin.org/zc9sca"
44
+ end
45
+ end
46
+
47
+ describe "#attributes" do
48
+ it "should return a hash of datapoint properties" do
49
+ attrs = {}
50
+ Cosm::Trigger::ALLOWED_KEYS.each do |key|
51
+ attrs[key] = "key #{rand(1000)}"
52
+ end
53
+ trigger = Cosm::Trigger.new(attrs)
54
+
55
+ trigger.attributes.should == attrs
56
+ end
57
+
58
+ it "should not return nil values" do
59
+ attrs = {}
60
+ Cosm::Trigger::ALLOWED_KEYS.each do |key|
61
+ attrs[key] = "key #{rand(1000)}"
62
+ end
63
+ attrs["notified_at"] = nil
64
+ trigger = Cosm::Trigger.new(attrs)
65
+
66
+ trigger.attributes.should_not include("notified_at")
67
+ end
68
+ end
69
+
70
+ describe "#attributes=" do
71
+ it "should accept and save a hash of datapoint properties" do
72
+ trigger = Cosm::Trigger.new({})
73
+
74
+ attrs = {}
75
+ Cosm::Trigger::ALLOWED_KEYS.each do |key|
76
+ value = "key #{rand(1000)}"
77
+ attrs[key] = value
78
+ trigger.should_receive("#{key}=").with(value)
79
+ end
80
+ trigger.attributes=(attrs)
81
+ end
82
+ end
83
+
84
+ describe "#as_json" do
85
+ it "should call the json generator" do
86
+ trigger = Cosm::Trigger.new({})
87
+ trigger.should_receive(:generate_json).and_return({"title" => "Environment"})
88
+ trigger.as_json.should == {"title" => "Environment"}
89
+ end
90
+ end
91
+
92
+ describe "#to_json" do
93
+ it "should call #as_json" do
94
+ trigger_hash = {"title" => "Environment"}
95
+ trigger = Cosm::Trigger.new(trigger_hash)
96
+ trigger.should_receive(:as_json).with({})
97
+ trigger.to_json
98
+ end
99
+
100
+ it "should pass options through to #as_json" do
101
+ trigger_hash = {"title" => "Environment"}
102
+ trigger = Cosm::Trigger.new(trigger_hash)
103
+ trigger.should_receive(:as_json).with({:crazy => "options"})
104
+ trigger.to_json({:crazy => "options"})
105
+ end
106
+
107
+ it "should pass the output of #as_json to yajl" do
108
+ trigger_hash = {"title" => "Environment"}
109
+ trigger = Cosm::Trigger.new(trigger_hash)
110
+ trigger.should_receive(:as_json).and_return({:awesome => "hash"})
111
+ ::JSON.should_receive(:generate).with({:awesome => "hash"})
112
+ trigger.to_json
113
+ end
114
+ end
115
+
116
+ end
117
+
@@ -0,0 +1,81 @@
1
+ class Feed
2
+ extend Cosm::Base
3
+
4
+ is_cosm :feed
5
+ attr_accessor :datastreams
6
+ attr_accessor :feed
7
+ attr_accessor :creator
8
+ attr_accessor :title
9
+ attr_accessor :website
10
+ attr_accessor :icon
11
+ attr_accessor :description
12
+ attr_accessor :updated
13
+ attr_accessor :email
14
+ attr_accessor :private
15
+ attr_accessor :tags
16
+ attr_accessor :location_disposition
17
+ attr_accessor :location_domain
18
+ attr_accessor :location_ele
19
+ attr_accessor :location_exposure
20
+ attr_accessor :location_lat
21
+ attr_accessor :location_lon
22
+ attr_accessor :location_name
23
+
24
+ def attributes
25
+ attributes = {}
26
+ Cosm::Feed::ALLOWED_KEYS.each do |key|
27
+ attributes[key] = self.send(key) if self.respond_to?(key)
28
+ end
29
+ attributes
30
+ end
31
+ end
32
+
33
+ class Datastream
34
+ extend Cosm::Base
35
+
36
+ is_cosm :datastream, {:id => :stream_id}
37
+
38
+ attr_accessor :feed
39
+ attr_accessor :feed_id
40
+ attr_accessor :datapoints
41
+ attr_accessor :id
42
+ attr_accessor :stream_id
43
+ attr_accessor :feed_creator
44
+ attr_accessor :current_value
45
+ attr_accessor :min_value
46
+ attr_accessor :max_value
47
+ attr_accessor :unit_label
48
+ attr_accessor :unit_type
49
+ attr_accessor :unit_symbol
50
+ attr_accessor :tags
51
+ attr_accessor :updated
52
+
53
+ def attributes
54
+ attributes = {}
55
+ Cosm::Datastream::ALLOWED_KEYS.each do |key|
56
+ attributes[key] = self.send(key) if self.respond_to?(key)
57
+ end
58
+ attributes
59
+ end
60
+
61
+ end
62
+
63
+ class Datapoint
64
+ extend Cosm::Base
65
+
66
+ is_cosm :datapoint
67
+ attr_accessor :datastream_id
68
+ attr_accessor :at
69
+ attr_accessor :value
70
+
71
+ def attributes
72
+ attributes = {}
73
+ Cosm::Datapoint::ALLOWED_KEYS.each do |key|
74
+ attributes[key] = self.send(key) if self.respond_to?(key)
75
+ end
76
+ attributes
77
+ end
78
+
79
+
80
+ end
81
+
@@ -0,0 +1,17 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'rspec'
5
+
6
+ require 'time'
7
+
8
+ Dir['./spec/support/**/*.rb'].map {|f| require f}
9
+
10
+ $:.push File.expand_path("../lib", __FILE__)
11
+ require 'cosm-rb'
12
+
13
+ require File.dirname(__FILE__) + '/fixtures/models.rb'
14
+
15
+ RSpec.configure do |c|
16
+ c.run_all_when_everything_filtered = true
17
+ end
@@ -0,0 +1,16 @@
1
+ RSpec::Matchers.define :contain_datapoint_eeml_for_version do |eeml_version|
2
+ match do |xml|
3
+ hash = datapoint_as_(:hash)
4
+ environment = xml.at_xpath("//xmlns:environment")
5
+ datapoint = environment.at_xpath("//xmlns:data").at_xpath("xmlns:datapoints")
6
+ value = datapoint.at_xpath("xmlns:value")
7
+ value.content.should == hash["value"]
8
+ value.attributes["at"].value.should == hash["at"].iso8601(6)
9
+ end
10
+
11
+ failure_message_for_should do |xml|
12
+ "expected #{xml} to describe eeml version #{eeml_version}"
13
+ end
14
+ end
15
+
16
+
@@ -0,0 +1,60 @@
1
+ RSpec::Matchers.define :contain_datastream_eeml_for_version do |eeml_version|
2
+ match do |xml|
3
+ hash = datastream_as_(:hash)
4
+ case eeml_version
5
+ when "0.5.1"
6
+ environment = xml.at_xpath("//xmlns:environment")
7
+ environment.attributes["updated"].value.should == hash["updated"].iso8601(6)
8
+ environment.attributes["id"].value.should == hash["feed_id"]
9
+ environment.attributes["creator"].value.should == hash["feed_creator"]
10
+ datastream = environment.at_xpath("//xmlns:data")
11
+ datastream.attributes["id"].value.should == hash["id"]
12
+ tags = datastream.xpath("//xmlns:tag")
13
+ hash["tags"].split(',').map(&:strip).sort{|a,b| a.downcase <=> b.downcase}.each_with_index do |tag, index|
14
+ tags[index].content.should == tag
15
+ end
16
+ current_value = datastream.at_xpath("//xmlns:current_value")
17
+ current_value.content.should == hash["current_value"]
18
+ current_value.attributes["at"].value.should == hash["updated"].iso8601(6)
19
+ datastream.at_xpath("//xmlns:max_value").content.should == hash["max_value"].to_s
20
+ datastream.at_xpath("//xmlns:min_value").content.should == hash["min_value"].to_s
21
+ unit = datastream.at_xpath("//xmlns:unit")
22
+ unit.content.should == hash["unit_label"]
23
+ unit.attributes["type"].value.should == hash["unit_type"]
24
+ unit.attributes["symbol"].value.should == hash["unit_symbol"]
25
+ datapoints = datastream.at_xpath("xmlns:datapoints")
26
+ datapoints.xpath("xmlns:value").each do |datapoint|
27
+ dp = hash["datapoints"].detect{|dp| dp["at"].iso8601(6) == datapoint.attributes["at"].value}
28
+ datapoint.content.should == dp["value"]
29
+ end
30
+ when "5"
31
+ environment = xml.at_xpath("//xmlns:environment")
32
+ environment.attributes["updated"].value.should == hash["updated"].iso8601
33
+ environment.attributes["id"].value.should == hash["feed_id"]
34
+ environment.attributes["creator"].value.should == "http://www.haque.co.uk"
35
+ datastream = environment.at_xpath("//xmlns:data")
36
+ datastream.attributes["id"].value.should == hash["id"]
37
+ tags = datastream.xpath("//xmlns:tag")
38
+ hash["tags"].split(',').map(&:strip).sort{|a,b| a.downcase <=> b.downcase}.each_with_index do |tag, index|
39
+ tags[index].content.should == tag
40
+ end
41
+ current_value = datastream.at_xpath("//xmlns:value")
42
+ current_value.content.should == hash["current_value"]
43
+ current_value.attributes["minValue"].value.should == hash["min_value"].to_s
44
+ current_value.attributes["maxValue"].value.should == hash["max_value"].to_s
45
+ unit = datastream.at_xpath("//xmlns:unit")
46
+ unit.content.should == hash["unit_label"]
47
+ unit.attributes["type"].value.should == hash["unit_type"]
48
+ unit.attributes["symbol"].value.should == hash["unit_symbol"]
49
+
50
+ else
51
+ false
52
+ end
53
+ end
54
+
55
+ failure_message_for_should do |xml|
56
+ "expected #{xml} to describe eeml version #{eeml_version}"
57
+ end
58
+ end
59
+
60
+
@@ -0,0 +1,96 @@
1
+ RSpec::Matchers.define :contain_feed_eeml_for_version do |eeml_version|
2
+ match do |xml|
3
+ hash = feed_as_(:hash)
4
+ case eeml_version
5
+ when "0.5.1"
6
+ environment = xml.at_xpath("//xmlns:environment")
7
+ environment.attributes["updated"].value.should == hash["updated"].iso8601(6)
8
+ environment.attributes["created"].value.should == hash["created"].iso8601(6)
9
+ environment.attributes["id"].value.should == hash["id"].to_s
10
+ environment.attributes["creator"].value.should == hash["creator"]
11
+ environment.at_xpath("//xmlns:title").content.should == hash["title"]
12
+ environment.at_xpath("//xmlns:feed").content.should == "#{hash["feed"]}.xml"
13
+ environment.at_xpath("//xmlns:status").content.should == hash["status"]
14
+ environment.at_xpath("//xmlns:private").content.should == hash["private"].to_s
15
+ environment.at_xpath("//xmlns:description").content.should == hash["description"]
16
+ environment.at_xpath("//xmlns:icon").content.should == hash["icon"]
17
+ environment.at_xpath("//xmlns:website").content.should == hash["website"]
18
+ environment.at_xpath("//xmlns:email").content.should == hash["email"]
19
+ tags = environment.xpath("//xmlns:tag")
20
+ hash["tags"].split(',').map(&:strip).sort{|a,b| a.downcase <=> b.downcase}.each_with_index do |tag, index|
21
+ tags[index].content.should == tag
22
+ end
23
+ location = environment.at_xpath("//xmlns:location")
24
+ location.attributes["domain"].value.should == hash["location_domain"]
25
+ location.attributes["exposure"].value.should == hash["location_exposure"]
26
+ location.attributes["disposition"].value.should == hash["location_disposition"]
27
+ location.at_xpath("//xmlns:name").content.should == hash["location_name"]
28
+ location.at_xpath("//xmlns:lat").content.should == hash["location_lat"].to_s
29
+ location.at_xpath("//xmlns:lon").content.should == hash["location_lon"].to_s
30
+ location.at_xpath("//xmlns:ele").content.should == hash["location_ele"]
31
+ hash["datastreams"].each do |ds_hash|
32
+ datastream = environment.at_xpath("//xmlns:data[@id=#{ds_hash["id"]}]")
33
+ tags = datastream.xpath("xmlns:tag")
34
+ ds_hash["tags"].split(',').map(&:strip).sort{|a,b| a.downcase <=> b.downcase}.each_with_index do |tag, index|
35
+ tags[index].content.should == tag
36
+ end if ds_hash["tags"]
37
+ current_value = datastream.at_xpath("xmlns:current_value")
38
+ current_value.content.should == ds_hash["current_value"]
39
+ current_value.attributes["at"].value.should == ds_hash["updated"].iso8601(6)
40
+ datastream.at_xpath("xmlns:max_value").content.should == ds_hash["max_value"].to_s
41
+ datastream.at_xpath("xmlns:min_value").content.should == ds_hash["min_value"].to_s
42
+ unit = datastream.at_xpath("xmlns:unit")
43
+ unit.content.should == ds_hash["unit_label"] unless ds_hash["unit_label"].empty?
44
+ unit.attributes["type"].value.should == ds_hash["unit_type"] unless ds_hash["unit_type"].empty?
45
+ unit.attributes["symbol"].value.should == ds_hash["unit_symbol"] unless ds_hash["unit_symbol"].empty?
46
+ ds_hash["datapoints"].each do |dp_hash|
47
+ datapoint = datastream.at_xpath("xmlns:datapoints").at_xpath("xmlns:value[@at=\"#{dp_hash["at"].iso8601(6)}\"]")
48
+ datapoint.content.should == dp_hash["value"]
49
+ end if ds_hash["datapoints"]
50
+ end
51
+ when "5"
52
+ environment = xml.at_xpath("//xmlns:environment")
53
+ environment.attributes["updated"].value.should == hash["updated"].iso8601
54
+ environment.attributes["id"].value.should == hash["id"].to_s
55
+ environment.attributes["creator"].value.should == "http://www.haque.co.uk"
56
+ environment.at_xpath("//xmlns:title").content.should == hash["title"]
57
+ environment.at_xpath("//xmlns:feed").content.should == "#{hash["feed"]}.xml"
58
+ environment.at_xpath("//xmlns:status").content.should == hash["status"]
59
+ environment.at_xpath("//xmlns:description").content.should == hash["description"]
60
+ environment.at_xpath("//xmlns:icon").content.should == hash["icon"]
61
+ environment.at_xpath("//xmlns:website").content.should == hash["website"]
62
+ environment.at_xpath("//xmlns:email").content.should == hash["email"]
63
+ location = environment.at_xpath("//xmlns:location")
64
+ location.attributes["domain"].value.should == hash["location_domain"]
65
+ location.attributes["exposure"].value.should == hash["location_exposure"]
66
+ location.attributes["disposition"].value.should == hash["location_disposition"]
67
+ location.at_xpath("//xmlns:name").content.should == hash["location_name"]
68
+ location.at_xpath("//xmlns:lat").content.should == hash["location_lat"].to_s
69
+ location.at_xpath("//xmlns:lon").content.should == hash["location_lon"].to_s
70
+ location.at_xpath("//xmlns:ele").content.should == hash["location_ele"]
71
+ hash["datastreams"].each do |ds_hash|
72
+ datastream = environment.at_xpath("//xmlns:data[@id=#{ds_hash["id"]}]")
73
+ tags = datastream.xpath("xmlns:tag")
74
+ ds_hash["tags"].split(',').map(&:strip).sort{|a,b| a.downcase <=> b.downcase}.each_with_index do |tag, index|
75
+ tags[index].content.should == tag
76
+ end if ds_hash["tags"]
77
+ current_value = datastream.at_xpath("xmlns:value")
78
+ current_value.content.should == ds_hash["current_value"]
79
+ current_value.attributes["minValue"].value.should == ds_hash["min_value"].to_s
80
+ current_value.attributes["maxValue"].value.should == ds_hash["max_value"].to_s
81
+ unit = datastream.at_xpath("xmlns:unit")
82
+ unit.content.should == ds_hash["unit_label"] unless ds_hash["unit_label"].empty?
83
+ unit.attributes["type"].value.should == ds_hash["unit_type"] unless ds_hash["unit_type"].empty?
84
+ unit.attributes["symbol"].value.should == ds_hash["unit_symbol"] unless ds_hash["unit_symbol"].empty?
85
+ end
86
+ else
87
+ false
88
+ end
89
+ end
90
+
91
+ failure_message_for_should do |xml|
92
+ "expected #{xml} to contain eeml version #{eeml_version}"
93
+ end
94
+ end
95
+
96
+