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,78 @@
1
+ require 'spec_helper'
2
+
3
+ describe "default feed json templates" do
4
+ before(:each) do
5
+ @feed = Xively::Feed.new(feed_as_(:hash))
6
+ @feed.datastreams.each do |ds|
7
+ ds.current_value = "I \n , will , break , you"
8
+ ds.datapoints.each do |dp|
9
+ dp.value = "one,data,point"
10
+ end
11
+ end
12
+ end
13
+
14
+ context "2 (used by API v2)" do
15
+ it "should be the default" do
16
+ @feed.generate_csv("2").should == @feed.to_csv
17
+ end
18
+
19
+ it "should represent Pachube JSON" do
20
+ @feed.datastreams.each do |ds|
21
+ ds.datapoints = []
22
+ end
23
+ csv = @feed.generate_csv("2")
24
+ expected_csv = @feed.datastreams.collect do |datastream|
25
+ Xively::CSV.generate_line([datastream.id, datastream.updated.iso8601(6), datastream.current_value]).strip
26
+ end.join("\n")
27
+ csv.should == expected_csv
28
+ end
29
+
30
+ it "should allow representing full Pachube JSON" do
31
+ @feed.datastreams.each do |ds|
32
+ ds.datapoints = []
33
+ end
34
+ csv = @feed.generate_csv("2", :full => true)
35
+ expected_csv = @feed.datastreams.collect do |datastream|
36
+ Xively::CSV.generate_line([@feed.id, datastream.id, datastream.updated.iso8601(6), datastream.current_value]).strip
37
+ end.join("\n")
38
+ csv.should == expected_csv
39
+ end
40
+
41
+ it "should represent Pachube JSON with datapoints" do
42
+ csv = @feed.generate_csv("2")
43
+ expected_csv = @feed.datastreams.collect do |datastream|
44
+ if datastream.datapoints.any?
45
+ datastream.datapoints.collect {|dp| Xively::CSV.generate_line([datastream.id, dp.at.iso8601(6), dp.value]).strip }
46
+ else
47
+ Xively::CSV.generate_line([datastream.id, datastream.updated.iso8601(6), datastream.current_value]).strip
48
+ end
49
+ end.join("\n")
50
+ csv.should == expected_csv
51
+ end
52
+
53
+ it "should allow representing full Pachube JSON with datapoints" do
54
+ csv = @feed.generate_csv("2", :full => true)
55
+ expected_csv = @feed.datastreams.collect do |datastream|
56
+ if datastream.datapoints.any?
57
+ datastream.datapoints.collect {|dp| Xively::CSV.generate_line([@feed.id, datastream.id, dp.at.iso8601(6), dp.value]).strip }
58
+ else
59
+ Xively::CSV.generate_line([@feed.id, datastream.id, datastream.updated.iso8601(6), datastream.current_value]).strip
60
+ end
61
+ end.join("\n")
62
+ csv.should == expected_csv
63
+ end
64
+ end
65
+
66
+ context "0.6-alpha (used by API v1)" do
67
+
68
+ it "should represent Pachube JSON" do
69
+ csv = @feed.generate_csv("1")
70
+ expected_csv = @feed.datastreams.collect do |datastream|
71
+ Xively::CSV.generate_line([datastream.current_value]).strip
72
+ end.join(",")
73
+ csv.should == expected_csv
74
+ end
75
+
76
+ end
77
+ end
78
+
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
3
+ describe "default datapoint json templates" do
4
+ before(:each) do
5
+ @datapoint = Xively::Datapoint.new(datapoint_as_(:hash))
6
+ end
7
+
8
+ it "should represent Pachube datapoints (only used by API v2)" do
9
+ json = @datapoint.generate_json
10
+ json[:value].should == @datapoint.value
11
+ json[:at].should == @datapoint.at.iso8601(6)
12
+ end
13
+ end
14
+
@@ -0,0 +1,170 @@
1
+ require 'spec_helper'
2
+
3
+ describe "default datastream json templates" do
4
+ before(:each) do
5
+ @datastream = Xively::Datastream.new(datastream_as_(:hash))
6
+ end
7
+
8
+ context "1.0.0 (used by Pachube API V2)" do
9
+ it "should be the default" do
10
+ @datastream.generate_json("1.0.0").should == @datastream.as_json
11
+ end
12
+
13
+ it "should represent Pachube JSON 1.0.0 (used by API v2)" do
14
+ json = @datastream.generate_json("1.0.0")
15
+ json[:id].should == @datastream.id
16
+ json[:version].should == "1.0.0"
17
+ json[:at].should == @datastream.updated.iso8601(6)
18
+ json[:current_value].should == @datastream.current_value
19
+ json[:max_value].should == @datastream.max_value.to_s
20
+ json[:min_value].should == @datastream.min_value.to_s
21
+ json[:tags].should == @datastream.tags.split(',').map(&:strip).sort{|a,b| a.downcase <=> b.downcase}
22
+ json[:unit].should == {
23
+ :type => @datastream.unit_type,
24
+ :symbol => @datastream.unit_symbol,
25
+ :label => @datastream.unit_label
26
+ }
27
+ json[:datapoints].should_not be_nil
28
+ json[:datapoints].each do |datapoint|
29
+ @datastream.datapoints.detect {|dp| dp.at == datapoint["at"]}.value.should == datapoint["value"]
30
+ end
31
+ end
32
+
33
+ it "should ignore tags if blank" do
34
+ @datastream.tags = []
35
+ json = @datastream.generate_json("1.0.0")
36
+ json[:tags].should be_nil
37
+ end
38
+
39
+ it "should ignore nil unit elements" do
40
+ @datastream.unit_symbol = nil
41
+ json = @datastream.generate_json("1.0.0")
42
+ json[:unit][:symbol].should be_nil
43
+ end
44
+
45
+ it "should ignore unit if none of the elements are set" do
46
+ @datastream.unit_label = nil
47
+ @datastream.unit_symbol = nil
48
+ @datastream.unit_type = nil
49
+ json = @datastream.generate_json("1.0.0")
50
+ json[:unit].should be_nil
51
+ end
52
+
53
+ it "should ignore unit if all the elements are blank" do
54
+ @datastream.unit_label = ''
55
+ @datastream.unit_symbol = ''
56
+ @datastream.unit_type = ''
57
+ json = @datastream.generate_json("1.0.0")
58
+ json[:unit].should be_nil
59
+ end
60
+
61
+ it "should ignore datapoints_function if it is not set" do
62
+ @datastream.datapoints_function = nil
63
+ json = @datastream.generate_json("1.0.0")
64
+ json[:datapoints_function].should be_nil
65
+ end
66
+
67
+ it "should use datapoints_function if it is set" do
68
+ @datastream.datapoints_function = 'average'
69
+ json = @datastream.generate_json("1.0.0")
70
+ json[:datapoints_function].should == 'average'
71
+ end
72
+
73
+ it "should ignore tags if nil" do
74
+ @datastream.tags = nil
75
+ json = @datastream.generate_json("1.0.0")
76
+ json[:tags].should be_nil
77
+ end
78
+
79
+ it "should ignore datapoints if empty" do
80
+ @datastream.datapoints = []
81
+ json = @datastream.generate_json("1.0.0")
82
+ json[:datapoints].should be_nil
83
+ end
84
+
85
+ it "should ignore unit if none of the elements are set" do
86
+ @datastream.unit_label = nil
87
+ @datastream.unit_symbol = nil
88
+ @datastream.unit_type = nil
89
+ json = @datastream.generate_json("1.0.0")
90
+ json[:unit].should be_nil
91
+ end
92
+
93
+ it "should include empty elements if we pass :include_blank" do
94
+ @datastream.unit_label = ''
95
+ @datastream.unit_symbol = ''
96
+ json = @datastream.generate_json("1.0.0", :include_blank => true)
97
+ json[:unit][:label].should == ''
98
+ json[:unit][:symbol].should == ''
99
+ end
100
+ end
101
+
102
+ context "0.6-alpha (used by API V1)" do
103
+ it "should represent Pachube JSON 0.6-alpha (used by API v1)" do
104
+ json = @datastream.generate_json("0.6-alpha")
105
+ json[:id].should == @datastream.id
106
+ json[:version].should == "0.6-alpha"
107
+ json[:values].first[:recorded_at].should == @datastream.updated.iso8601
108
+ json[:values].first[:value].should == @datastream.current_value
109
+ json[:values].first[:max_value].should == @datastream.max_value.to_s
110
+ json[:values].first[:min_value].should == @datastream.min_value.to_s
111
+ json[:tags].should == @datastream.tags.split(',').map(&:strip).sort{|a,b| a.downcase <=> b.downcase}
112
+ json[:unit].should == {
113
+ :type => @datastream.unit_type,
114
+ :symbol => @datastream.unit_symbol,
115
+ :label => @datastream.unit_label
116
+ }
117
+ end
118
+
119
+ it "should ignore unit if none of the elements are set" do
120
+ @datastream.unit_label = nil
121
+ @datastream.unit_symbol = nil
122
+ @datastream.unit_type = nil
123
+ json = @datastream.generate_json("0.6-alpha")
124
+ json[:unit].should be_nil
125
+ end
126
+
127
+ it "should ignore tags if nil" do
128
+ @datastream.tags = nil
129
+ json = @datastream.generate_json("0.6-alpha")
130
+ json[:tags].should be_nil
131
+ end
132
+
133
+ it "should ignore unit if all the elements are blank" do
134
+ @datastream.unit_label = ''
135
+ @datastream.unit_symbol = ''
136
+ @datastream.unit_type = ''
137
+ json = @datastream.generate_json("0.6-alpha")
138
+ json[:unit].should be_nil
139
+ end
140
+
141
+ it "should ignore unit if none of the elements are set" do
142
+ @datastream.unit_label = nil
143
+ @datastream.unit_symbol = nil
144
+ @datastream.unit_type = nil
145
+ json = @datastream.generate_json("0.6-alpha")
146
+ json[:unit].should be_nil
147
+ end
148
+
149
+ it "should ignore nil unit elements" do
150
+ @datastream.unit_symbol = nil
151
+ json = @datastream.generate_json("0.6-alpha")
152
+ json[:unit][:symbol].should be_nil
153
+ end
154
+
155
+ it "should ignore tags if blank" do
156
+ @datastream.tags = []
157
+ json = @datastream.generate_json("0.6-alpha")
158
+ json[:tags].should be_nil
159
+ end
160
+
161
+ it "should include empty elements if we pass :include_blank" do
162
+ @datastream.unit_label = ''
163
+ @datastream.unit_symbol = ''
164
+ json = @datastream.generate_json("0.6-alpha", :include_blank => true)
165
+ json[:unit][:label].should == ''
166
+ json[:unit][:symbol].should == ''
167
+ end
168
+ end
169
+ end
170
+
@@ -0,0 +1,399 @@
1
+ require 'spec_helper'
2
+
3
+ describe "default feed json templates" do
4
+ before(:each) do
5
+ @feed = Xively::Feed.new(feed_as_(:hash))
6
+ end
7
+
8
+ context "1.0.0" do
9
+ it "should be the default" do
10
+ @feed.generate_json("1.0.0").should == @feed.as_json
11
+ end
12
+
13
+ it "should represent Xively JSON (used by API v2)" do
14
+ json = @feed.generate_json("1.0.0")
15
+ json[:id].should == @feed.id
16
+ json[:version].should == "1.0.0"
17
+ json[:title].should == "Xively Office Environment"
18
+ json[:private].should == "false"
19
+ json[:icon].should == "http://xively.com/logo.png"
20
+ json[:website].should == "http://xively.com"
21
+ json[:tags].should == ["aardvark", "kittens", "sofa"]
22
+ json[:description].should == "Sensors in xively.com's headquarters."
23
+ json[:feed].should == "http://test.host/testfeed.html?random=890299&rand2=91.json"
24
+ json[:auto_feed_url].should == "http://test.host2/testfeed.xml?q=something"
25
+ json[:user][:login].should == "skeletor"
26
+ json[:status].should == "live"
27
+ json[:updated].should == "2011-01-02T00:00:00.000000+00:00"
28
+ json[:created].should == "2011-01-01T00:00:00.000000+00:00"
29
+ json[:email].should == "abc@example.com"
30
+ json[:creator].should == "http://xively.com/users/skeletor"
31
+ json[:product_id].should == "product_id"
32
+ json[:device_serial].should == "device_serial"
33
+ json[:location][:disposition].should == "fixed"
34
+ json[:location][:name].should == "office"
35
+ json[:location][:exposure].should == "indoor"
36
+ json[:location][:domain].should == "physical"
37
+ json[:location][:ele].should == "23.0"
38
+ json[:location][:lat].should == 51.5235375648154
39
+ json[:location][:lon].should == -0.0807666778564453
40
+ json[:datastreams].should have(7).things
41
+ json[:datastreams].each do |ds|
42
+ datastream = @feed.datastreams.detect{|stream| stream.id == ds[:id]}
43
+ ds[:at].should == datastream.updated.iso8601(6)
44
+ ds[:max_value].should == datastream.max_value.to_s
45
+ ds[:min_value].should == datastream.min_value.to_s
46
+ ds[:current_value].should == datastream.current_value
47
+ ds[:tags].should == datastream.tags.split(',').map(&:strip).sort{|a,b| a.downcase <=> b.downcase} if datastream.tags
48
+ ds[:unit].should == {
49
+ :label => datastream.unit_label,
50
+ :type => datastream.unit_type,
51
+ :symbol => datastream.unit_symbol
52
+ }.delete_if{ |k,v|v.nil? || v.blank? } if ds[:unit]
53
+ ds[:datapoints].each do |dp|
54
+ datapoint = datastream.datapoints.detect {|point| point.at.iso8601(6) == dp[:at]}
55
+ dp[:value].should == datapoint.value
56
+ end if ds[:datapoints]
57
+ end
58
+ end
59
+
60
+ it "should ignore datastream datapoints if empty" do
61
+ @feed.datastreams.each do |ds|
62
+ ds.datapoints = []
63
+ end
64
+ json = @feed.generate_json("1.0.0")
65
+ json[:datastreams].each do |ds|
66
+ ds[:datapoints].should be_nil
67
+ end
68
+ end
69
+
70
+ it "should ignore datastream tags if nil" do
71
+ @feed.datastreams.each do |ds|
72
+ ds.tags = nil
73
+ end
74
+ json = @feed.generate_json("1.0.0")
75
+ json[:datastreams].each do |ds|
76
+ datastream = @feed.datastreams.detect{|stream| stream.id == ds[:id]}
77
+ datastream.tags.should be_nil
78
+ end
79
+ end
80
+
81
+ it "should blank feed if blank" do
82
+ @feed.feed = ''
83
+ json = @feed.generate_json("1.0.0")
84
+ json[:feed].should be_blank
85
+ end
86
+
87
+ it "should blank feed if nil" do
88
+ @feed.feed = nil
89
+ json = @feed.generate_json("1.0.0")
90
+ json[:feed].should be_blank
91
+ end
92
+
93
+ it "should ignore updated if nil" do
94
+ @feed.updated = nil
95
+ json = @feed.generate_json("1.0.0")
96
+ json[:updated].should be_nil
97
+ end
98
+
99
+ it "should ignore updated if nil" do
100
+ @feed.created = nil
101
+ json = @feed.generate_json("1.0.0")
102
+ json[:created].should be_nil
103
+ end
104
+
105
+ it "should ignore tags if nil" do
106
+ @feed.tags = nil
107
+ json = @feed.generate_json("1.0.0")
108
+ json[:tags].should be_nil
109
+ end
110
+
111
+ it "should handle tags if an array" do
112
+ @feed.tags = %w(b a c)
113
+ json = @feed.generate_json("1.0.0")
114
+ json[:tags].should == %w(a b c)
115
+ end
116
+
117
+ it "should ignore location if all elements are nil" do
118
+ @feed.location_name = nil
119
+ @feed.location_disposition = nil
120
+ @feed.location_lat = nil
121
+ @feed.location_lon = nil
122
+ @feed.location_exposure = nil
123
+ @feed.location_ele = nil
124
+ @feed.location_domain = nil
125
+ json = @feed.generate_json("1.0.0")
126
+ json[:location].should be_nil
127
+ end
128
+
129
+ it "should ignore datastream units if blank" do
130
+ @feed.datastreams.each do |ds|
131
+ ds.unit_label = ''
132
+ ds.unit_symbol = ''
133
+ ds.unit_type = ''
134
+ end
135
+ json = @feed.generate_json("1.0.0")
136
+ json[:datastreams].each do |ds|
137
+ ds[:unit].should be_nil
138
+ end
139
+ end
140
+
141
+ it "should ignore datastream units if nil" do
142
+ @feed.datastreams.each do |ds|
143
+ ds.unit_label = nil
144
+ ds.unit_symbol = nil
145
+ ds.unit_type = nil
146
+ end
147
+ json = @feed.generate_json("1.0.0")
148
+ json[:datastreams].each do |ds|
149
+ ds[:unit].should be_nil
150
+ end
151
+ end
152
+
153
+ it "should ignore location if all elements are nil" do
154
+ @feed.location_name = nil
155
+ @feed.location_disposition = nil
156
+ @feed.location_lat = nil
157
+ @feed.location_lon = nil
158
+ @feed.location_exposure = nil
159
+ @feed.location_ele = nil
160
+ @feed.location_domain = nil
161
+ json = @feed.generate_json("1.0.0")
162
+ json[:location].should be_nil
163
+ end
164
+
165
+ it "should ignore blank location elements" do
166
+ @feed.location_disposition = ""
167
+ @feed.location_ele = ""
168
+ json = @feed.generate_json("1.0.0")
169
+ json[:location][:disposition].should be_nil
170
+ json[:location][:ele].should be_nil
171
+ end
172
+
173
+ it "should ignore nil location elements" do
174
+ @feed.location_disposition = nil
175
+ @feed.location_ele = nil
176
+ json = @feed.generate_json("1.0.0")
177
+ json[:location][:disposition].should be_nil
178
+ json[:location][:ele].should be_nil
179
+ end
180
+
181
+ it "should ignore location_waypoints if it is nil" do
182
+ @feed.location_disposition = 'mobile'
183
+ @feed.location_waypoints = nil
184
+ json = @feed.generate_json("1.0.0")
185
+ json[:location][:disposition].should == 'mobile'
186
+ json[:location][:waypoints].should be_nil
187
+ end
188
+
189
+ it "should use location_waypoints if it is set" do
190
+ @feed.location_disposition = 'mobile'
191
+ @feed.location_waypoints = [
192
+ {:at => Time.parse('2012-01-01 12:00:00'), :lat => 1.0, :lon => 1.1, :ele => "1.2"},
193
+ {:at => Time.parse('2012-01-01 13:00:00'), :lat => 2.0, :lon => 2.1, :ele => "2.2"}
194
+ ]
195
+ json = @feed.generate_json("1.0.0")
196
+ json[:location][:disposition].should == 'mobile'
197
+ json[:location][:waypoints].should == [
198
+ {:at => Time.parse('2012-01-01 12:00:00').iso8601(6), :lat => 1.0, :lon => 1.1, :ele => "1.2"},
199
+ {:at => Time.parse('2012-01-01 13:00:00').iso8601(6), :lat => 2.0, :lon => 2.1, :ele => "2.2"}
200
+ ]
201
+ end
202
+
203
+ it "should ignore datastream tags if blank" do
204
+ @feed.datastreams.each do |ds|
205
+ ds.tags = ""
206
+ end
207
+ json = @feed.generate_json("1.0.0")
208
+ json[:datastreams].each do |ds|
209
+ ds[:tags].should be_nil
210
+ end
211
+ end
212
+
213
+ it "should ignore datastream tags if nil" do
214
+ @feed.datastreams.each do |ds|
215
+ ds.tags = nil
216
+ end
217
+ json = @feed.generate_json("1.0.0")
218
+ json[:datastreams].each do |ds|
219
+ ds[:tags].should be_nil
220
+ end
221
+ end
222
+
223
+ it "should ignore tags if blank" do
224
+ @feed.tags = ""
225
+ json = @feed.generate_json("1.0.0")
226
+ json[:tags].should be_nil
227
+ end
228
+
229
+ it "should ignore tags if nil" do
230
+ @feed.tags = nil
231
+ json = @feed.generate_json("1.0.0")
232
+ json[:tags].should be_nil
233
+ end
234
+
235
+ it "should include empty stuff if we pass :include_blank" do
236
+ @feed.description = ''
237
+ @feed.location_ele = ''
238
+ json = @feed.generate_json("1.0.0", :include_blank => true)
239
+ json[:description].should == ''
240
+ json[:location][:ele].should == ''
241
+ end
242
+ end
243
+
244
+ context "0.6-alpha" do
245
+
246
+ it "should represent Xively JSON (used by API v1)" do
247
+ json = @feed.generate_json("0.6-alpha")
248
+ json[:id].should == @feed.id
249
+ json[:version].should == "0.6-alpha"
250
+ json[:title].should == "Xively Office Environment"
251
+ json[:private].should be_nil
252
+ json[:icon].should == "http://xively.com/logo.png"
253
+ json[:website].should == "http://xively.com"
254
+ json[:tags].should be_nil
255
+ json[:description].should == "Sensors in xively.com's headquarters."
256
+ json[:feed].should == "http://test.host/testfeed.html?random=890299&rand2=91.json"
257
+ json[:status].should == "live"
258
+ json[:updated].should == "2011-01-02T00:00:00.000000+00:00"
259
+ json[:email].should == "abc@example.com"
260
+ json[:location][:disposition].should == "fixed"
261
+ json[:location][:name].should == "office"
262
+ json[:location][:exposure].should == "indoor"
263
+ json[:location][:domain].should == "physical"
264
+ json[:location][:ele].should == "23.0"
265
+ json[:location][:lat].should == 51.5235375648154
266
+ json[:location][:lon].should == -0.0807666778564453
267
+ json[:datastreams].should have(7).things
268
+ json[:datastreams].each do |ds|
269
+ datastream = @feed.datastreams.detect{|stream| stream.id == ds[:id]}
270
+ ds[:values].first[:max_value].should == datastream.max_value.to_s
271
+ ds[:values].first[:min_value].should == datastream.min_value.to_s
272
+ ds[:values].first[:value].should == datastream.current_value
273
+ ds[:values].first[:recorded_at].should == datastream.updated.iso8601
274
+ ds[:tags].should == datastream.tags.split(',').map(&:strip).sort{|a,b| a.downcase <=> b.downcase} if datastream.tags
275
+ ds[:unit].should == {
276
+ :label => datastream.unit_label,
277
+ :type => datastream.unit_type,
278
+ :symbol => datastream.unit_symbol
279
+ }.delete_if{ |k,v|v.nil? || v.blank? } if ds[:unit]
280
+ end
281
+ end
282
+
283
+ it "should ignore datastream units if blank" do
284
+ @feed.datastreams.each do |ds|
285
+ ds.unit_label = ''
286
+ ds.unit_symbol = ''
287
+ ds.unit_type = ''
288
+ end
289
+ json = @feed.generate_json("0.6-alpha")
290
+ json[:datastreams].each do |ds|
291
+ ds[:unit].should be_nil
292
+ end
293
+ end
294
+
295
+ it "should ignore datastream units if nil" do
296
+ @feed.datastreams.each do |ds|
297
+ ds.unit_label = nil
298
+ ds.unit_symbol = nil
299
+ ds.unit_type = nil
300
+ end
301
+ json = @feed.generate_json("0.6-alpha")
302
+ json[:datastreams].each do |ds|
303
+ ds[:unit].should be_nil
304
+ end
305
+ end
306
+
307
+ it "should ignore datastream tags if blank" do
308
+ @feed.datastreams.each do |ds|
309
+ ds.tags = ""
310
+ end
311
+ json = @feed.generate_json("0.6-alpha")
312
+ json[:datastreams].each do |ds|
313
+ ds[:tags].should be_nil
314
+ end
315
+ end
316
+
317
+
318
+ it "should ignore datastream tags if nil" do
319
+ @feed.datastreams.each do |ds|
320
+ ds.tags = nil
321
+ end
322
+ json = @feed.generate_json("0.6-alpha")
323
+ json[:datastreams].each do |ds|
324
+ ds[:tags].should be_nil
325
+ end
326
+ end
327
+
328
+ it "should ignore datastream value fields if nil" do
329
+ @feed.datastreams.each do |ds|
330
+ ds.min_value = nil
331
+ end
332
+ json = @feed.generate_json("0.6-alpha")
333
+ json[:datastreams].each do |ds|
334
+ ds[:values].first[:min_value].should be_nil
335
+ end
336
+ end
337
+
338
+ it "should ignore datastream value fields if blank" do
339
+ @feed.datastreams.each do |ds|
340
+ ds.min_value = ""
341
+ end
342
+ json = @feed.generate_json("0.6-alpha")
343
+ json[:datastreams].each do |ds|
344
+ ds[:values].first[:min_value].should be_nil
345
+ end
346
+ end
347
+ it "should ignore tags if blank" do
348
+ @feed.tags = ""
349
+ json = @feed.generate_json("0.6-alpha")
350
+ json[:tags].should be_nil
351
+ end
352
+
353
+
354
+ it "should ignore tags if nil" do
355
+ @feed.tags = nil
356
+ json = @feed.generate_json("0.6-alpha")
357
+ json[:tags].should be_nil
358
+ end
359
+
360
+ it "should ignore nil location elements" do
361
+ @feed.location_disposition = nil
362
+ @feed.location_ele = nil
363
+ json = @feed.generate_json("0.6-alpha")
364
+ json[:location][:disposition].should be_nil
365
+ json[:location][:ele].should be_nil
366
+ end
367
+
368
+
369
+ it "should ignore blank location elements" do
370
+ @feed.location_disposition = ""
371
+ @feed.location_ele = ""
372
+ json = @feed.generate_json("0.6-alpha")
373
+ json[:location][:disposition].should be_nil
374
+ json[:location][:ele].should be_nil
375
+ end
376
+
377
+
378
+ it "should ignore location if all elements are nil" do
379
+ @feed.location_name = nil
380
+ @feed.location_disposition = nil
381
+ @feed.location_lat = nil
382
+ @feed.location_lon = nil
383
+ @feed.location_exposure = nil
384
+ @feed.location_ele = nil
385
+ @feed.location_domain = nil
386
+ json = @feed.generate_json("0.6-alpha")
387
+ json[:location].should be_nil
388
+ end
389
+
390
+ it "should include empty stuff if we pass :include_blank" do
391
+ @feed.description = ''
392
+ @feed.location_ele = ''
393
+ json = @feed.generate_json("0.6-alpha", :include_blank => true)
394
+ json[:description].should == ''
395
+ json[:location][:ele].should == ''
396
+ end
397
+ end
398
+ end
399
+