supplejack_client 1.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 (47) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +27 -0
  3. data/.rspec +2 -0
  4. data/Gemfile +16 -0
  5. data/Guardfile +24 -0
  6. data/LICENSE.txt +674 -0
  7. data/README.md +153 -0
  8. data/Rakefile +9 -0
  9. data/lib/generators/locales/en.yml +11 -0
  10. data/lib/generators/supplejack/install_generator.rb +28 -0
  11. data/lib/generators/templates/README +19 -0
  12. data/lib/generators/templates/supplejack_client.rb +120 -0
  13. data/lib/supplejack/config.rb +116 -0
  14. data/lib/supplejack/controllers/helpers.rb +172 -0
  15. data/lib/supplejack/engine.rb +20 -0
  16. data/lib/supplejack/exceptions.rb +17 -0
  17. data/lib/supplejack/facet.rb +33 -0
  18. data/lib/supplejack/item.rb +94 -0
  19. data/lib/supplejack/item_relation.rb +73 -0
  20. data/lib/supplejack/log_subscriber.rb +58 -0
  21. data/lib/supplejack/paginated_collection.rb +61 -0
  22. data/lib/supplejack/record.rb +147 -0
  23. data/lib/supplejack/request.rb +95 -0
  24. data/lib/supplejack/search.rb +346 -0
  25. data/lib/supplejack/url_formats/item_hash.rb +208 -0
  26. data/lib/supplejack/user.rb +132 -0
  27. data/lib/supplejack/user_set.rb +349 -0
  28. data/lib/supplejack/user_set_relation.rb +143 -0
  29. data/lib/supplejack/util.rb +120 -0
  30. data/lib/supplejack/version.rb +10 -0
  31. data/lib/supplejack_client.rb +29 -0
  32. data/spec/spec_helper.rb +23 -0
  33. data/spec/supplejack/controllers/helpers_spec.rb +277 -0
  34. data/spec/supplejack/facet_spec.rb +44 -0
  35. data/spec/supplejack/item_relation_spec.rb +111 -0
  36. data/spec/supplejack/item_spec.rb +115 -0
  37. data/spec/supplejack/log_subscriber_spec.rb +40 -0
  38. data/spec/supplejack/paginated_collection_spec.rb +43 -0
  39. data/spec/supplejack/record_spec.rb +255 -0
  40. data/spec/supplejack/request_spec.rb +195 -0
  41. data/spec/supplejack/search_spec.rb +727 -0
  42. data/spec/supplejack/url_formats/item_hash_spec.rb +341 -0
  43. data/spec/supplejack/user_set_relation_spec.rb +149 -0
  44. data/spec/supplejack/user_set_spec.rb +465 -0
  45. data/spec/supplejack/user_spec.rb +159 -0
  46. data/supplejack_client.gemspec +30 -0
  47. metadata +159 -0
@@ -0,0 +1,465 @@
1
+ # The Supplejack Client code is Crown copyright (C) 2014, New Zealand Government,
2
+ # and is licensed under the GNU General Public License, version 3.
3
+ # See https://github.com/DigitalNZ/supplejack_client for details.
4
+ #
5
+ # Supplejack was created by DigitalNZ at the National Library of NZ
6
+ # and the Department of Internal Affairs. http://digitalnz.org/supplejack
7
+
8
+ require 'spec_helper'
9
+
10
+ class SupplejackRecord
11
+ include Supplejack::Record
12
+ end
13
+
14
+ module Supplejack
15
+ describe UserSet do
16
+ let(:supplejack_set) { Supplejack::UserSet.new(records: [{record_id: 1, position: 2, title: 'Dogs'}]) }
17
+
18
+ before :each do
19
+ Supplejack.stub(:enable_caching) { false }
20
+ Supplejack::UserSet.stub(:get) { {'set' => {id: '123abc', name: 'Dogs', count: 0}} }
21
+ end
22
+
23
+ describe '#initialize' do
24
+ [:id, :name, :description, :privacy, :url, :priority, :count, :tag_list, :homepage, :approved].each do |attribute|
25
+ it "initializes the #{attribute}" do
26
+ Supplejack::UserSet.new({attribute => 'value'}).send(attribute).should eq 'value'
27
+ end
28
+ end
29
+
30
+ it 'converts the updated_at into a time object' do
31
+ Supplejack::UserSet.new(updated_at: '2012-08-17T10:01:00+12:00').updated_at.should be_a Time
32
+ end
33
+
34
+ it 'symbolizes the attributes hash' do
35
+ Supplejack::UserSet.new({'name' => 'Dog'}).name.should eq 'Dog'
36
+ end
37
+
38
+ it 'handles nil attributes' do
39
+ Supplejack::UserSet.new(nil).attributes
40
+ end
41
+
42
+ it 'initializes a user object' do
43
+ user_set = Supplejack::UserSet.new({user: {name: 'Juanito'}})
44
+ user_set.user.should be_a Supplejack::User
45
+ user_set.user.name.should eq 'Juanito'
46
+ end
47
+ end
48
+
49
+ describe '#attributes' do
50
+ it 'returns a hash of the set attributes' do
51
+ set = Supplejack::UserSet.new
52
+ set.name = 'Dogs'
53
+ set.description = 'Hi'
54
+ set.attributes.should include(name: 'Dogs', description: 'Hi')
55
+ end
56
+
57
+ it 'includes an array of :records' do
58
+ set = Supplejack::UserSet.new
59
+ set.records = [{record_id: 1, position: 1}]
60
+ set.attributes[:records].should eq [{record_id: 1, position: 1}]
61
+ end
62
+ end
63
+
64
+ describe '#api_attributes' do
65
+ it 'only returns the fields that can be stored' do
66
+ supplejack_set.attributes = {count: 1, url: 'Hi'}
67
+ supplejack_set.should_not_receive(:count)
68
+ supplejack_set.should_not_receive(:url)
69
+ supplejack_set.api_attributes
70
+ end
71
+
72
+ it 'should send the homepage value' do
73
+ supplejack_set.homepage = true
74
+ supplejack_set.api_attributes.should include(homepage: true)
75
+ end
76
+
77
+ it 'returns a array of records with only id and position' do
78
+ supplejack_set.stub(:api_records) { [{record_id: 1, position: 1}] }
79
+ supplejack_set.api_attributes[:records].should eq [{record_id: 1, position: 1}]
80
+ end
81
+ end
82
+
83
+ describe '#items' do
84
+ it "initializes a item_relation object" do
85
+ supplejack_set.items.should be_a Supplejack::ItemRelation
86
+ end
87
+ end
88
+
89
+ describe '#tag_list' do
90
+ it 'returns a comma sepparated list of tags' do
91
+ Supplejack::UserSet.new(tags: ['dog', 'cat']).tag_list.should eq 'dog, cat'
92
+ end
93
+ end
94
+
95
+ describe '#priority' do
96
+ it 'defaults to 1 when is null' do
97
+ Supplejack::UserSet.new.priority.should eq 1
98
+ end
99
+
100
+ it 'keeps the value set' do
101
+ Supplejack::UserSet.new(priority: 0).priority.should eq 0
102
+ end
103
+ end
104
+
105
+ describe '#favourite?' do
106
+ it 'returns true when the name is Favourites' do
107
+ Supplejack::UserSet.new(name: 'Favourites').favourite?.should be_true
108
+ end
109
+
110
+ it 'returns false when the name is something else' do
111
+ Supplejack::UserSet.new(name: 'Dogs').favourite?.should be_false
112
+ end
113
+ end
114
+
115
+ describe '#private?' do
116
+ it 'returns true when the privacy is private' do
117
+ Supplejack::UserSet.new(privacy: 'private').private?.should be_true
118
+ end
119
+
120
+ it 'returns false when the privacy is something else' do
121
+ Supplejack::UserSet.new(privacy: 'public').private?.should be_false
122
+ end
123
+ end
124
+
125
+ describe '#public?' do
126
+ it 'returns false when the privacy is private' do
127
+ Supplejack::UserSet.new(privacy: 'private').public?.should be_false
128
+ end
129
+
130
+ it 'returns true when the privacy is public' do
131
+ Supplejack::UserSet.new(privacy: 'public').public?.should be_true
132
+ end
133
+ end
134
+
135
+ describe '#hidden?' do
136
+ it 'returns false when the privacy is not hidden' do
137
+ Supplejack::UserSet.new(privacy: 'public').hidden?.should be_false
138
+ end
139
+
140
+ it 'returns true when the privacy is hidden' do
141
+ Supplejack::UserSet.new(privacy: 'hidden').hidden?.should be_true
142
+ end
143
+ end
144
+
145
+ describe '#has_record?' do
146
+ let(:supplejack_set) { Supplejack::UserSet.new(records: [{record_id: 1, position: 2, title: 'Dogs'}]) }
147
+
148
+ it 'returns true when the record is part of the set' do
149
+ supplejack_set.has_record?(1).should be_true
150
+ end
151
+
152
+ it 'returns false when the record is not part of the set' do
153
+ supplejack_set.has_record?(3).should be_false
154
+ end
155
+ end
156
+
157
+ describe '#save' do
158
+ before :each do
159
+ @attributes = {name: 'Dogs', description: 'hi', count: 3}
160
+ supplejack_set.stub(:api_attributes) { @attributes }
161
+ supplejack_set.stub(:api_key) { '123abc' }
162
+ end
163
+
164
+ context 'user_set is a new_record' do
165
+ before :each do
166
+ supplejack_set.stub(:new_record?) { true }
167
+ end
168
+
169
+ it 'triggers a POST request to /sets.json' do
170
+ Supplejack::UserSet.should_receive(:post).with("/sets", {api_key: "123abc"}, {set: @attributes}) { {"set" => {"id" => "new-id"}} }
171
+ supplejack_set.save.should be_true
172
+ end
173
+
174
+ it 'stores the id of the user_set' do
175
+ Supplejack::UserSet.stub(:post) { {'set' => {'id' => 'new-id'}} }
176
+ supplejack_set.save
177
+ supplejack_set.id.should eq 'new-id'
178
+ end
179
+
180
+ it 'returns false for anything other that a 200 response' do
181
+ Supplejack::UserSet.stub(:post).and_raise(RestClient::Forbidden.new)
182
+ supplejack_set.save.should be_false
183
+ end
184
+ end
185
+
186
+ context 'user_set is not new' do
187
+ before :each do
188
+ supplejack_set.stub(:new_record?) { false }
189
+ supplejack_set.id = '123'
190
+ end
191
+
192
+ it 'triggers a PUT request to /sets/123.json with the user set api_key' do
193
+ Supplejack::UserSet.should_receive(:put).with('/sets/123', {api_key: '123abc'}, {set: @attributes})
194
+ supplejack_set.save
195
+ end
196
+ end
197
+ end
198
+
199
+ describe '#update_attributes' do
200
+ it 'sets the attributes on the user_set' do
201
+ supplejack_set.should_receive('attributes=').with(name: 'Mac')
202
+ supplejack_set.update_attributes({name: 'Mac'})
203
+ end
204
+
205
+ it 'saves the user_set' do
206
+ supplejack_set.should_receive(:save)
207
+ supplejack_set.update_attributes({name: 'Mac'})
208
+ end
209
+ end
210
+
211
+ describe '#attributes=' do
212
+ it 'updates the attributes on the user_set' do
213
+ supplejack_set.attributes = {name: 'Mac'}
214
+ supplejack_set.name.should eq 'Mac'
215
+ end
216
+
217
+ it 'should only update passed attributes' do
218
+ supplejack_set.id = '12345'
219
+ supplejack_set.attributes = {name: 'Mac'}
220
+ supplejack_set.id.should eq '12345'
221
+ end
222
+
223
+ it "replaces the records in the ordered form" do
224
+ supplejack_set.attributes = {ordered_records: [9,1,5]}
225
+ supplejack_set.records.should eq([{record_id: 9, position: 1}, {record_id: 1, position: 2}, {record_id: 5, position: 3}])
226
+ end
227
+ end
228
+
229
+ describe '#updated_at= and created_at=' do
230
+ [:created_at, :updated_at].each do |attr|
231
+ it 'converts a string into a time object' do
232
+ supplejack_set.send("#{attr}=", '2012-08-17T10:01:00+12:00')
233
+ supplejack_set.send(attr).should be_a Time
234
+ end
235
+
236
+ it 'sets nil when the time is incorrect' do
237
+ supplejack_set.send("#{attr}=", '838927587hdfhsjdf')
238
+ supplejack_set.send(attr).should be_nil
239
+ end
240
+
241
+ it 'should accept a Time object too' do
242
+ @time = Time.now
243
+ supplejack_set.send("#{attr}=", @time)
244
+ supplejack_set.send(attr).should eq @time
245
+ end
246
+ end
247
+ end
248
+
249
+ describe '#api_records' do
250
+ it 'generates a hash of records with position and record_id' do
251
+ supplejack_set.stub(:records) { [{title: 'Hi', record_id: 1, position: 1} ] }
252
+ supplejack_set.api_records.should eq [{record_id: 1, position: 1}]
253
+ end
254
+
255
+ it 'removes records without a record_id' do
256
+ supplejack_set.stub(:records) { [{title: 'Hi', record_id: 1, position: 1}, {position: 6} ] }
257
+ supplejack_set.api_records.should eq [{record_id: 1, position: 1}]
258
+ end
259
+
260
+ it 'handles nil records' do
261
+ supplejack_set.stub(:records) { nil }
262
+ supplejack_set.api_records.should eq []
263
+ end
264
+ end
265
+
266
+ describe '#ordered_records_from_array' do
267
+ it 'returns a hash with positons and record_ids' do
268
+ supplejack_set.ordered_records_from_array([9,1,5]).should eq([{record_id: 9, position: 1}, {record_id: 1, position: 2}, {record_id: 5, position: 3}])
269
+ end
270
+ end
271
+
272
+ describe '#new_record?' do
273
+ it 'returns true when the user_set doesn\'t have a id' do
274
+ Supplejack::UserSet.new.new_record?.should be_true
275
+ end
276
+
277
+ it 'returns false when the user_set has a id' do
278
+ Supplejack::UserSet.new(id: '1234abc').new_record?.should be_false
279
+ end
280
+ end
281
+
282
+ describe '#destroy' do
283
+ before :each do
284
+ supplejack_set.stub(:api_key) { '123abc' }
285
+ supplejack_set.stub(:id) { '999' }
286
+ end
287
+
288
+ it 'executes a delete request to the API with the user set api_key' do
289
+ Supplejack::UserSet.should_receive(:delete).with('/sets/999', {api_key: '123abc'})
290
+ supplejack_set.destroy.should be_true
291
+ end
292
+
293
+ it 'returns false when the response is not a 200' do
294
+ Supplejack::UserSet.stub(:delete).and_raise(RestClient::Forbidden.new)
295
+ supplejack_set.destroy.should be_false
296
+ supplejack_set.errors.should eq 'Forbidden: '
297
+ end
298
+
299
+ it 'returns false when it is a new user set' do
300
+ supplejack_set.stub(:new_record?) { true }
301
+ Supplejack::UserSet.should_not_receive(:delete)
302
+ supplejack_set.destroy.should be_false
303
+ end
304
+ end
305
+
306
+ describe '#reload' do
307
+ let(:supplejack_set) { Supplejack::UserSet.new(id: '123456') }
308
+
309
+ before :each do
310
+ Supplejack::UserSet.should_receive(:get).with('/sets/123456') { {'set' => {'id' => 'abc'}} }
311
+ end
312
+
313
+ it 'fetches the set from the api and repopulates the set' do
314
+ supplejack_set.reload
315
+ supplejack_set.id.should eq 'abc'
316
+ end
317
+
318
+ it 'removes the existing @items relation' do
319
+ supplejack_set.items
320
+ supplejack_set.reload
321
+ supplejack_set.instance_variable_get('@items').should be_nil
322
+ end
323
+ end
324
+
325
+ describe '#viewable_by?' do
326
+ it 'returns true when the user_set is public' do
327
+ supplejack_set.stub(:public?) { true }
328
+ supplejack_set.viewable_by?(nil).should be_true
329
+ end
330
+
331
+ it 'returns true when the user_set is hidden' do
332
+ supplejack_set.stub(:hidden?) { true }
333
+ supplejack_set.viewable_by?(nil).should be_true
334
+ end
335
+
336
+ context 'private set' do
337
+ before :each do
338
+ supplejack_set.stub(:public?) { false }
339
+ end
340
+
341
+ it 'returns false when the user is not present' do
342
+ supplejack_set.viewable_by?(nil).should be_false
343
+ end
344
+
345
+ it 'returns true when the user has the same api_key as the user_set' do
346
+ user = double(:user, api_key: '12345')
347
+ supplejack_set.api_key = '12345'
348
+ supplejack_set.viewable_by?(user).should be_true
349
+ end
350
+
351
+ it 'returns false if both the api_key in the user and the set are nil' do
352
+ user = double(:user, api_key: nil)
353
+ supplejack_set.api_key = nil
354
+ supplejack_set.viewable_by?(user).should be_false
355
+ end
356
+ end
357
+ end
358
+
359
+ describe '#owned_by?' do
360
+ let(:user) { double(:user, api_key: '123456') }
361
+
362
+ it 'returns true when the users api_key is the same as the set\'s' do
363
+ supplejack_set.stub(:api_key) { "123456" }
364
+ supplejack_set.owned_by?(user).should be_true
365
+ end
366
+
367
+ it 'returns false when the set and user have different api_keys' do
368
+ supplejack_set.stub(:api_key) { '666' }
369
+ supplejack_set.owned_by?(user).should be_false
370
+ end
371
+
372
+ it 'returns false when both keys are nil' do
373
+ user.stub(:api_key) { nil }
374
+ supplejack_set.stub(:api_key) { nil }
375
+ supplejack_set.owned_by?(user).should be_false
376
+ end
377
+ end
378
+
379
+ describe '#set_record_id?' do
380
+ before(:each) do
381
+ @set = supplejack_set
382
+ end
383
+
384
+ it 'should return the record_id' do
385
+ @set.stub(:record).and_return({'record_id' => 123})
386
+ @set.set_record_id.should eq 123
387
+ end
388
+
389
+ it 'should return nil if the set doesn\'t have a record' do
390
+ @set.set_record_id.should be_nil
391
+ end
392
+ end
393
+
394
+ describe '#find' do
395
+ before :each do
396
+ @set = supplejack_set
397
+ Supplejack::UserSet.stub(:new) { @set }
398
+ end
399
+
400
+ it 'fetches the set from the api' do
401
+ Supplejack::UserSet.should_receive(:get).with('/sets/123abc')
402
+ Supplejack::UserSet.find('123abc')
403
+ end
404
+
405
+ it 'initializes a UserSet object' do
406
+ Supplejack::UserSet.should_receive(:new).with({id: '123abc', name: 'Dogs', count: 0}).and_return(@set)
407
+ set = Supplejack::UserSet.find('123abc')
408
+ set.class.should eq Supplejack::UserSet
409
+ end
410
+
411
+ it 'initializes the UserSet and sets the user api_key' do
412
+ set = Supplejack::UserSet.find('123abc', '98765')
413
+ set.api_key.should eq '98765'
414
+ end
415
+
416
+ it 'raises a Supplejack::SetNotFound' do
417
+ Supplejack::UserSet.stub(:get).and_raise(RestClient::ResourceNotFound)
418
+ expect { Supplejack::UserSet.find('123') }.to raise_error(Supplejack::SetNotFound)
419
+ end
420
+ end
421
+
422
+ describe '#public_sets' do
423
+ before :each do
424
+ Supplejack::UserSet.stub(:get) { {'sets' => [{'id' => '123', 'name' => 'Dog'}]} }
425
+ end
426
+
427
+ it 'fetches the public sets from the api' do
428
+ Supplejack::UserSet.should_receive(:get).with('/sets/public', {page: 1, per_page: 100})
429
+ Supplejack::UserSet.public_sets
430
+ end
431
+
432
+ it 'returns an array of user set objects' do
433
+ @set = supplejack_set
434
+ Supplejack::UserSet.should_receive(:new).once.with({'id' => '123', 'name' => 'Dog'}) { @set }
435
+ sets = Supplejack::UserSet.public_sets
436
+ sets.should be_a Array
437
+ sets.size.should eq 1
438
+ end
439
+
440
+ it 'sends pagination information' do
441
+ Supplejack::UserSet.should_receive(:get).with('/sets/public', {page: 2, per_page: 100})
442
+ Supplejack::UserSet.public_sets(page: 2)
443
+ end
444
+ end
445
+
446
+ describe '#homepage_sets' do
447
+ before :each do
448
+ Supplejack::UserSet.stub(:get) { {'sets' => [{'id' => '123', 'name' => 'Dog'}]} }
449
+ end
450
+
451
+ it 'fetches the public sets from the api' do
452
+ Supplejack::UserSet.should_receive(:get).with('/sets/home')
453
+ Supplejack::UserSet.homepage_sets
454
+ end
455
+
456
+ it 'returns an array of user set objects' do
457
+ @set = supplejack_set
458
+ Supplejack::UserSet.should_receive(:new).once.with({'id' => '123', 'name' => 'Dog'}) { @set }
459
+ sets = Supplejack::UserSet.homepage_sets
460
+ sets.should be_a Array
461
+ sets.size.should eq 1
462
+ end
463
+ end
464
+ end
465
+ end