Module: Amico::Relationships
- Included in:
- Amico
- Defined in:
- lib/amico/relationships.rb
Instance Method Summary collapse
-
#accept(from_id, to_id, scope = Amico.default_scope_key) ⇒ Object
Accept a relationship that is pending between two IDs.
-
#all(id, type, scope = Amico.default_scope_key) ⇒ Object
Retrieve all of the individuals for a given id, type (e.g. following) and scope.
-
#block(from_id, to_id, scope = Amico.default_scope_key) ⇒ Object
Block a relationship between two IDs.
-
#blocked(id, page_options = default_paging_options, scope = Amico.default_scope_key) ⇒ Object
Retrieve a page of blocked individuals for a given ID.
-
#blocked?(id, blocked_id, scope = Amico.default_scope_key) ⇒ Boolean
Check to see if one individual has blocked another individual.
-
#blocked_count(id, scope = Amico.default_scope_key) ⇒ Object
Count the number of individuals that someone has blocked.
-
#blocked_page_count(id, page_size = Amico.page_size, scope = Amico.default_scope_key) ⇒ Object
Count the number of pages of blocked relationships for an individual.
-
#count(id, type, scope = Amico.default_scope_key) ⇒ Object
Retrieve a count of all of a given type of relationship for the specified id.
-
#follow(from_id, to_id, scope = Amico.default_scope_key) ⇒ Object
Establish a follow relationship between two IDs.
-
#follower?(id, follower_id, scope = Amico.default_scope_key) ⇒ Boolean
Check to see if one individual is a follower of another individual.
-
#followers(id, page_options = default_paging_options, scope = Amico.default_scope_key) ⇒ Object
Retrieve a page of followers for a given ID.
-
#followers_count(id, scope = Amico.default_scope_key) ⇒ Object
Count the number of individuals that are following someone.
-
#followers_page_count(id, page_size = Amico.page_size, scope = Amico.default_scope_key) ⇒ Object
Count the number of pages of follower relationships for an individual.
-
#following(id, page_options = default_paging_options, scope = Amico.default_scope_key) ⇒ Object
Retrieve a page of followed individuals for a given ID.
-
#following?(id, following_id, scope = Amico.default_scope_key) ⇒ Boolean
Check to see if one individual is following another individual.
-
#following_count(id, scope = Amico.default_scope_key) ⇒ Object
Count the number of individuals that someone is following.
-
#following_page_count(id, page_size = Amico.page_size, scope = Amico.default_scope_key) ⇒ Object
Count the number of pages of following relationships for an individual.
-
#page_count(id, type, page_size = Amico.page_size, scope = Amico.default_scope_key) ⇒ Object
Retrieve a page count of a given type of relationship for the specified id.
-
#pending(id, page_options = default_paging_options, scope = Amico.default_scope_key) ⇒ Object
Retrieve a page of pending relationships for a given ID.
-
#pending?(from_id, to_id, scope = Amico.default_scope_key) ⇒ Boolean
Check to see if one individual has a pending relationship in following another individual.
-
#pending_count(id, scope = Amico.default_scope_key) ⇒ Object
Count the number of relationships pending for an individual.
-
#pending_page_count(id, page_size = Amico.page_size, scope = Amico.default_scope_key) ⇒ Object
Count the number of pages of pending relationships for an individual.
-
#reciprocated(id, page_options = default_paging_options, scope = Amico.default_scope_key) ⇒ Object
Retrieve a page of individuals that have reciprocated a follow for a given ID.
-
#reciprocated?(from_id, to_id, scope = Amico.default_scope_key) ⇒ Boolean
Check to see if one individual has reciprocated in following another individual.
-
#reciprocated_count(id, scope = Amico.default_scope_key) ⇒ Object
Count the number of individuals that have reciprocated a following relationship.
-
#reciprocated_page_count(id, page_size = Amico.page_size, scope = Amico.default_scope_key) ⇒ Object
Count the number of pages of reciprocated relationships for an individual.
-
#unblock(from_id, to_id, scope = Amico.default_scope_key) ⇒ Object
Unblock a relationship between two IDs.
-
#unfollow(from_id, to_id, scope = Amico.default_scope_key) ⇒ Object
Remove a follow relationship between two IDs.
Instance Method Details
#accept(from_id, to_id, scope = Amico.default_scope_key) ⇒ Object
104 105 106 107 108 |
# File 'lib/amico/relationships.rb', line 104 def accept(from_id, to_id, scope = Amico.default_scope_key) return if from_id == to_id add_following_followers_reciprocated(from_id, to_id, scope) end |
#all(id, type, scope = Amico.default_scope_key) ⇒ Object
Retrieve all of the individuals for a given id, type (e.g. following) and scope
447 448 449 450 451 452 |
# File 'lib/amico/relationships.rb', line 447 def all(id, type, scope = Amico.default_scope_key) valid_types = [:following, :followers, :blocked, :reciprocated, :pending] raise "Must be one of #{valid_types.to_s}" if !valid_types.include?(type) count = self.send("#{type.to_s}_count".to_sym, id, scope) count > 0 ? self.send("#{type}", id, {:page_size => count}, scope) : [] end |
#block(from_id, to_id, scope = Amico.default_scope_key) ⇒ Object
Block a relationship between two IDs. This method also has the side effect of removing any follower or following relationship between the two IDs.
Examples
Amico.block(1, 11)
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/amico/relationships.rb', line 60 def block(from_id, to_id, scope = Amico.default_scope_key) return if from_id == to_id Amico.redis.multi do Amico.redis.zrem("#{Amico.namespace}:#{Amico.following_key}:#{scope}:#{from_id}", to_id) Amico.redis.zrem("#{Amico.namespace}:#{Amico.following_key}:#{scope}:#{to_id}", from_id) Amico.redis.zrem("#{Amico.namespace}:#{Amico.followers_key}:#{scope}:#{to_id}", from_id) Amico.redis.zrem("#{Amico.namespace}:#{Amico.followers_key}:#{scope}:#{from_id}", to_id) Amico.redis.zrem("#{Amico.namespace}:#{Amico.reciprocated_key}:#{scope}:#{from_id}", to_id) Amico.redis.zrem("#{Amico.namespace}:#{Amico.reciprocated_key}:#{scope}:#{to_id}", from_id) Amico.redis.zrem("#{Amico.namespace}:#{Amico.pending_key}:#{scope}:#{from_id}", to_id) Amico.redis.zadd("#{Amico.namespace}:#{Amico.blocked_key}:#{scope}:#{from_id}", Time.now.to_i, to_id) end end |
#blocked(id, page_options = default_paging_options, scope = Amico.default_scope_key) ⇒ Object
315 316 317 |
# File 'lib/amico/relationships.rb', line 315 def blocked(id, = , scope = Amico.default_scope_key) members("#{Amico.namespace}:#{Amico.blocked_key}:#{scope}:#{id}", ) end |
#blocked?(id, blocked_id, scope = Amico.default_scope_key) ⇒ Boolean
231 232 233 |
# File 'lib/amico/relationships.rb', line 231 def blocked?(id, blocked_id, scope = Amico.default_scope_key) !Amico.redis.zscore("#{Amico.namespace}:#{Amico.blocked_key}:#{scope}:#{id}", blocked_id).nil? end |
#blocked_count(id, scope = Amico.default_scope_key) ⇒ Object
151 152 153 |
# File 'lib/amico/relationships.rb', line 151 def blocked_count(id, scope = Amico.default_scope_key) Amico.redis.zcard("#{Amico.namespace}:#{Amico.blocked_key}:#{scope}:#{id}") end |
#blocked_page_count(id, page_size = Amico.page_size, scope = Amico.default_scope_key) ⇒ Object
402 403 404 |
# File 'lib/amico/relationships.rb', line 402 def blocked_page_count(id, page_size = Amico.page_size, scope = Amico.default_scope_key) total_pages("#{Amico.namespace}:#{Amico.blocked_key}:#{scope}:#{id}", page_size) end |
#count(id, type, scope = Amico.default_scope_key) ⇒ Object
Retrieve a count of all of a given type of relationship for the specified id.
461 462 463 |
# File 'lib/amico/relationships.rb', line 461 def count(id, type, scope = Amico.default_scope_key) self.send("#{type.to_s}_count".to_sym, id, scope) end |
#follow(from_id, to_id, scope = Amico.default_scope_key) ⇒ Object
Establish a follow relationship between two IDs. After adding the follow relationship, it checks to see if the relationship is reciprocated and establishes that relationship if so.
Examples
Amico.follow(1, 11)
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/amico/relationships.rb', line 14 def follow(from_id, to_id, scope = Amico.default_scope_key) return if from_id == to_id return if blocked?(to_id, from_id, scope) return if Amico.pending_follow && pending?(from_id, to_id, scope) unless Amico.pending_follow add_following_followers_reciprocated(from_id, to_id, scope) else Amico.redis.zadd("#{Amico.namespace}:#{Amico.pending_key}:#{scope}:#{to_id}", Time.now.to_i, from_id) end end |
#follower?(id, follower_id, scope = Amico.default_scope_key) ⇒ Boolean
215 216 217 |
# File 'lib/amico/relationships.rb', line 215 def follower?(id, follower_id, scope = Amico.default_scope_key) !Amico.redis.zscore("#{Amico.namespace}:#{Amico.followers_key}:#{scope}:#{id}", follower_id).nil? end |
#followers(id, page_options = default_paging_options, scope = Amico.default_scope_key) ⇒ Object
298 299 300 |
# File 'lib/amico/relationships.rb', line 298 def followers(id, = , scope = Amico.default_scope_key) members("#{Amico.namespace}:#{Amico.followers_key}:#{scope}:#{id}", ) end |
#followers_count(id, scope = Amico.default_scope_key) ⇒ Object
136 137 138 |
# File 'lib/amico/relationships.rb', line 136 def followers_count(id, scope = Amico.default_scope_key) Amico.redis.zcard("#{Amico.namespace}:#{Amico.followers_key}:#{scope}:#{id}") end |
#followers_page_count(id, page_size = Amico.page_size, scope = Amico.default_scope_key) ⇒ Object
385 386 387 |
# File 'lib/amico/relationships.rb', line 385 def followers_page_count(id, page_size = Amico.page_size, scope = Amico.default_scope_key) total_pages("#{Amico.namespace}:#{Amico.followers_key}:#{scope}:#{id}", page_size) end |
#following(id, page_options = default_paging_options, scope = Amico.default_scope_key) ⇒ Object
281 282 283 |
# File 'lib/amico/relationships.rb', line 281 def following(id, = , scope = Amico.default_scope_key) members("#{Amico.namespace}:#{Amico.following_key}:#{scope}:#{id}", ) end |
#following?(id, following_id, scope = Amico.default_scope_key) ⇒ Boolean
199 200 201 |
# File 'lib/amico/relationships.rb', line 199 def following?(id, following_id, scope = Amico.default_scope_key) !Amico.redis.zscore("#{Amico.namespace}:#{Amico.following_key}:#{scope}:#{id}", following_id).nil? end |
#following_count(id, scope = Amico.default_scope_key) ⇒ Object
121 122 123 |
# File 'lib/amico/relationships.rb', line 121 def following_count(id, scope = Amico.default_scope_key) Amico.redis.zcard("#{Amico.namespace}:#{Amico.following_key}:#{scope}:#{id}") end |
#following_page_count(id, page_size = Amico.page_size, scope = Amico.default_scope_key) ⇒ Object
368 369 370 |
# File 'lib/amico/relationships.rb', line 368 def following_page_count(id, page_size = Amico.page_size, scope = Amico.default_scope_key) total_pages("#{Amico.namespace}:#{Amico.following_key}:#{scope}:#{id}", page_size) end |
#page_count(id, type, page_size = Amico.page_size, scope = Amico.default_scope_key) ⇒ Object
Retrieve a page count of a given type of relationship for the specified id.
473 474 475 |
# File 'lib/amico/relationships.rb', line 473 def page_count(id, type, page_size = Amico.page_size, scope = Amico.default_scope_key) self.send("#{type.to_s}_page_count".to_sym, id, page_size, scope) end |
#pending(id, page_options = default_paging_options, scope = Amico.default_scope_key) ⇒ Object
351 352 353 |
# File 'lib/amico/relationships.rb', line 351 def pending(id, = , scope = Amico.default_scope_key) members("#{Amico.namespace}:#{Amico.pending_key}:#{scope}:#{id}", ) end |
#pending?(from_id, to_id, scope = Amico.default_scope_key) ⇒ Boolean
264 265 266 |
# File 'lib/amico/relationships.rb', line 264 def pending?(from_id, to_id, scope = Amico.default_scope_key) !Amico.redis.zscore("#{Amico.namespace}:#{Amico.pending_key}:#{scope}:#{to_id}", from_id).nil? end |
#pending_count(id, scope = Amico.default_scope_key) ⇒ Object
183 184 185 |
# File 'lib/amico/relationships.rb', line 183 def pending_count(id, scope = Amico.default_scope_key) Amico.redis.zcard("#{Amico.namespace}:#{Amico.pending_key}:#{scope}:#{id}") end |
#pending_page_count(id, page_size = Amico.page_size, scope = Amico.default_scope_key) ⇒ Object
438 439 440 |
# File 'lib/amico/relationships.rb', line 438 def pending_page_count(id, page_size = Amico.page_size, scope = Amico.default_scope_key) total_pages("#{Amico.namespace}:#{Amico.pending_key}:#{scope}:#{id}", page_size) end |
#reciprocated(id, page_options = default_paging_options, scope = Amico.default_scope_key) ⇒ Object
334 335 336 |
# File 'lib/amico/relationships.rb', line 334 def reciprocated(id, = , scope = Amico.default_scope_key) members("#{Amico.namespace}:#{Amico.reciprocated_key}:#{scope}:#{id}", ) end |
#reciprocated?(from_id, to_id, scope = Amico.default_scope_key) ⇒ Boolean
248 249 250 |
# File 'lib/amico/relationships.rb', line 248 def reciprocated?(from_id, to_id, scope = Amico.default_scope_key) following?(from_id, to_id, scope) && following?(to_id, from_id, scope) end |
#reciprocated_count(id, scope = Amico.default_scope_key) ⇒ Object
167 168 169 |
# File 'lib/amico/relationships.rb', line 167 def reciprocated_count(id, scope = Amico.default_scope_key) Amico.redis.zcard("#{Amico.namespace}:#{Amico.reciprocated_key}:#{scope}:#{id}") end |
#reciprocated_page_count(id, page_size = Amico.page_size, scope = Amico.default_scope_key) ⇒ Object
421 422 423 |
# File 'lib/amico/relationships.rb', line 421 def reciprocated_page_count(id, page_size = Amico.page_size, scope = Amico.default_scope_key) total_pages("#{Amico.namespace}:#{Amico.reciprocated_key}:#{scope}:#{id}", page_size) end |
#unblock(from_id, to_id, scope = Amico.default_scope_key) ⇒ Object
85 86 87 88 89 |
# File 'lib/amico/relationships.rb', line 85 def unblock(from_id, to_id, scope = Amico.default_scope_key) return if from_id == to_id Amico.redis.zrem("#{Amico.namespace}:#{Amico.blocked_key}:#{scope}:#{from_id}", to_id) end |
#unfollow(from_id, to_id, scope = Amico.default_scope_key) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/amico/relationships.rb', line 38 def unfollow(from_id, to_id, scope = Amico.default_scope_key) return if from_id == to_id Amico.redis.multi do Amico.redis.zrem("#{Amico.namespace}:#{Amico.following_key}:#{scope}:#{from_id}", to_id) Amico.redis.zrem("#{Amico.namespace}:#{Amico.followers_key}:#{scope}:#{to_id}", from_id) Amico.redis.zrem("#{Amico.namespace}:#{Amico.reciprocated_key}:#{scope}:#{from_id}", to_id) Amico.redis.zrem("#{Amico.namespace}:#{Amico.reciprocated_key}:#{scope}:#{to_id}", from_id) Amico.redis.zrem("#{Amico.namespace}:#{Amico.pending_key}:#{scope}:#{to_id}", from_id) end end |