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.



113
114
115
116
117
# File 'lib/amico/relationships.rb', line 113

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, :reciprocated, :blocked, :blocked_by, :pending, :pending_with.

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

    Scope for the call.



634
635
636
637
638
# File 'lib/amico/relationships.rb', line 634

def all(id, type, scope = Amico.default_scope_key)
   validate_relationship_type(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.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/amico/relationships.rb', line 64

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.zrem("#{Amico.namespace}:#{Amico.pending_with_key}:#{scope}:#{to_id}", from_id)
 		Amico.redis.zadd("#{Amico.namespace}:#{Amico.blocked_key}:#{scope}:#{from_id}", Time.now.to_i, to_id)
 		Amico.redis.zadd("#{Amico.namespace}:#{Amico.blocked_by_key}:#{scope}:#{to_id}", Time.now.to_i, from_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.



434
435
436
# File 'lib/amico/relationships.rb', line 434

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



318
319
320
# File 'lib/amico/relationships.rb', line 318

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_by(id, page_options = default_paging_options, scope = Amico.default_scope_key) ⇒ Object

Retrieve a page of individuals who have blocked a given ID.

Examples

Amico.block(11, 1)
Amico.block(12, 1)
Amico.blocked_by(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 blocking individuals.

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

    Scope for the call.

Returns:

  • a page of individuals who have blocked a given ID.



451
452
453
# File 'lib/amico/relationships.rb', line 451

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

#blocked_by?(id, blocked_by_id, scope = Amico.default_scope_key) ⇒ Boolean

Check to see if one individual is blocked by another individual.

Examples

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

Parameters:

  • id (String)

    ID of the individual checking the blocked by status.

  • blocked_id (String)

    ID of the individual to see if they have blocked id.

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

    Scope for the call.

Returns:

  • (Boolean)

    true if id is blocked by blocked_by_id, false otherwise



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

def blocked_by?(id, blocked_by_id, scope = Amico.default_scope_key)
  !Amico.redis.zscore("#{Amico.namespace}:#{Amico.blocked_by_key}:#{scope}:#{id}", blocked_by_id).nil?
end

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

Count the number of individuals blocking another.

Examples

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

Parameters:

  • id (String)

    ID of the individual to retrieve blocked_by count for.

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

    Scope for the call.

Returns:

  • the count of the number of individuals blocking someone.



222
223
224
# File 'lib/amico/relationships.rb', line 222

def blocked_by_count(id, scope = Amico.default_scope_key)
  Amico.redis.zcard("#{Amico.namespace}:#{Amico.blocked_by_key}:#{scope}:#{id}")
end

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

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

Examples

Amico.block(11, 1)
Amico.block(12, 1)
Amico.blocked_by_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_by relationships for an individual.



572
573
574
# File 'lib/amico/relationships.rb', line 572

def blocked_by_page_count(id, page_size = Amico.page_size, scope = Amico.default_scope_key)
  total_pages("#{Amico.namespace}:#{Amico.blocked_by_key}:#{scope}:#{id}", page_size)
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.



207
208
209
# File 'lib/amico/relationships.rb', line 207

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.



555
556
557
# File 'lib/amico/relationships.rb', line 555

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

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

Clears all relationships (in either direction) stored for an individual. Helpful to prevent orphaned associations when deleting users.

Examples

Amico.follow(1, 11)
Amico.followers_count(11)  => 1
Amico.clear(1)
Amico.followers_count(11)  => 0

Parameters:

  • id (String)

    ID of the individual to clear info for.

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

    Scope for the call.



153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/amico/relationships.rb', line 153

def clear(id, scope = Amico.default_scope_key)
  # no longer following (or followed by) anyone
  clear_bidirectional_sets_for_id(id, Amico.following_key, Amico.followers_key, scope)
  clear_bidirectional_sets_for_id(id, Amico.followers_key, Amico.following_key, scope)
  clear_bidirectional_sets_for_id(id, Amico.reciprocated_key, Amico.reciprocated_key, scope)
  # no longer blocked by (or blocking) anyone
  clear_bidirectional_sets_for_id(id, Amico.blocked_by_key, Amico.blocked_key, scope)
  clear_bidirectional_sets_for_id(id, Amico.blocked_key, Amico.blocked_by_key, scope)
  # no longer pending with anyone (or have any pending followers)
  clear_bidirectional_sets_for_id(id, Amico.pending_with_key, Amico.pending_key, scope)
  clear_bidirectional_sets_for_id(id, Amico.pending_key, Amico.pending_with_key, scope)
end

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

Returns Count of all of a given type of relationship for the specified id.

Parameters:

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



647
648
649
650
# File 'lib/amico/relationships.rb', line 647

def count(id, type, scope = Amico.default_scope_key)
   validate_relationship_type(type)
   self.send("#{type.to_s}_count".to_sym, id, scope)
end

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

Deny a relationship that is pending between two IDs.

Example

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

Parameters:

  • from_id (String)

    The ID of the individual denying the relationship.

  • to_id (String)

    The ID of the individual to be denied.

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

    Scope for the call.



132
133
134
135
136
137
138
139
# File 'lib/amico/relationships.rb', line 132

def deny(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.pending_key}:#{scope}:#{to_id}", from_id)
 		Amico.redis.zrem("#{Amico.namespace}:#{Amico.pending_with_key}:#{scope}:#{from_id}", to_id)
   end
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
25
26
27
# 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)

  if Amico.pending_follow
     Amico.redis.multi do |transaction|
       transaction.zadd("#{Amico.namespace}:#{Amico.pending_key}:#{scope}:#{to_id}", Time.now.to_i, from_id)
       transaction.zadd("#{Amico.namespace}:#{Amico.pending_with_key}:#{scope}:#{from_id}", Time.now.to_i, to_id)
     end
  else
     add_following_followers_reciprocated(from_id, to_id, scope)
  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



302
303
304
# File 'lib/amico/relationships.rb', line 302

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.



417
418
419
# File 'lib/amico/relationships.rb', line 417

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.



192
193
194
# File 'lib/amico/relationships.rb', line 192

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.



538
539
540
# File 'lib/amico/relationships.rb', line 538

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.



400
401
402
# File 'lib/amico/relationships.rb', line 400

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



286
287
288
# File 'lib/amico/relationships.rb', line 286

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.



177
178
179
# File 'lib/amico/relationships.rb', line 177

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.



521
522
523
# File 'lib/amico/relationships.rb', line 521

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

Returns Page count of a given type of relationship for the specified id.

Parameters:

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



660
661
662
663
# File 'lib/amico/relationships.rb', line 660

def page_count(id, type, page_size = Amico.page_size, scope = Amico.default_scope_key)
  validate_relationship_type(type)
  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(11, 1)
Amico.follow(12, 1)
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.



487
488
489
# File 'lib/amico/relationships.rb', line 487

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



367
368
369
# File 'lib/amico/relationships.rb', line 367

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.



254
255
256
# File 'lib/amico/relationships.rb', line 254

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.



608
609
610
# File 'lib/amico/relationships.rb', line 608

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

#pending_with(id, page_options = default_paging_options, scope = Amico.default_scope_key) ⇒ Object

Retrieve a page of individuals that are waiting to approve the given ID.

Examples

Amico.follow(1, 11)
Amico.follow(1, 12)
Amico.pending_with(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 individuals that are waiting to approve the given ID.



504
505
506
# File 'lib/amico/relationships.rb', line 504

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

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

Check to see if one individual has a pending relationship with another.

Examples

Amico.follow(1, 11)
Amico.pending_with?(11, 1) # 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 an approval 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



383
384
385
# File 'lib/amico/relationships.rb', line 383

def pending_with?(from_id, to_id, scope = Amico.default_scope_key)
  !Amico.redis.zscore("#{Amico.namespace}:#{Amico.pending_with_key}:#{scope}:#{to_id}", from_id).nil?
end

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

Count the number of relationships an individual has pending with another.

Examples

Amico.follow(1, 11)
Amico.follow(1, 12)
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 an individual has pending with another.



270
271
272
# File 'lib/amico/relationships.rb', line 270

def pending_with_count(id, scope = Amico.default_scope_key)
  Amico.redis.zcard("#{Amico.namespace}:#{Amico.pending_with_key}:#{scope}:#{id}")
end

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

Count the number of pages of individuals waiting to approve another individual.

Examples

Amico.follow(1, 11)
Amico.follow(1, 12)
Amico.pending_with_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 individuals waiting to approve another individual.



625
626
627
# File 'lib/amico/relationships.rb', line 625

def pending_with_page_count(id, page_size = Amico.page_size, scope = Amico.default_scope_key)
  total_pages("#{Amico.namespace}:#{Amico.pending_with_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.



470
471
472
# File 'lib/amico/relationships.rb', line 470

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



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

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.



238
239
240
# File 'lib/amico/relationships.rb', line 238

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.



591
592
593
# File 'lib/amico/relationships.rb', line 591

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.



91
92
93
94
95
96
97
98
# File 'lib/amico/relationships.rb', line 91

def unblock(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.blocked_key}:#{scope}:#{from_id}", to_id)
		Amico.redis.zrem("#{Amico.namespace}:#{Amico.blocked_by_key}:#{scope}:#{to_id}", from_id)
	end
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.



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/amico/relationships.rb', line 41

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)
 		Amico.redis.zrem("#{Amico.namespace}:#{Amico.pending_with_key}:#{scope}:#{from_id}", to_id)
  end
end