amico 2.0.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,8 @@
1
+ # 2.0.1
2
+
3
+ * Added `Amico.all(id, type, scope)` to retrieve all of the individuals for a given id, type (e.g. following) and scope. Thanks to @mettadore for the initial code and inspiration.
4
+ * Clarified parameters in following, followers, blocked, reciprocated, and pending calls.
5
+
1
6
  # 2.0.0
2
7
 
3
8
  * Added `Amico.default_scope_key` and `scope` parameter to all of the methods to allow you to scope the calls to express relationships for different types of things
data/README.md CHANGED
@@ -383,7 +383,7 @@ Amico.following?(1, 11, 'user')
383
383
  Amico.following(1)
384
384
  => ["11"]
385
385
 
386
- Amico.following(1, Amico.default_options, 'user')
386
+ Amico.following(1, {:page_size => Amico.page_size, :page => 1}, 'user')
387
387
  => ["11"]
388
388
 
389
389
  Amico.following?(1, 11, 'project')
@@ -394,10 +394,24 @@ Amico.follow(1, 11, 'project')
394
394
  Amico.following?(1, 11, 'project')
395
395
  => true
396
396
 
397
- Amico.following(1, Amico.default_options, 'project')
397
+ Amico.following(1, {:page_size => Amico.page_size, :page => 1}, 'project')
398
398
  => ["11"]
399
399
  ```
400
400
 
401
+ You can retrieve all of a particular type of relationship using the `all(id, type, scope)` call. For example:
402
+
403
+ ```ruby
404
+ Amico.follow(1, 11)
405
+ => nil
406
+ Amico.follow(1, 12)
407
+ => nil
408
+ Amico.all(1, :following)
409
+ => ["12", "11"]
410
+ ```
411
+
412
+ `type` can be one of :following, :followers, :blocked, :reciprocated, :pending. Use this with caution
413
+ as there may potentially be a large number of items that could be returned from this call.
414
+
401
415
  ## Documentation
402
416
 
403
417
  The source for the [relationships module](https://github.com/agoragames/amico/blob/master/lib/amico/relationships.rb) is well-documented. There are some
@@ -6,7 +6,7 @@ module Amico
6
6
  #
7
7
  # @param from_id [String] The ID of the individual establishing the follow relationship.
8
8
  # @param to_id [String] The ID of the individual to be followed.
9
- # @param scope [String] Scope for the call
9
+ # @param scope [String] Scope for the call.
10
10
  #
11
11
  # Examples
12
12
  #
@@ -29,7 +29,7 @@ module Amico
29
29
  #
30
30
  # @param from_id [String] The ID of the individual removing the follow relationship.
31
31
  # @param to_id [String] The ID of the individual to be unfollowed.
32
- # @param scope [String] Scope for the call
32
+ # @param scope [String] Scope for the call.
33
33
  #
34
34
  # Examples
35
35
  #
@@ -52,7 +52,7 @@ module Amico
52
52
  #
53
53
  # @param from_id [String] The ID of the individual blocking the relationship.
54
54
  # @param to_id [String] The ID of the individual being blocked.
55
- # @param scope [String] Scope for the call
55
+ # @param scope [String] Scope for the call.
56
56
  #
57
57
  # Examples
58
58
  #
@@ -76,7 +76,7 @@ module Amico
76
76
  #
77
77
  # @param from_id [String] The ID of the individual unblocking the relationship.
78
78
  # @param to_id [String] The ID of the blocked individual.
79
- # @param scope [String] Scope for the call
79
+ # @param scope [String] Scope for the call.
80
80
  #
81
81
  # Examples
82
82
  #
@@ -92,7 +92,7 @@ module Amico
92
92
  #
93
93
  # @param from_id [String] The ID of the individual accepting the relationship.
94
94
  # @param to_id [String] The ID of the individual to be accepted.
95
- # @param scope [String] Scope for the call
95
+ # @param scope [String] Scope for the call.
96
96
  #
97
97
  # Example
98
98
  #
@@ -110,7 +110,7 @@ module Amico
110
110
  # Count the number of individuals that someone is following.
111
111
  #
112
112
  # @param id [String] ID of the individual to retrieve following count for.
113
- # @param scope [String] Scope for the call
113
+ # @param scope [String] Scope for the call.
114
114
  #
115
115
  # Examples
116
116
  #
@@ -125,7 +125,7 @@ module Amico
125
125
  # Count the number of individuals that are following someone.
126
126
  #
127
127
  # @param id [String] ID of the individual to retrieve followers count for.
128
- # @param scope [String] Scope for the call
128
+ # @param scope [String] Scope for the call.
129
129
  #
130
130
  # Examples
131
131
  #
@@ -140,7 +140,7 @@ module Amico
140
140
  # Count the number of individuals that someone has blocked.
141
141
  #
142
142
  # @param id [String] ID of the individual to retrieve blocked count for.
143
- # @param scope [String] Scope for the call
143
+ # @param scope [String] Scope for the call.
144
144
  #
145
145
  # Examples
146
146
  #
@@ -155,7 +155,7 @@ module Amico
155
155
  # Count the number of individuals that have reciprocated a following relationship.
156
156
  #
157
157
  # @param id [String] ID of the individual to retrieve reciprocated following count for.
158
- # @param scope [String] Scope for the call
158
+ # @param scope [String] Scope for the call.
159
159
  #
160
160
  # Examples
161
161
  #
@@ -171,7 +171,7 @@ module Amico
171
171
  # Count the number of relationships pending for an individual.
172
172
  #
173
173
  # @param id [String] ID of the individual to retrieve pending count for.
174
- # @param scope [String] Scope for the call
174
+ # @param scope [String] Scope for the call.
175
175
  #
176
176
  # Examples
177
177
  #
@@ -188,7 +188,7 @@ module Amico
188
188
  #
189
189
  # @param id [String] ID of the individual checking the following status.
190
190
  # @param following_id [String] ID of the individual to see if they are being followed by id.
191
- # @param scope [String] Scope for the call
191
+ # @param scope [String] Scope for the call.
192
192
  #
193
193
  # Examples
194
194
  #
@@ -204,7 +204,7 @@ module Amico
204
204
  #
205
205
  # @param id [String] ID of the individual checking the follower status.
206
206
  # @param following_id [String] ID of the individual to see if they are following id.
207
- # @param scope [String] Scope for the call
207
+ # @param scope [String] Scope for the call.
208
208
  #
209
209
  # Examples
210
210
  #
@@ -220,7 +220,7 @@ module Amico
220
220
  #
221
221
  # @param id [String] ID of the individual checking the blocked status.
222
222
  # @param blocked_id [String] ID of the individual to see if they are blocked by id.
223
- # @param scope [String] Scope for the call
223
+ # @param scope [String] Scope for the call.
224
224
  #
225
225
  # Examples
226
226
  #
@@ -236,7 +236,7 @@ module Amico
236
236
  #
237
237
  # @param from_id [String] ID of the individual checking the reciprocated relationship.
238
238
  # @param to_id [String] ID of the individual to see if they are following from_id.
239
- # @param scope [String] Scope for the call
239
+ # @param scope [String] Scope for the call.
240
240
  #
241
241
  # Examples
242
242
  #
@@ -253,7 +253,7 @@ module Amico
253
253
  #
254
254
  # @param from_id [String] ID of the individual checking the pending relationships.
255
255
  # @param to_id [String] ID of the individual to see if they are pending a follow from from_id.
256
- # @param scope [String] Scope for the call
256
+ # @param scope [String] Scope for the call.
257
257
  #
258
258
  # Examples
259
259
  #
@@ -268,8 +268,8 @@ module Amico
268
268
  # Retrieve a page of followed individuals for a given ID.
269
269
  #
270
270
  # @param id [String] ID of the individual.
271
- # @param options [Hash] Options to be passed for retrieving a page of followed individuals.
272
- # @param scope [String] Scope for the call
271
+ # @param page_options [Hash] Options to be passed for retrieving a page of followed individuals.
272
+ # @param scope [String] Scope for the call.
273
273
  #
274
274
  # Examples
275
275
  #
@@ -278,15 +278,15 @@ module Amico
278
278
  # Amico.following(1, :page => 1)
279
279
  #
280
280
  # @return a page of followed individuals for a given ID.
281
- def following(id, options = default_options, scope = Amico.default_scope_key)
282
- members("#{Amico.namespace}:#{Amico.following_key}:#{scope}:#{id}", options)
281
+ def following(id, page_options = default_paging_options, scope = Amico.default_scope_key)
282
+ members("#{Amico.namespace}:#{Amico.following_key}:#{scope}:#{id}", page_options)
283
283
  end
284
284
 
285
285
  # Retrieve a page of followers for a given ID.
286
286
  #
287
287
  # @param id [String] ID of the individual.
288
- # @param options [Hash] Options to be passed for retrieving a page of followers.
289
- # @param scope [String] Scope for the call
288
+ # @param page_options [Hash] Options to be passed for retrieving a page of followers.
289
+ # @param scope [String] Scope for the call.
290
290
  #
291
291
  # Examples
292
292
  #
@@ -295,15 +295,15 @@ module Amico
295
295
  # Amico.followers(1, :page => 1)
296
296
  #
297
297
  # @return a page of followers for a given ID.
298
- def followers(id, options = default_options, scope = Amico.default_scope_key)
299
- members("#{Amico.namespace}:#{Amico.followers_key}:#{scope}:#{id}", options)
298
+ def followers(id, page_options = default_paging_options, scope = Amico.default_scope_key)
299
+ members("#{Amico.namespace}:#{Amico.followers_key}:#{scope}:#{id}", page_options)
300
300
  end
301
301
 
302
302
  # Retrieve a page of blocked individuals for a given ID.
303
303
  #
304
304
  # @param id [String] ID of the individual.
305
- # @param options [Hash] Options to be passed for retrieving a page of blocked individuals.
306
- # @param scope [String] Scope for the call
305
+ # @param page_options [Hash] Options to be passed for retrieving a page of blocked individuals.
306
+ # @param scope [String] Scope for the call.
307
307
  #
308
308
  # Examples
309
309
  #
@@ -312,15 +312,15 @@ module Amico
312
312
  # Amico.blocked(1, :page => 1)
313
313
  #
314
314
  # @return a page of blocked individuals for a given ID.
315
- def blocked(id, options = default_options, scope = Amico.default_scope_key)
316
- members("#{Amico.namespace}:#{Amico.blocked_key}:#{scope}:#{id}", options)
315
+ def blocked(id, page_options = default_paging_options, scope = Amico.default_scope_key)
316
+ members("#{Amico.namespace}:#{Amico.blocked_key}:#{scope}:#{id}", page_options)
317
317
  end
318
318
 
319
319
  # Retrieve a page of individuals that have reciprocated a follow for a given ID.
320
320
  #
321
321
  # @param id [String] ID of the individual.
322
- # @param options [Hash] Options to be passed for retrieving a page of individuals that have reciprocated a follow.
323
- # @param scope [String] Scope for the call
322
+ # @param page_options [Hash] Options to be passed for retrieving a page of individuals that have reciprocated a follow.
323
+ # @param scope [String] Scope for the call.
324
324
  #
325
325
  # Examples
326
326
  #
@@ -331,15 +331,15 @@ module Amico
331
331
  # Amico.reciprocated(1, :page => 1)
332
332
  #
333
333
  # @return a page of individuals that have reciprocated a follow for a given ID.
334
- def reciprocated(id, options = default_options, scope = Amico.default_scope_key)
335
- members("#{Amico.namespace}:#{Amico.reciprocated_key}:#{scope}:#{id}", options)
334
+ def reciprocated(id, page_options = default_paging_options, scope = Amico.default_scope_key)
335
+ members("#{Amico.namespace}:#{Amico.reciprocated_key}:#{scope}:#{id}", page_options)
336
336
  end
337
337
 
338
338
  # Retrieve a page of pending relationships for a given ID.
339
339
  #
340
340
  # @param id [String] ID of the individual.
341
- # @param options [Hash] Options to be passed for retrieving a page of pending relationships.
342
- # @param scope [String] Scope for the call
341
+ # @param page_options [Hash] Options to be passed for retrieving a page of pending relationships.
342
+ # @param scope [String] Scope for the call.
343
343
  #
344
344
  # Examples
345
345
  #
@@ -348,15 +348,15 @@ module Amico
348
348
  # Amico.pending(1, :page => 1)
349
349
  #
350
350
  # @return a page of pending relationships for a given ID.
351
- def pending(id, options = default_options, scope = Amico.default_scope_key)
352
- members("#{Amico.namespace}:#{Amico.pending_key}:#{scope}:#{id}", options)
351
+ def pending(id, page_options = default_paging_options, scope = Amico.default_scope_key)
352
+ members("#{Amico.namespace}:#{Amico.pending_key}:#{scope}:#{id}", page_options)
353
353
  end
354
354
 
355
355
  # Count the number of pages of following relationships for an individual.
356
356
  #
357
357
  # @param id [String] ID of the individual.
358
358
  # @param page_size [int] Page size.
359
- # @param scope [String] Scope for the call
359
+ # @param scope [String] Scope for the call.
360
360
  #
361
361
  # Examples
362
362
  #
@@ -373,7 +373,7 @@ module Amico
373
373
  #
374
374
  # @param id [String] ID of the individual.
375
375
  # @param page_size [int] Page size (default: Amico.page_size).
376
- # @param scope [String] Scope for the call
376
+ # @param scope [String] Scope for the call.
377
377
  #
378
378
  # Examples
379
379
  #
@@ -390,7 +390,7 @@ module Amico
390
390
  #
391
391
  # @param id [String] ID of the individual.
392
392
  # @param page_size [int] Page size (default: Amico.page_size).
393
- # @param scope [String] Scope for the call
393
+ # @param scope [String] Scope for the call.
394
394
  #
395
395
  # Examples
396
396
  #
@@ -407,7 +407,7 @@ module Amico
407
407
  #
408
408
  # @param id [String] ID of the individual.
409
409
  # @param page_size [int] Page size (default: Amico.page_size).
410
- # @param scope [String] Scope for the call
410
+ # @param scope [String] Scope for the call.
411
411
  #
412
412
  # Examples
413
413
  #
@@ -426,7 +426,7 @@ module Amico
426
426
  #
427
427
  # @param id [String] ID of the individual.
428
428
  # @param page_size [int] Page size (default: Amico.page_size).
429
- # @param scope [String] Scope for the call
429
+ # @param scope [String] Scope for the call.
430
430
  #
431
431
  # Examples
432
432
  #
@@ -437,14 +437,26 @@ module Amico
437
437
  # @return the number of pages of pending relationships for an individual.
438
438
  def pending_page_count(id, page_size = Amico.page_size, scope = Amico.default_scope_key)
439
439
  total_pages("#{Amico.namespace}:#{Amico.pending_key}:#{scope}:#{id}", page_size)
440
- end
440
+ end
441
+
442
+ # Retrieve all of the individuals for a given id, type (e.g. following) and scope
443
+ #
444
+ # @param id [String] ID of the individual.
445
+ # @param type [Symbol] One of :following, :followers, :blocked, :reciprocated, :pending
446
+ # @param scope [String] Scope for the call.
447
+ def all(id, type, scope = Amico.default_scope_key)
448
+ valid_types = [:following, :followers, :blocked, :reciprocated, :pending]
449
+ raise "Must be one of #{valid_types.to_s}" if !valid_types.include?(type)
450
+ count = self.send("#{type.to_s}_count".to_sym, id, scope)
451
+ count > 0 ? self.send("#{type}", id, {:page_size => count}, scope) : []
452
+ end
441
453
 
442
454
  private
443
455
 
444
- # Default options for doing, for example, paging.
456
+ # Default paging options.
445
457
  #
446
- # @return a hash of the default options.
447
- def default_options
458
+ # @return a hash of the default paging options.
459
+ def default_paging_options
448
460
  {:page_size => Amico.page_size, :page => 1}
449
461
  end
450
462
 
@@ -484,8 +496,8 @@ module Amico
484
496
  # @param options [Hash] Default options for paging.
485
497
  #
486
498
  # @return a page of items from a Redis sorted set without scores.
487
- def members(key, options = default_options)
488
- options = default_options.dup.merge!(options)
499
+ def members(key, options = default_paging_options)
500
+ options = default_paging_options.dup.merge!(options)
489
501
  if options[:page] < 1
490
502
  options[:page] = 1
491
503
  end
@@ -1,3 +1,3 @@
1
1
  module Amico
2
- VERSION = '2.0.0'
2
+ VERSION = '2.0.1'
3
3
  end
@@ -405,6 +405,83 @@ describe Amico::Relationships do
405
405
  end
406
406
  end
407
407
 
408
+ describe '#all' do
409
+ it 'should raise an exception if passing an invalid type' do
410
+ lambda {Amico.all(1, :unknown)}.should raise_error
411
+ end
412
+
413
+ it 'should return the correct list when calling all for various types' do
414
+ add_reciprocal_followers(5)
415
+
416
+ [:following, :followers, :reciprocated].each do |type|
417
+ list = Amico.all(1, type)
418
+ # It is 29, not 30, since you cannot follow yourself
419
+ list.length.should be(4)
420
+ end
421
+ end
422
+
423
+ it 'should return the correct list when calling all for a pending relationship' do
424
+ Amico.pending_follow = true
425
+ add_reciprocal_followers(5)
426
+
427
+ [:following, :followers, :reciprocated].each do |type|
428
+ list = Amico.all(1, type)
429
+ list.length.should be(0)
430
+ end
431
+
432
+ pending_list = Amico.all(1, :pending)
433
+ pending_list.length.should be(4)
434
+
435
+ Amico.pending_follow = false
436
+ end
437
+
438
+ it 'should return the correct list when calling all for a blocked relationship' do
439
+ add_reciprocal_followers(5, true)
440
+
441
+ [:following, :followers, :reciprocated].each do |type|
442
+ list = Amico.all(1, type)
443
+ list.length.should be(0)
444
+ end
445
+
446
+ blocked_list = Amico.all(1, :blocked)
447
+ blocked_list.length.should be(4)
448
+ end
449
+ end
450
+
451
+ describe '#following with options and scope' do
452
+ it 'should allow you to pass in an empty set of options that will use the default options' do
453
+ add_reciprocal_followers(5)
454
+
455
+ following = Amico.following(1, {}, Amico.default_scope_key)
456
+ following.length.should be(4)
457
+ end
458
+
459
+ it 'should allow you to pass in options that will override the option in the default options' do
460
+ add_reciprocal_followers(5)
461
+
462
+ following = Amico.following(1, {:page_size => 1}, Amico.default_scope_key)
463
+ following.length.should be(1)
464
+ end
465
+
466
+ it 'should allow you to pass in an empty set of options that will use the default options with a custom scope' do
467
+ Amico.default_scope_key = 'friends'
468
+ add_reciprocal_followers(5)
469
+
470
+ following = Amico.following(1, {}, 'friends')
471
+ following.length.should be(4)
472
+ Amico.default_scope_key = 'default'
473
+ end
474
+
475
+ it 'should allow you to pass in options that will override the option in the default options with a custom scope' do
476
+ Amico.default_scope_key = 'friends'
477
+ add_reciprocal_followers(5)
478
+
479
+ following = Amico.following(1, {:page_size => 1}, 'friends')
480
+ following.length.should be(1)
481
+ Amico.default_scope_key = 'default'
482
+ end
483
+ end
484
+
408
485
  private
409
486
 
410
487
  def add_reciprocal_followers(count = 26, block_relationship = false)
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Amico::VERSION' do
4
+ it 'should be the correct version' do
5
+ Amico::VERSION.should == '2.0.1'
6
+ end
7
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: amico
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-28 00:00:00.000000000 Z
12
+ date: 2012-03-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: redis
16
- requirement: &2153496040 !ruby/object:Gem::Requirement
16
+ requirement: &2152963800 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2153496040
24
+ version_requirements: *2152963800
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &2153495620 !ruby/object:Gem::Requirement
27
+ requirement: &2152963380 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2153495620
35
+ version_requirements: *2152963380
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &2153495200 !ruby/object:Gem::Requirement
38
+ requirement: &2152962960 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2153495200
46
+ version_requirements: *2152962960
47
47
  description: Relationships (e.g. friendships) backed by Redis
48
48
  email:
49
49
  - dczarnecki@agoragames.com
@@ -66,6 +66,7 @@ files:
66
66
  - lib/amico/version.rb
67
67
  - spec/amico/configuration_spec.rb
68
68
  - spec/amico/relationships_spec.rb
69
+ - spec/amico/version_spec.rb
69
70
  - spec/spec_helper.rb
70
71
  homepage: https://github.com/agoragames/amico
71
72
  licenses: []
@@ -81,7 +82,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
81
82
  version: '0'
82
83
  segments:
83
84
  - 0
84
- hash: 1543039261316922594
85
+ hash: 1022229374045319322
85
86
  required_rubygems_version: !ruby/object:Gem::Requirement
86
87
  none: false
87
88
  requirements:
@@ -90,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
90
91
  version: '0'
91
92
  segments:
92
93
  - 0
93
- hash: 1543039261316922594
94
+ hash: 1022229374045319322
94
95
  requirements: []
95
96
  rubyforge_project: amico
96
97
  rubygems_version: 1.8.10
@@ -100,4 +101,5 @@ summary: Relationships (e.g. friendships) backed by Redis
100
101
  test_files:
101
102
  - spec/amico/configuration_spec.rb
102
103
  - spec/amico/relationships_spec.rb
104
+ - spec/amico/version_spec.rb
103
105
  - spec/spec_helper.rb