cosm-rb 0.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.
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,301 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe Cosm::Datastream do
5
+
6
+ it "should have a constant that defines the allowed keys" do
7
+ Cosm::Datastream::ALLOWED_KEYS.should == %w(feed_id id feed_creator current_value datapoints max_value min_value tags unit_label unit_symbol unit_type updated datapoints_function)
8
+ end
9
+
10
+ describe "validation" do
11
+ before(:each) do
12
+ @datastream = Cosm::Datastream.new
13
+ end
14
+
15
+ %w(id).each do |field|
16
+ it "should require a '#{field}'" do
17
+ @datastream.send("#{field}=".to_sym, nil)
18
+ @datastream.should_not be_valid
19
+ @datastream.errors[field.to_sym].should include("can't be blank")
20
+ end
21
+ end
22
+
23
+ ["red hat", "foo*", "KYB:FOO"].each do |invalid_id|
24
+ it "should not allow '#{invalid_id}' as an id" do
25
+ @datastream.id = invalid_id
26
+ @datastream.should_not be_valid
27
+ @datastream.errors[:id].should include("is invalid")
28
+ end
29
+ end
30
+
31
+ ["üöäÜÖÄ", "ÜÖÄ", "üöä", "current_to_direction-degrees_true-1", "current_to_direction.degrees_true.1"].each do |valid_id|
32
+ it "should allow '#{valid_id}' as an id" do
33
+ @datastream.feed_id = 1234
34
+ @datastream.id = valid_id
35
+ @datastream.should be_valid
36
+ @datastream.errors[:id].should be_empty
37
+ end
38
+ end
39
+
40
+ ["red hat", "foo*", 12.05].each do |invalid_feed_id|
41
+ it "should not allow '#{invalid_feed_id}' as a feed_id" do
42
+ @datastream.id = "1"
43
+ @datastream.feed_id = invalid_feed_id
44
+ @datastream.should_not be_valid
45
+ @datastream.errors[:feed_id].should include("is invalid")
46
+ end
47
+ end
48
+
49
+ [0, 1234, 102931203, "123"].each do |valid_feed_id|
50
+ it "should allow '#{valid_feed_id}' as a feed_id" do
51
+ @datastream.id = "1"
52
+ @datastream.feed_id = valid_feed_id
53
+ @datastream.should be_valid
54
+ @datastream.errors[:feed_id].should be_empty
55
+ end
56
+ end
57
+
58
+ %w(current_value tags).each do |field|
59
+ it "should restrict '#{field}' field length to 255" do
60
+ @datastream.send("#{field}=".to_sym, "a"*256)
61
+ @datastream.should_not be_valid
62
+ @datastream.errors[field.to_sym].should == ["is too long (maximum is 255 characters)"]
63
+ end
64
+ end
65
+
66
+ it "should not allow arrays of 255 entries for tags" do
67
+ @datastream.tags = []
68
+ 254.times {@datastream.tags << 'a'}
69
+ @datastream.should_not be_valid
70
+ @datastream.errors[:tags].should == ["is too long (maximum is 255 characters)"]
71
+ end
72
+
73
+ %w(unit_type current_value).each do |field|
74
+ it "should allow blank '#{field}'" do
75
+ @datastream.send("#{field}=".to_sym, nil)
76
+ @datastream.valid?
77
+ @datastream.errors[field.to_sym].should be_blank
78
+ end
79
+ end
80
+
81
+ %w(basicSI derivedSI conversionBasedUnits derivedUnits contextDependentUnits).each do |valid_unit_type|
82
+ it "should allow unit_type of '#{valid_unit_type}'" do
83
+ @datastream.unit_type = valid_unit_type
84
+ @datastream.valid?
85
+ @datastream.errors[:unit_type].should be_blank
86
+ end
87
+ end
88
+
89
+ %w(baicSI deriedSI conversinBasedUnits deriedUnits cotextDependentUnits).each do |invalid_unit_type|
90
+ it "should not allow unit_type of '#{invalid_unit_type}'" do
91
+ @datastream.unit_type = invalid_unit_type
92
+ @datastream.valid?
93
+ @datastream.errors[:unit_type].should == ["is not a valid unit_type (pick one from #{Cosm::Datastream::VALID_UNIT_TYPES.join(', ')} or leave blank)"]
94
+ end
95
+ end
96
+ end
97
+
98
+ describe "#initialize" do
99
+ it "should create a blank slate when passed no arguments" do
100
+ datastream = Cosm::Datastream.new
101
+ Cosm::Datastream::ALLOWED_KEYS.each do |attr|
102
+ datastream.attributes[attr.to_sym].should be_nil
103
+ end
104
+ end
105
+
106
+ %w(xml json hash csv).each do |format|
107
+ it "should accept #{format}" do
108
+ datastream = Cosm::Datastream.new(datastream_as_(format.to_sym))
109
+ datastream.current_value.should == "14"
110
+ end
111
+
112
+ %w(to_csv as_json to_xml to_json attributes).each do |output_format|
113
+ it "should be able to output from #{format} using #{output_format}" do
114
+ datastream = Cosm::Datastream.new(datastream_as_(format.to_sym))
115
+ lambda {datastream.send(output_format.to_sym)}.should_not raise_error
116
+ end
117
+ end
118
+ end
119
+ end
120
+
121
+ describe "#attributes" do
122
+ it "should return a hash of datastream properties" do
123
+ attrs = {}
124
+ Cosm::Datastream::ALLOWED_KEYS.each do |key|
125
+ attrs[key] = "key #{rand(1000)}"
126
+ end
127
+ attrs["datapoints"] = [Cosm::Datapoint.new({"value" => "ein"})]
128
+ datastream = Cosm::Datastream.new(attrs)
129
+
130
+ datastream.attributes.should == attrs
131
+ end
132
+
133
+ it "should not return nil values" do
134
+ attrs = {}
135
+ Cosm::Datastream::ALLOWED_KEYS.each do |key|
136
+ attrs[key] = "key #{rand(1000)}"
137
+ end
138
+ attrs["created_at"] = nil
139
+ datastream = Cosm::Datastream.new(attrs)
140
+
141
+ datastream.attributes.should_not include("created_at")
142
+ end
143
+ end
144
+
145
+ describe "#attributes=" do
146
+ it "should accept and save a hash of datastream properties" do
147
+ datastream = Cosm::Datastream.new({})
148
+
149
+ attrs = {}
150
+ Cosm::Datastream::ALLOWED_KEYS.each do |key|
151
+ value = "key #{rand(1000)}"
152
+ attrs[key] = value
153
+ datastream.should_receive("#{key}=").with(value)
154
+ end
155
+ datastream.attributes=(attrs)
156
+ end
157
+ end
158
+
159
+ context "associated datapoints" do
160
+
161
+ describe "#datapoints" do
162
+ it "should return an array of datapoints" do
163
+ datapoints = [Cosm::Datapoint.new(datapoint_as_(:hash))]
164
+ attrs = {"datapoints" => datapoints}
165
+ datastream = Cosm::Datastream.new(attrs)
166
+ datastream.datapoints.each do |ds|
167
+ ds.should be_kind_of(Cosm::Datapoint)
168
+ end
169
+ end
170
+
171
+ it "should never be nil" do
172
+ Cosm::Datastream.new({}).datapoints.should == []
173
+ end
174
+ end
175
+
176
+ describe "#datapoints=" do
177
+ before(:each) do
178
+ @datastream = Cosm::Datastream.new({})
179
+ end
180
+
181
+ it "should be empty if not assigned an array" do
182
+ @datastream.datapoints = "kittens"
183
+ @datastream.datapoints.should be_empty
184
+ end
185
+
186
+ it "should accept an array of datapoints and hashes and store an array of datapoints" do
187
+ new_datapoint1 = Cosm::Datapoint.new(datapoint_as_(:hash))
188
+ new_datapoint2 = Cosm::Datapoint.new(datapoint_as_(:hash))
189
+ Cosm::Datapoint.should_receive(:new).with(datapoint_as_(:hash)).and_return(new_datapoint2)
190
+
191
+ datapoints = [new_datapoint1, datapoint_as_(:hash)]
192
+ @datastream.datapoints = datapoints
193
+ @datastream.datapoints.length.should == 2
194
+ @datastream.datapoints.should include(new_datapoint1)
195
+ @datastream.datapoints.should include(new_datapoint2)
196
+ end
197
+
198
+ it "should accept an array of datapoints and store an array of datapoints" do
199
+ datapoints = [Cosm::Datapoint.new(datapoint_as_(:hash))]
200
+ @datastream.datapoints = datapoints
201
+ @datastream.datapoints.should == datapoints
202
+ end
203
+
204
+ it "should accept an array of hashes and store an array of datapoints" do
205
+ new_datapoint = Cosm::Datapoint.new(datapoint_as_(:hash))
206
+ Cosm::Datapoint.should_receive(:new).with(datapoint_as_(:hash)).and_return(new_datapoint)
207
+
208
+ datapoints_hash = [datapoint_as_(:hash)]
209
+ @datastream.datapoints = datapoints_hash
210
+ @datastream.datapoints.should == [new_datapoint]
211
+ end
212
+ end
213
+ end
214
+
215
+ # Provided by Cosm::Templates::DatastreamDefaults
216
+ describe "#generate_json" do
217
+ it "should take a version and generate the appropriate template" do
218
+ datastream = Cosm::Datastream.new({})
219
+ Cosm::Template.should_receive(:new).with(datastream, :json)
220
+ lambda {datastream.generate_json("1.0.0")}.should raise_error(NoMethodError)
221
+ end
222
+ end
223
+
224
+ describe "#to_csv" do
225
+
226
+ it "should call the csv generator with default version" do
227
+ datastream = Cosm::Datastream.new({})
228
+ datastream.should_receive(:generate_csv).with("2", {}).and_return("2")
229
+ datastream.to_csv.should == "2"
230
+ end
231
+
232
+ it "should accept optional xml version" do
233
+ datastream = Cosm::Datastream.new({})
234
+ datastream.should_receive(:generate_csv).with("1", {}).and_return("1234,32")
235
+ datastream.to_csv(:version => "1").should == "1234,32"
236
+ end
237
+
238
+ it "should accept additional options" do
239
+ datastream = Cosm::Datastream.new({})
240
+ datastream.should_receive(:generate_csv).with("1", {:full => true}).and_return("34")
241
+ datastream.to_csv(:version => "1", :full => true).should == "34"
242
+ end
243
+ end
244
+
245
+ describe "#to_xml" do
246
+
247
+ it "should call the xml generator with default version" do
248
+ datastream = Cosm::Datastream.new({})
249
+ datastream.should_receive(:generate_xml).with("0.5.1", {}).and_return("<xml></xml>")
250
+ datastream.to_xml.should == "<xml></xml>"
251
+ end
252
+
253
+ it "should accept optional xml version" do
254
+ datastream = Cosm::Datastream.new({})
255
+ datastream.should_receive(:generate_xml).with("5", {}).and_return("<xml></xml>")
256
+ datastream.to_xml(:version => "5").should == "<xml></xml>"
257
+ end
258
+
259
+ end
260
+
261
+ describe "#as_json" do
262
+
263
+ it "should call the json generator with default version" do
264
+ datastream = Cosm::Datastream.new({})
265
+ datastream.should_receive(:generate_json).with("1.0.0", {}).and_return({"title" => "Environment"})
266
+ datastream.as_json.should == {"title" => "Environment"}
267
+ end
268
+
269
+ it "should accept optional json version" do
270
+ datastream = Cosm::Datastream.new({})
271
+ datastream.should_receive(:generate_json).with("0.6-alpha", {}).and_return({"title" => "Environment"})
272
+ datastream.as_json(:version => "0.6-alpha").should == {"title" => "Environment"}
273
+ end
274
+
275
+ end
276
+
277
+ describe "#to_json" do
278
+ it "should call #as_json" do
279
+ datastream_hash = {"id" => "env001", "value" => "2344"}
280
+ datastream = Cosm::Datastream.new(datastream_hash)
281
+ datastream.should_receive(:as_json).with({})
282
+ datastream.to_json
283
+ end
284
+
285
+ it "should pass options through to #as_json" do
286
+ datastream_hash = {"id" => "env001", "value" => "2344"}
287
+ datastream = Cosm::Datastream.new(datastream_hash)
288
+ datastream.should_receive(:as_json).with({:crazy => "options"})
289
+ datastream.to_json({:crazy => "options"})
290
+ end
291
+
292
+ it "should pass the output of #as_json to yajl" do
293
+ datastream_hash = {"id" => "env001", "value" => "2344"}
294
+ datastream = Cosm::Datastream.new(datastream_hash)
295
+ datastream.should_receive(:as_json).and_return({:awesome => "hash"})
296
+ ::JSON.should_receive(:generate).with({:awesome => "hash"})
297
+ datastream.to_json
298
+ end
299
+ end
300
+ end
301
+
@@ -0,0 +1,298 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cosm::Feed do
4
+
5
+ it "should have a constant that defines the allowed keys" do
6
+ Cosm::Feed::ALLOWED_KEYS.should == %w(id creator owner_login datastreams description email feed icon location_disposition location_domain location_ele location_exposure location_lat location_lon location_name location_waypoints private status tags title updated created website auto_feed_url csv_version)
7
+ end
8
+
9
+ context "attr accessors" do
10
+ before(:each) do
11
+ @feed = Cosm::Feed.new(feed_as_(:json))
12
+ end
13
+
14
+ describe "setting whitelisted fields" do
15
+ Cosm::Feed::ALLOWED_KEYS.each do |key|
16
+ it "##{key}=" do
17
+ lambda {
18
+ @feed.send("#{key}=", key)
19
+ }.should_not raise_error
20
+ end
21
+ end
22
+ end
23
+
24
+ describe "getting whitelisted fields" do
25
+ Cosm::Feed::ALLOWED_KEYS.each do |key|
26
+ it "##{key}" do
27
+ lambda {
28
+ @feed.send(key)
29
+ }.should_not raise_error
30
+ end
31
+ end
32
+ end
33
+
34
+ describe "setting non-whitelisted keys" do
35
+ it "should not be possible to set non-whitelisted fields" do
36
+ lambda {
37
+ @feed.something_bogus = 'whatevs'
38
+ }.should raise_error
39
+ end
40
+
41
+ it "should not be possible to get non-whitelisted fields" do
42
+ lambda {
43
+ @feed.something_bogus
44
+ }.should raise_error
45
+ end
46
+ end
47
+ end
48
+
49
+ describe "validation" do
50
+ %w(title).each do |field|
51
+ it "should require a '#{field}'" do
52
+ feed = Cosm::Feed.new
53
+ feed.send("#{field}=".to_sym, nil)
54
+ feed.should_not be_valid
55
+ feed.errors[field.to_sym].should == ["can't be blank"]
56
+ end
57
+ end
58
+
59
+ it "should not allow duplicate datastreams" do
60
+ feed = Cosm::Feed.new
61
+ feed.datastreams = [Cosm::Datastream.new('id' => '1'), Cosm::Datastream.new('id' => '1')]
62
+ feed.should_not be_valid
63
+ feed.errors[:datastreams].should == ["can't have duplicate IDs: 1"]
64
+ end
65
+
66
+ it "should validate all datastreams" do
67
+ feed = Cosm::Feed.new
68
+ feed.datastreams = [Cosm::Datastream.new('id' => '')]
69
+ feed.should_not be_valid
70
+ feed.errors[:datastreams_id].should == ["can't be blank"]
71
+ end
72
+ end
73
+
74
+ describe "#initialize" do
75
+ it "should create a blank slate when passed no arguments" do
76
+ feed = Cosm::Feed.new
77
+ Cosm::Feed::ALLOWED_KEYS.each do |attr|
78
+ feed.attributes[attr.to_sym].should be_nil
79
+ end
80
+ end
81
+
82
+ %w(xml json hash).each do |format|
83
+ it "should accept #{format}" do
84
+ feed = Cosm::Feed.new(feed_as_(format.to_sym))
85
+ feed.title.downcase.should == "cosm office environment"
86
+ end
87
+
88
+ %w(to_csv as_json to_xml to_json attributes).each do |output_format|
89
+ it "should be able to output from #{format} using #{output_format}" do
90
+ feed = Cosm::Feed.new(feed_as_(format.to_sym))
91
+ lambda {feed.send(output_format.to_sym)}.should_not raise_error
92
+ end
93
+ end
94
+ end
95
+ end
96
+
97
+ describe "#attributes" do
98
+ it "should return a hash of feed properties" do
99
+ attrs = {}
100
+ Cosm::Feed::ALLOWED_KEYS.each do |key|
101
+ attrs[key] = "key #{rand(1000)}"
102
+ end
103
+ attrs["datastreams"] = [Cosm::Datastream.new({"id" => "ein"})]
104
+ feed = Cosm::Feed.new(attrs)
105
+
106
+ feed.attributes.should == attrs
107
+ end
108
+
109
+ it "should contain csv_version in the allowed keys" do
110
+ Cosm::Feed::ALLOWED_KEYS.should include("csv_version")
111
+ end
112
+
113
+ it "should not return nil values" do
114
+ attrs = {}
115
+ Cosm::Feed::ALLOWED_KEYS.each do |key|
116
+ attrs[key] = "key #{rand(1000)}"
117
+ end
118
+ attrs["created_at"] = nil
119
+ feed = Cosm::Feed.new(attrs)
120
+
121
+ feed.attributes.should_not include("created_at")
122
+ end
123
+ end
124
+
125
+ describe "#attributes=" do
126
+ it "should accept and save a hash of feed properties" do
127
+ feed = Cosm::Feed.new({})
128
+
129
+ attrs = {}
130
+ Cosm::Feed::ALLOWED_KEYS.each do |key|
131
+ value = "key #{rand(1000)}"
132
+ attrs[key] = value
133
+ feed.should_receive("#{key}=").with(value)
134
+ end
135
+ feed.attributes=(attrs)
136
+ end
137
+ end
138
+
139
+ context "associated datastreams" do
140
+
141
+ describe "#datastreams" do
142
+ it "should return an array of datastreams" do
143
+ datastreams = [Cosm::Datastream.new(datastream_as_(:hash))]
144
+ attrs = {"datastreams" => datastreams}
145
+ feed = Cosm::Feed.new(attrs)
146
+ feed.datastreams.each do |ds|
147
+ ds.should be_kind_of(Cosm::Datastream)
148
+ end
149
+ end
150
+
151
+ it "should default to an empty array" do
152
+ feed = Cosm::Feed.new({})
153
+ feed.datastreams.should == []
154
+ end
155
+ end
156
+
157
+ describe "#datastreams=" do
158
+ before(:each) do
159
+ @feed = Cosm::Feed.new({})
160
+ end
161
+
162
+ it "should return an empty array if not an array" do
163
+ @feed.datastreams = "kittens"
164
+ @feed.datastreams.should be_empty
165
+ end
166
+
167
+ it "should accept an array of datastreams and hashes and store an array of datastreams" do
168
+ new_datastream1 = Cosm::Datastream.new(datastream_as_(:hash))
169
+ new_datastream2 = Cosm::Datastream.new(datastream_as_(:hash))
170
+ Cosm::Datastream.should_receive(:new).with(datastream_as_(:hash)).and_return(new_datastream2)
171
+
172
+ datastreams = [new_datastream1, datastream_as_(:hash)]
173
+ @feed.datastreams = datastreams
174
+ @feed.datastreams.length.should == 2
175
+ @feed.datastreams.should include(new_datastream1)
176
+ @feed.datastreams.should include(new_datastream2)
177
+ end
178
+
179
+ it "should accept an array of datastreams and store an array of datastreams" do
180
+ datastreams = [Cosm::Datastream.new(datastream_as_(:hash))]
181
+ @feed.datastreams = datastreams
182
+ @feed.datastreams.should == datastreams
183
+ end
184
+
185
+ it "should accept an array of hashes and store an array of datastreams" do
186
+ new_datastream = Cosm::Datastream.new(datastream_as_(:hash))
187
+ Cosm::Datastream.should_receive(:new).with(datastream_as_(:hash)).and_return(new_datastream)
188
+
189
+ datastreams_hash = [datastream_as_(:hash)]
190
+ @feed.datastreams = datastreams_hash
191
+ @feed.datastreams.should == [new_datastream]
192
+ end
193
+
194
+ it "should accept an array of subclass of datastream and store an array of datastreams" do
195
+ class OurSpecialDatastreamClass < Cosm::Datastream
196
+ attr_accessor :something_new
197
+ end
198
+
199
+ datastreams = [OurSpecialDatastreamClass.new(datastream_as_(:hash))]
200
+ @feed.datastreams = datastreams
201
+ @feed.datastreams.should == datastreams
202
+ end
203
+ end
204
+
205
+ end
206
+
207
+ # Provided by Cosm::Templates::FeedDefaults
208
+ describe "#generate_json" do
209
+ it "should take a version and generate the appropriate template" do
210
+ feed = Cosm::Feed.new({})
211
+ Cosm::Template.should_receive(:new).with(feed, :json)
212
+ lambda {feed.generate_json("1.0.0", {})}.should raise_error(NoMethodError)
213
+ end
214
+ end
215
+
216
+ describe "#to_csv" do
217
+ it "should call the csv generator with default version" do
218
+ feed = Cosm::Feed.new({})
219
+ feed.should_receive(:generate_csv).with("2", {}).and_return("1,2,3,4")
220
+ feed.to_csv.should == "1,2,3,4"
221
+ end
222
+
223
+ it "should accept optional csv version" do
224
+ version = "1"
225
+ feed = Cosm::Feed.new({})
226
+ feed.should_receive(:generate_csv).with(version, {}).and_return("1,2,3,4")
227
+ feed.to_csv(:version => version).should == "1,2,3,4"
228
+ end
229
+
230
+ it "should accept additional options" do
231
+ version = "1"
232
+ feed = Cosm::Feed.new({})
233
+ feed.should_receive(:generate_csv).with(version, :full => true).and_return("1,2,3,4")
234
+ feed.to_csv(:version => version, :full => true).should == "1,2,3,4"
235
+ end
236
+ end
237
+
238
+ describe "#to_xml" do
239
+ it "should call the xml generator with default version" do
240
+ feed = Cosm::Feed.new({})
241
+ feed.should_receive(:generate_xml).with("0.5.1", {}).and_return("<xml></xml>")
242
+ feed.to_xml.should == "<xml></xml>"
243
+ end
244
+
245
+ it "should accept optional xml version" do
246
+ version = "5"
247
+ feed = Cosm::Feed.new({})
248
+ feed.should_receive(:generate_xml).with(version, {}).and_return("<xml></xml>")
249
+ feed.to_xml(:version => version).should == "<xml></xml>"
250
+ end
251
+ end
252
+
253
+ describe "#as_json" do
254
+ it "should call the json generator with default version" do
255
+ feed = Cosm::Feed.new({})
256
+ feed.should_receive(:generate_json).with("1.0.0", {}).and_return({"title" => "Environment"})
257
+ feed.as_json.should == {"title" => "Environment"}
258
+ end
259
+
260
+ it "should accept optional json version" do
261
+ version = "0.6-alpha"
262
+ feed = Cosm::Feed.new({})
263
+ feed.should_receive(:generate_json).with(version, {}).and_return({"title" => "Environment"})
264
+ feed.as_json(:version => version).should == {"title" => "Environment"}
265
+ end
266
+ end
267
+
268
+ describe "#to_json" do
269
+ it "should call #as_json" do
270
+ feed_hash = {"title" => "Environment"}
271
+ feed = Cosm::Feed.new(feed_hash)
272
+ feed.should_receive(:as_json).with({})
273
+ feed.to_json
274
+ end
275
+
276
+ it "should pass options through to #as_json" do
277
+ feed_hash = {"title" => "Environment"}
278
+ feed = Cosm::Feed.new(feed_hash)
279
+ feed.should_receive(:as_json).with({:crazy => "options"})
280
+ feed.to_json({:crazy => "options"})
281
+ end
282
+
283
+ it "should generate datastreams" do
284
+ feed = Cosm::Feed.new(feed_as_('hash'))
285
+ feed.datastreams = datastream_as_(:hash)
286
+ JSON.parse(feed.to_json)["datastreams"].should_not be_nil
287
+ end
288
+
289
+ it "should pass the output of #as_json to yajl" do
290
+ feed_hash = {"title" => "Environment"}
291
+ feed = Cosm::Feed.new(feed_hash)
292
+ feed.should_receive(:as_json).and_return({:awesome => "hash"})
293
+ ::JSON.should_receive(:generate).with({:awesome => "hash"})
294
+ feed.to_json
295
+ end
296
+ end
297
+ end
298
+
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Hash extensions" do
4
+ it "should add a delete_if_nil_value method to hashes" do
5
+ hash = { "foo" => "awesome", "bar" => nil, :symbol => "" }
6
+ hash.delete_if_nil_value.should == { "foo" => "awesome" }
7
+ end
8
+
9
+ # HACK probably
10
+ it "should add a deep_stringify_keys method to hash instances" do
11
+ hash = { :sym => "val", :nested => { "str" => 12, :symtoo => 58 } }
12
+ hash.deep_stringify_keys.should == { "sym" => "val", "nested" => { "str" => 12, "symtoo" => 58 } }
13
+ end
14
+
15
+ it "should have a destructive in place version of the deep_stringify_keys method" do
16
+ hash = { :sym => "val", :nested => { "str" => 12, :symtoo => 58 } }
17
+ hash.deep_stringify_keys!
18
+ hash.should == { "sym" => "val", "nested" => { "str" => 12, "symtoo" => 58 } }
19
+ end
20
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Template helpers" do
4
+ include Cosm::Helpers
5
+
6
+ describe "#join_tags" do
7
+
8
+ it "should just return the string if it already is one" do
9
+ join_tags('help, me').should == 'help, me'
10
+ end
11
+
12
+ it "should convert an array into a string" do
13
+ join_tags(['bag', 'jag', 'lag', 'tag']).should == 'bag,jag,lag,tag'
14
+ end
15
+
16
+ it "should sort the array" do
17
+ join_tags(['zag', 'xag', 'Bag', 'aag']).should == 'aag,Bag,xag,zag'
18
+ end
19
+ end
20
+
21
+ describe "#parse_tag_string" do
22
+ it "should handle single tags" do
23
+ parse_tag_string('tag').should == ['tag']
24
+ end
25
+
26
+ it "should handle multiple tags" do
27
+ parse_tag_string('tag, bag ,lag,jag').should == ['bag', 'jag', 'lag', 'tag']
28
+ end
29
+
30
+ it "should handle complex tags" do
31
+ string = 'apple, orange, "red room", " I like space ", "tennis, later"'
32
+ parse_tag_string(string).should ==
33
+ [
34
+ " I like space ",
35
+ "apple",
36
+ "orange",
37
+ "red room",
38
+ "tennis, later"
39
+ ]
40
+ end
41
+
42
+ it "should handle tags with escaped double quote" do
43
+ string = 'apple, orange, "horse:height=2\"", " I like space ", "tennis, later", "\"quote\""'
44
+ parse_tag_string(string).should ==
45
+ [
46
+ " I like space ",
47
+ "\"quote\"",
48
+ "apple",
49
+ "horse:height=2\"",
50
+ "orange",
51
+ "tennis, later"
52
+ ]
53
+ end
54
+ end
55
+ end
56
+