Class: Bluecap::Identify

Inherits:
Object
  • Object
show all
Defined in:
lib/bluecap/handlers/identify.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Identify

Initialize an Identify handler.

name - A String to uniquely identify the user from the source system.



10
11
12
# File 'lib/bluecap/handlers/identify.rb', line 10

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/bluecap/handlers/identify.rb', line 4

def name
  @name
end

Instance Method Details

#handleObject

Returns an id to track a user in Bluecap, creating an id if one does not already exist.

Bluecap::Identify.new('Andy').handle
# => 1

Bluecap::Identify.new('Evelyn').handle
# => 2

Returns the Integer id.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/bluecap/handlers/identify.rb', line 24

def handle
  id = Bluecap.redis.hget('user.map', @name)
  return id.to_i if id

  id = Bluecap.redis.incr('user.ids')
  if Bluecap.redis.hsetnx('user.map', @name, id)
    return id
  else
    # Race condition, another process has set the id for this user.
    # The unused id shouldn't cause inaccuracies in reporting since
    # the position will be 0 in all bitsets across the system.
    return redis.hget('user.map', @name).to_i
  end
end