Module: Amico::Relationships

Included in:
Amico
Defined in:
lib/amico/relationships.rb

Instance Method Summary collapse

Instance Method Details

#accept(from_id, to_id, scope = Amico.default_scope_key) ⇒ Object

Accept a relationship that is pending between two IDs.

Example

Amico.follow(1, 11)
Amico.pending?(1, 11) # true
Amico.accept(1, 11)
Amico.pending?(1, 11) # false
Amico.following?(1, 11) #true

Parameters:

  • from_id (String)

    The ID of the individual accepting the relationship.

  • to_id (String)

    The ID of the individual to be accepted.

  • scope (String) (defaults to: Amico.default_scope_key)

    Scope for the call.



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

Parameters:

  • id (String)

    ID of the individual.

  • type (Symbol)

    One of :following, :followers, :blocked, :reciprocated, :pending.

  • scope (String) (defaults to: Amico.default_scope_key)

    Scope for the call.



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)

Parameters:

  • from_id (String)

    The ID of the individual blocking the relationship.

  • to_id (String)

    The ID of the individual being blocked.

  • scope (String) (defaults to: Amico.default_scope_key)

    Scope for the call.



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

Retrieve a page of blocked individuals for a given ID.

Examples

Amico.block(1, 11)
Amico.block(1, 12)
Amico.blocked(1, :page => 1)

Parameters:

  • id (String)

    ID of the individual.

  • page_options (Hash) (defaults to: default_paging_options)

    Options to be passed for retrieving a page of blocked individuals.

  • scope (String) (defaults to: Amico.default_scope_key)

    Scope for the call.

Returns:

  • a page of blocked individuals for a given ID.



315
316
317
# File 'lib/amico/relationships.rb', line 315

def blocked(id, page_options = default_paging_options, scope = Amico.default_scope_key)
  members("#{Amico.namespace}:#{Amico.blocked_key}:#{scope}:#{id}", page_options)
end

#blocked?(id, blocked_id, scope = Amico.default_scope_key) ⇒ Boolean

Check to see if one individual has blocked another individual.

Examples

Amico.block(1, 11)
Amico.blocked?(1, 11)

Parameters:

  • id (String)

    ID of the individual checking the blocked status.

  • blocked_id (String)

    ID of the individual to see if they are blocked by id.

  • scope (String) (defaults to: Amico.default_scope_key)

    Scope for the call.

Returns:

  • (Boolean)

    true if id has blocked blocked_id, false otherwise



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

Count the number of individuals that someone has blocked.

Examples

Amico.block(1, 11)
Amico.blocked_count(1)

Parameters:

  • id (String)

    ID of the individual to retrieve blocked count for.

  • scope (String) (defaults to: Amico.default_scope_key)

    Scope for the call.

Returns:

  • the count of the number of individuals that someone has blocked.



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

Count the number of pages of blocked relationships for an individual.

Examples

Amico.block(1, 11)
Amico.block(1, 12)
Amico.blocked_page_count(1)

Parameters:

  • id (String)

    ID of the individual.

  • page_size (int) (defaults to: Amico.page_size)

    Page size (default: Amico.page_size).

  • scope (String) (defaults to: Amico.default_scope_key)

    Scope for the call.

Returns:

  • the number of pages of blocked relationships for an individual.



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.

Parameters:

  • id (String)

    ID of the individual.

  • type (Symbol)

    One of :following, :followers, :blocked, :reciprocated, :pending.

  • scope (String) (defaults to: Amico.default_scope_key)

    Scope for the call.

Returns:

  • 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)

Parameters:

  • from_id (String)

    The ID of the individual establishing the follow relationship.

  • to_id (String)

    The ID of the individual to be followed.

  • scope (String) (defaults to: Amico.default_scope_key)

    Scope for the call.



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

Check to see if one individual is a follower of another individual.

Examples

Amico.follow(11, 1)
Amico.follower?(1, 11)

Parameters:

  • id (String)

    ID of the individual checking the follower status.

  • following_id (String)

    ID of the individual to see if they are following id.

  • scope (String) (defaults to: Amico.default_scope_key)

    Scope for the call.

Returns:

  • (Boolean)

    true if follower_id is following id, false otherwise



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

Retrieve a page of followers for a given ID.

Examples

Amico.follow(11, 1)
Amico.follow(12, 1)
Amico.followers(1, :page => 1)

Parameters:

  • id (String)

    ID of the individual.

  • page_options (Hash) (defaults to: default_paging_options)

    Options to be passed for retrieving a page of followers.

  • scope (String) (defaults to: Amico.default_scope_key)

    Scope for the call.

Returns:

  • a page of followers for a given ID.



298
299
300
# File 'lib/amico/relationships.rb', line 298

def followers(id, page_options = default_paging_options, scope = Amico.default_scope_key)
  members("#{Amico.namespace}:#{Amico.followers_key}:#{scope}:#{id}", page_options)
end

#followers_count(id, scope = Amico.default_scope_key) ⇒ Object

Count the number of individuals that are following someone.

Examples

Amico.follow(11, 1)
Amico.followers_count(1)

Parameters:

  • id (String)

    ID of the individual to retrieve followers count for.

  • scope (String) (defaults to: Amico.default_scope_key)

    Scope for the call.

Returns:

  • the count of the number of individuals that are following someone.



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

Count the number of pages of follower relationships for an individual.

Examples

Amico.follow(11, 1)
Amico.follow(12, 1)
Amico.followers_page_count(1)

Parameters:

  • id (String)

    ID of the individual.

  • page_size (int) (defaults to: Amico.page_size)

    Page size (default: Amico.page_size).

  • scope (String) (defaults to: Amico.default_scope_key)

    Scope for the call.

Returns:

  • the number of pages of follower relationships for an individual.



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

Retrieve a page of followed individuals for a given ID.

Examples

Amico.follow(1, 11)
Amico.follow(1, 12)
Amico.following(1, :page => 1)

Parameters:

  • id (String)

    ID of the individual.

  • page_options (Hash) (defaults to: default_paging_options)

    Options to be passed for retrieving a page of followed individuals.

  • scope (String) (defaults to: Amico.default_scope_key)

    Scope for the call.

Returns:

  • a page of followed individuals for a given ID.



281
282
283
# File 'lib/amico/relationships.rb', line 281

def following(id, page_options = default_paging_options, scope = Amico.default_scope_key)
  members("#{Amico.namespace}:#{Amico.following_key}:#{scope}:#{id}", page_options)
end

#following?(id, following_id, scope = Amico.default_scope_key) ⇒ Boolean

Check to see if one individual is following another individual.

Examples

Amico.follow(1, 11)
Amico.following?(1, 11)

Parameters:

  • id (String)

    ID of the individual checking the following status.

  • following_id (String)

    ID of the individual to see if they are being followed by id.

  • scope (String) (defaults to: Amico.default_scope_key)

    Scope for the call.

Returns:

  • (Boolean)

    true if id is following following_id, false otherwise



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

Count the number of individuals that someone is following.

Examples

Amico.follow(1, 11)
Amico.following_count(1)

Parameters:

  • id (String)

    ID of the individual to retrieve following count for.

  • scope (String) (defaults to: Amico.default_scope_key)

    Scope for the call.

Returns:

  • the count of the number of individuals that someone is following.



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

Count the number of pages of following relationships for an individual.

Examples

Amico.follow(1, 11)
Amico.follow(1, 12)
Amico.following_page_count(1)

Parameters:

  • id (String)

    ID of the individual.

  • page_size (int) (defaults to: Amico.page_size)

    Page size.

  • scope (String) (defaults to: Amico.default_scope_key)

    Scope for the call.

Returns:

  • the number of pages of following relationships for an individual.



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.

Parameters:

  • id (String)

    ID of the individual.

  • type (Symbol)

    One of :following, :followers, :blocked, :reciprocated, :pending.

  • page_size (int) (defaults to: Amico.page_size)

    Page size (default: Amico.page_size).

  • scope (String) (defaults to: Amico.default_scope_key)

    Scope for the call.

Returns:

  • 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

Retrieve a page of pending relationships for a given ID.

Examples

Amico.follow(1, 11)
Amico.follow(2, 11)
Amico.pending(1, :page => 1)

Parameters:

  • id (String)

    ID of the individual.

  • page_options (Hash) (defaults to: default_paging_options)

    Options to be passed for retrieving a page of pending relationships.

  • scope (String) (defaults to: Amico.default_scope_key)

    Scope for the call.

Returns:

  • a page of pending relationships for a given ID.



351
352
353
# File 'lib/amico/relationships.rb', line 351

def pending(id, page_options = default_paging_options, scope = Amico.default_scope_key)
  members("#{Amico.namespace}:#{Amico.pending_key}:#{scope}:#{id}", page_options)
end

#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.

Examples

Amico.follow(1, 11)
Amico.pending?(1, 11) # true

Parameters:

  • from_id (String)

    ID of the individual checking the pending relationships.

  • to_id (String)

    ID of the individual to see if they are pending a follow from from_id.

  • scope (String) (defaults to: Amico.default_scope_key)

    Scope for the call.

Returns:

  • (Boolean)

    true if the relationship is pending, false otherwise



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

Count the number of relationships pending for an individual.

Examples

Amico.follow(11, 1)
Amico.follow(12, 1)
Amico.pending_count(1) # 2

Parameters:

  • id (String)

    ID of the individual to retrieve pending count for.

  • scope (String) (defaults to: Amico.default_scope_key)

    Scope for the call.

Returns:

  • the count of the number of relationships pending for an individual.



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

Count the number of pages of pending relationships for an individual.

Examples

Amico.follow(11, 1)
Amico.follow(12, 1)
Amico.pending_page_count(1) # 1

Parameters:

  • id (String)

    ID of the individual.

  • page_size (int) (defaults to: Amico.page_size)

    Page size (default: Amico.page_size).

  • scope (String) (defaults to: Amico.default_scope_key)

    Scope for the call.

Returns:

  • the number of pages of pending relationships for an individual.



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

Retrieve a page of individuals that have reciprocated a follow for a given ID.

Examples

Amico.follow(1, 11)
Amico.follow(1, 12)
Amico.follow(11, 1)
Amico.follow(12, 1)
Amico.reciprocated(1, :page => 1)

Parameters:

  • id (String)

    ID of the individual.

  • page_options (Hash) (defaults to: default_paging_options)

    Options to be passed for retrieving a page of individuals that have reciprocated a follow.

  • scope (String) (defaults to: Amico.default_scope_key)

    Scope for the call.

Returns:

  • a page of individuals that have reciprocated a follow for a given ID.



334
335
336
# File 'lib/amico/relationships.rb', line 334

def reciprocated(id, page_options = default_paging_options, scope = Amico.default_scope_key)
  members("#{Amico.namespace}:#{Amico.reciprocated_key}:#{scope}:#{id}", page_options)
end

#reciprocated?(from_id, to_id, scope = Amico.default_scope_key) ⇒ Boolean

Check to see if one individual has reciprocated in following another individual.

Examples

Amico.follow(1, 11)
Amico.follow(11, 1)
Amico.reciprocated?(1, 11)

Parameters:

  • from_id (String)

    ID of the individual checking the reciprocated relationship.

  • to_id (String)

    ID of the individual to see if they are following from_id.

  • scope (String) (defaults to: Amico.default_scope_key)

    Scope for the call.

Returns:

  • (Boolean)

    true if both individuals are following each other, false otherwise



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

Count the number of individuals that have reciprocated a following relationship.

Examples

Amico.follow(1, 11)
Amico.follow(11, 1)
Amico.reciprocated_count(1)

Parameters:

  • id (String)

    ID of the individual to retrieve reciprocated following count for.

  • scope (String) (defaults to: Amico.default_scope_key)

    Scope for the call.

Returns:

  • the count of the number of individuals that have reciprocated a following relationship.



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

Count the number of pages of reciprocated relationships for an individual.

Examples

Amico.follow(1, 11)
Amico.follow(1, 12)
Amico.follow(11, 1)
Amico.follow(12, 1)
Amico.reciprocated_page_count(1)

Parameters:

  • id (String)

    ID of the individual.

  • page_size (int) (defaults to: Amico.page_size)

    Page size (default: Amico.page_size).

  • scope (String) (defaults to: Amico.default_scope_key)

    Scope for the call.

Returns:

  • the number of pages of reciprocated relationships for an individual.



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

Unblock a relationship between two IDs.

Examples

Amico.block(1, 11)
Amico.unblock(1, 11)

Parameters:

  • from_id (String)

    The ID of the individual unblocking the relationship.

  • to_id (String)

    The ID of the blocked individual.

  • scope (String) (defaults to: Amico.default_scope_key)

    Scope for the call.



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

Remove a follow relationship between two IDs. After removing the follow relationship, if a reciprocated relationship was established, it is also removed.

Examples

Amico.follow(1, 11)
Amico.unfollow(1, 11)

Parameters:

  • from_id (String)

    The ID of the individual removing the follow relationship.

  • to_id (String)

    The ID of the individual to be unfollowed.

  • scope (String) (defaults to: Amico.default_scope_key)

    Scope for the call.



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