xively-rb 0.2.09

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 (124) hide show
  1. data/.gitignore +13 -0
  2. data/.rbenv-version +1 -0
  3. data/.rspec +2 -0
  4. data/.rvmrc +1 -0
  5. data/.travis.yml +7 -0
  6. data/CHANGELOG.md +91 -0
  7. data/CONTRIBUTING.md +95 -0
  8. data/Gemfile +15 -0
  9. data/LICENSE.md +13 -0
  10. data/README.md +10 -0
  11. data/Rakefile +25 -0
  12. data/ci/build_hudson.sh +24 -0
  13. data/init.rb +2 -0
  14. data/lib/xively-rb.rb +44 -0
  15. data/lib/xively-rb/array_extensions.rb +6 -0
  16. data/lib/xively-rb/base.rb +52 -0
  17. data/lib/xively-rb/base/instance_methods.rb +28 -0
  18. data/lib/xively-rb/client.rb +43 -0
  19. data/lib/xively-rb/datapoint.rb +72 -0
  20. data/lib/xively-rb/datastream.rb +127 -0
  21. data/lib/xively-rb/exceptions.rb +5 -0
  22. data/lib/xively-rb/feed.rb +109 -0
  23. data/lib/xively-rb/hash_extensions.rb +16 -0
  24. data/lib/xively-rb/helpers.rb +41 -0
  25. data/lib/xively-rb/key.rb +99 -0
  26. data/lib/xively-rb/nil_content.rb +15 -0
  27. data/lib/xively-rb/object_extensions.rb +6 -0
  28. data/lib/xively-rb/parsers/csv/datastream_defaults.rb +50 -0
  29. data/lib/xively-rb/parsers/csv/feed_defaults.rb +97 -0
  30. data/lib/xively-rb/parsers/defaults.rb +15 -0
  31. data/lib/xively-rb/parsers/json/datapoint_defaults.rb +16 -0
  32. data/lib/xively-rb/parsers/json/datastream_defaults.rb +58 -0
  33. data/lib/xively-rb/parsers/json/feed_defaults.rb +114 -0
  34. data/lib/xively-rb/parsers/json/key_defaults.rb +20 -0
  35. data/lib/xively-rb/parsers/json/search_result_defaults.rb +24 -0
  36. data/lib/xively-rb/parsers/json/trigger_defaults.rb +16 -0
  37. data/lib/xively-rb/parsers/xml/datapoint_defaults.rb +27 -0
  38. data/lib/xively-rb/parsers/xml/datastream_defaults.rb +53 -0
  39. data/lib/xively-rb/parsers/xml/errors.rb +7 -0
  40. data/lib/xively-rb/parsers/xml/feed_defaults.rb +83 -0
  41. data/lib/xively-rb/parsers/xml/helpers.rb +116 -0
  42. data/lib/xively-rb/parsers/xml/key_defaults.rb +46 -0
  43. data/lib/xively-rb/parsers/xml/trigger_defaults.rb +28 -0
  44. data/lib/xively-rb/permission.rb +67 -0
  45. data/lib/xively-rb/resource.rb +43 -0
  46. data/lib/xively-rb/search_result.rb +68 -0
  47. data/lib/xively-rb/string_extensions.rb +6 -0
  48. data/lib/xively-rb/templates/csv/datapoint_defaults.rb +22 -0
  49. data/lib/xively-rb/templates/csv/datastream_defaults.rb +43 -0
  50. data/lib/xively-rb/templates/csv/feed_defaults.rb +47 -0
  51. data/lib/xively-rb/templates/defaults.rb +14 -0
  52. data/lib/xively-rb/templates/json/datapoint_defaults.rb +15 -0
  53. data/lib/xively-rb/templates/json/datastream_defaults.rb +61 -0
  54. data/lib/xively-rb/templates/json/feed_defaults.rb +90 -0
  55. data/lib/xively-rb/templates/json/key_defaults.rb +39 -0
  56. data/lib/xively-rb/templates/json/search_result_defaults.rb +42 -0
  57. data/lib/xively-rb/templates/json/trigger_defaults.rb +21 -0
  58. data/lib/xively-rb/templates/xml/datapoint_defaults.rb +25 -0
  59. data/lib/xively-rb/templates/xml/datastream_defaults.rb +66 -0
  60. data/lib/xively-rb/templates/xml/feed_defaults.rb +112 -0
  61. data/lib/xively-rb/templates/xml/search_result_defaults.rb +118 -0
  62. data/lib/xively-rb/templates/xml_headers.rb +17 -0
  63. data/lib/xively-rb/trigger.rb +66 -0
  64. data/lib/xively-rb/validations.rb +9 -0
  65. data/lib/xively-rb/version.rb +3 -0
  66. data/spec/fixtures/models.rb +81 -0
  67. data/spec/spec_helper.rb +29 -0
  68. data/spec/support/contain_datapoint_eeml_matcher.rb +16 -0
  69. data/spec/support/contain_datastream_eeml_matcher.rb +60 -0
  70. data/spec/support/contain_feed_eeml_matcher.rb +98 -0
  71. data/spec/support/datapoint_helper.rb +53 -0
  72. data/spec/support/datastream_helper.rb +324 -0
  73. data/spec/support/describe_eeml_matcher.rb +23 -0
  74. data/spec/support/feed_helper.rb +783 -0
  75. data/spec/support/fully_represent_datapoint_matcher.rb +30 -0
  76. data/spec/support/fully_represent_datastream_matcher.rb +96 -0
  77. data/spec/support/fully_represent_feed_matcher.rb +234 -0
  78. data/spec/support/fully_represent_key_matcher.rb +74 -0
  79. data/spec/support/fully_represent_search_result_matcher.rb +67 -0
  80. data/spec/support/fully_represent_trigger_matcher.rb +29 -0
  81. data/spec/support/key_helper.rb +74 -0
  82. data/spec/support/search_result_helper.rb +252 -0
  83. data/spec/support/trigger_helper.rb +51 -0
  84. data/spec/xively-rb/array_extensions_spec.rb +9 -0
  85. data/spec/xively-rb/base/instance_methods_spec.rb +109 -0
  86. data/spec/xively-rb/base_spec.rb +56 -0
  87. data/spec/xively-rb/client_spec.rb +51 -0
  88. data/spec/xively-rb/datapoint_spec.rb +187 -0
  89. data/spec/xively-rb/datastream_spec.rb +344 -0
  90. data/spec/xively-rb/feed_spec.rb +341 -0
  91. data/spec/xively-rb/hash_extensions_spec.rb +20 -0
  92. data/spec/xively-rb/helpers_spec.rb +56 -0
  93. data/spec/xively-rb/key_spec.rb +198 -0
  94. data/spec/xively-rb/parsers/csv/datastream_defaults_spec.rb +110 -0
  95. data/spec/xively-rb/parsers/csv/feed_defaults_spec.rb +234 -0
  96. data/spec/xively-rb/parsers/json/datapoint_defaults_spec.rb +21 -0
  97. data/spec/xively-rb/parsers/json/datastream_defaults_spec.rb +105 -0
  98. data/spec/xively-rb/parsers/json/feed_defaults_spec.rb +45 -0
  99. data/spec/xively-rb/parsers/json/key_defaults_spec.rb +14 -0
  100. data/spec/xively-rb/parsers/json/search_result_defaults_spec.rb +18 -0
  101. data/spec/xively-rb/parsers/json/trigger_defaults_spec.rb +22 -0
  102. data/spec/xively-rb/parsers/xml/datapoint_defaults_spec.rb +19 -0
  103. data/spec/xively-rb/parsers/xml/datastream_defaults_spec.rb +148 -0
  104. data/spec/xively-rb/parsers/xml/feed_defaults_spec.rb +254 -0
  105. data/spec/xively-rb/parsers/xml/key_defaults_spec.rb +22 -0
  106. data/spec/xively-rb/parsers/xml/trigger_defaults_spec.rb +22 -0
  107. data/spec/xively-rb/search_result_spec.rb +257 -0
  108. data/spec/xively-rb/string_extensions_spec.rb +12 -0
  109. data/spec/xively-rb/templates/csv/datapoint_defaults_spec.rb +41 -0
  110. data/spec/xively-rb/templates/csv/datastream_defaults_spec.rb +131 -0
  111. data/spec/xively-rb/templates/csv/feed_defaults_spec.rb +78 -0
  112. data/spec/xively-rb/templates/json/datapoint_defaults_spec.rb +14 -0
  113. data/spec/xively-rb/templates/json/datastream_defaults_spec.rb +170 -0
  114. data/spec/xively-rb/templates/json/feed_defaults_spec.rb +399 -0
  115. data/spec/xively-rb/templates/json/key_defaults_spec.rb +29 -0
  116. data/spec/xively-rb/templates/json/search_result_defaults_spec.rb +37 -0
  117. data/spec/xively-rb/templates/json/trigger_defaults_spec.rb +19 -0
  118. data/spec/xively-rb/templates/xml/datapoint_defaults_spec.rb +14 -0
  119. data/spec/xively-rb/templates/xml/datastream_defaults_spec.rb +113 -0
  120. data/spec/xively-rb/templates/xml/feed_defaults_spec.rb +131 -0
  121. data/spec/xively-rb/templates/xml/search_result_defaults_spec.rb +44 -0
  122. data/spec/xively-rb/trigger_spec.rb +157 -0
  123. data/xively-rb.gemspec +41 -0
  124. metadata +333 -0
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe "default key json templates" do
4
+ before(:each) do
5
+ @key = Xively::Key.new(key_as_(:hash))
6
+ end
7
+
8
+ it "should represent Pachube JSON (used by API v2)" do
9
+ json = @key.generate_json
10
+ json[:key][:id].should == @key.id
11
+ json[:key][:expires_at].should == @key.expires_at
12
+ json[:key][:api_key].should == @key.key
13
+ json[:key][:user].should == @key.user
14
+ json[:key][:label].should == @key.label
15
+ json[:key][:private_access].should == @key.private_access
16
+ json[:key][:permissions].each_index do |permission_index|
17
+ json[:key][:permissions][permission_index][:access_methods].should == @key.permissions[permission_index].access_methods
18
+ json[:key][:permissions][permission_index][:label].should == @key.permissions[permission_index].label
19
+ json[:key][:permissions][permission_index][:referer].should == @key.permissions[permission_index].referer
20
+ json[:key][:permissions][permission_index][:source_ip].should == @key.permissions[permission_index].source_ip
21
+ json[:key][:permissions][permission_index][:minimum_interval].should == @key.permissions[permission_index].minimum_interval
22
+ json[:key][:permissions][permission_index][:resources].each_index do |res_index|
23
+ json[:key][:permissions][permission_index][:resources][res_index][:feed_id].should == @key.permissions[permission_index].resources[res_index].feed_id
24
+ json[:key][:permissions][permission_index][:resources][res_index][:datastream_id].should == @key.permissions[permission_index].resources[res_index].datastream_id
25
+ json[:key][:permissions][permission_index][:resources][res_index][:datastream_trigger_id].should == @key.permissions[permission_index].resources[res_index].datastream_trigger_id
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe "default search result json templates" do
4
+ before(:each) do
5
+ @search_result = Xively::SearchResult.new(search_result_as_(:hash))
6
+ end
7
+
8
+ context "1.0.0" do
9
+ it "should represent Pachube v2 json for search results" do
10
+ @search_result.results.each do |feed|
11
+ feed.should_receive(:generate_json).with("1.0.0").and_return("feed_100_json")
12
+ end
13
+ @search_result.generate_json("1.0.0").should == {
14
+ :totalResults => "10000",
15
+ :startIndex => "0",
16
+ :itemsPerPage => "100",
17
+ :results => ["feed_100_json"]
18
+ }
19
+ end
20
+ end
21
+
22
+ context "0.6-alpha" do
23
+ it "should represent Pachube v1 json for search results" do
24
+ @search_result.results.each do |feed|
25
+ feed.should_receive(:generate_json).with("0.6-alpha").and_return("feed_6_json")
26
+ end
27
+ @search_result.generate_json("0.6-alpha").should == {
28
+ :startIndex => "0",
29
+ :totalResults => "10000",
30
+ :itemsPerPage => "100",
31
+ :results => ["feed_6_json"]
32
+ }
33
+ end
34
+ end
35
+ end
36
+
37
+
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe "default feed json templates" do
4
+ before(:each) do
5
+ @trigger = Xively::Trigger.new(trigger_as_(:hash))
6
+ end
7
+
8
+ it "should represent Pachube JSON (used by API v2)" do
9
+ json = @trigger.generate_json
10
+ json[:id].should == @trigger.id
11
+ json[:threshold_value].should == @trigger.threshold_value
12
+ json[:notified_at].should == nil
13
+ json[:url].should == @trigger.url
14
+ json[:trigger_type].should == @trigger.trigger_type
15
+ json[:stream_id].should == @trigger.stream_id
16
+ json[:environment_id].should == @trigger.environment_id
17
+ json[:user].should == @trigger.user
18
+ end
19
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
3
+ describe "default datapoint xml templates" do
4
+ before(:each) do
5
+ @datapoint = Xively::Datapoint.new(datapoint_as_(:hash))
6
+ end
7
+
8
+ it "should represent Pachube datapoints" do
9
+ xml = Nokogiri.parse(@datapoint.generate_xml)
10
+ xml.should describe_eeml_for_version("0.5.1")
11
+ xml.should contain_datapoint_eeml_for_version("0.5.1")
12
+ end
13
+ end
14
+
@@ -0,0 +1,113 @@
1
+ require 'spec_helper'
2
+
3
+ describe "default datastream xml templates" do
4
+ before(:each) do
5
+ @datastream = Xively::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 = Xively::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 = Xively::Feed.new(feed_as_(:hash))
6
+ @search_result = Xively::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,157 @@
1
+ require 'spec_helper'
2
+
3
+ describe Xively::Trigger do
4
+
5
+ it "should have a constant that defines the allowed keys" do
6
+ Xively::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 = Xively::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 = Xively::Trigger.new
26
+ Xively::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 = Xively::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 = Xively::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 = Xively::Trigger.new(trigger_as_(:hash))
43
+ trigger.url.should == "http://www.postbin.org/zc9sca"
44
+ end
45
+
46
+ context "specifying format explicitly" do
47
+ it "should raise known exception if passed json but told xml" do
48
+ expect {
49
+ Xively::Trigger.new(trigger_as_(:json), :xml)
50
+ }.to raise_error(Xively::Parsers::XML::InvalidXMLError)
51
+ end
52
+
53
+ it "should raise known exception if passed xml but told json" do
54
+ expect {
55
+ Xively::Trigger.new(trigger_as_(:json), :xml)
56
+ }.to raise_error(Xively::Parsers::XML::InvalidXMLError)
57
+ end
58
+
59
+ it "should raise known exception if passed unknown format" do
60
+ expect {
61
+ Xively::Trigger.new(trigger_as_(:json), :png)
62
+ }.to raise_error(Xively::InvalidFormatError)
63
+ end
64
+ end
65
+
66
+ context "specifying format" do
67
+ it "should raise known exception if told xml but given json" do
68
+ expect {
69
+ Xively::Trigger.new(trigger_as_(:json), :xml)
70
+ }.to raise_error(Xively::Parsers::XML::InvalidXMLError)
71
+ end
72
+
73
+ it "should raise known exception if told json but given xml" do
74
+ expect {
75
+ Xively::Trigger.new(trigger_as_(:xml), :json)
76
+ }.to raise_error(Xively::Parsers::JSON::InvalidJSONError)
77
+ end
78
+
79
+ it "should raise known exception if given unknown format" do
80
+ expect {
81
+ Xively::Trigger.new(trigger_as_(:xml), :gif)
82
+ }.to raise_error(Xively::InvalidFormatError)
83
+ end
84
+ end
85
+ end
86
+
87
+ describe "#attributes" do
88
+ it "should return a hash of datapoint properties" do
89
+ attrs = {}
90
+ Xively::Trigger::ALLOWED_KEYS.each do |key|
91
+ attrs[key] = "key #{rand(1000)}"
92
+ end
93
+ trigger = Xively::Trigger.new(attrs)
94
+
95
+ trigger.attributes.should == attrs
96
+ end
97
+
98
+ it "should not return nil values" do
99
+ attrs = {}
100
+ Xively::Trigger::ALLOWED_KEYS.each do |key|
101
+ attrs[key] = "key #{rand(1000)}"
102
+ end
103
+ attrs["notified_at"] = nil
104
+ trigger = Xively::Trigger.new(attrs)
105
+
106
+ trigger.attributes.should_not include("notified_at")
107
+ end
108
+ end
109
+
110
+ describe "#attributes=" do
111
+ it "should accept and save a hash of datapoint properties" do
112
+ trigger = Xively::Trigger.new({})
113
+
114
+ attrs = {}
115
+ Xively::Trigger::ALLOWED_KEYS.each do |key|
116
+ value = "key #{rand(1000)}"
117
+ attrs[key] = value
118
+ trigger.should_receive("#{key}=").with(value)
119
+ end
120
+ trigger.attributes=(attrs)
121
+ end
122
+ end
123
+
124
+ describe "#as_json" do
125
+ it "should call the json generator" do
126
+ trigger = Xively::Trigger.new({})
127
+ trigger.should_receive(:generate_json).and_return({"title" => "Environment"})
128
+ trigger.as_json.should == {"title" => "Environment"}
129
+ end
130
+ end
131
+
132
+ describe "#to_json" do
133
+ it "should call #as_json" do
134
+ trigger_hash = {"title" => "Environment"}
135
+ trigger = Xively::Trigger.new(trigger_hash)
136
+ trigger.should_receive(:as_json).with({})
137
+ trigger.to_json
138
+ end
139
+
140
+ it "should pass options through to #as_json" do
141
+ trigger_hash = {"title" => "Environment"}
142
+ trigger = Xively::Trigger.new(trigger_hash)
143
+ trigger.should_receive(:as_json).with({:crazy => "options"})
144
+ trigger.to_json({:crazy => "options"})
145
+ end
146
+
147
+ it "should pass the output of #as_json to yajl" do
148
+ trigger_hash = {"title" => "Environment"}
149
+ trigger = Xively::Trigger.new(trigger_hash)
150
+ trigger.should_receive(:as_json).and_return({:awesome => "hash"})
151
+ MultiJson.should_receive(:dump).with({:awesome => "hash"})
152
+ trigger.to_json
153
+ end
154
+ end
155
+
156
+ end
157
+