class Kissable::AB

Constants

MAX_GROUP_COUNT

Attributes

groups[R]
login[R]
ratios[R]
test_name[R]

Public Class Methods

new(test_name, groups=nil, ratios=nil) click to toggle source
# File lib/kissable/ab.rb, line 9
def initialize(test_name, groups=nil, ratios=nil)
  @test_name = test_name

  @groups = groups
  @groups ||= %w{Original Variant}

  @ratios = ratios
  @ratios ||= [100.0 / @groups.length] * @groups.length

  validate_groups
  validate_ratios
end

Public Instance Methods

cookies() click to toggle source
# File lib/kissable/ab.rb, line 32
def cookies
  @cookies ||= {}
end
group(object) click to toggle source

Assigns a test group based on the 'abid' cookie or login (email). Takes either a cookies object (Hash-like) or login (String).

# File lib/kissable/ab.rb, line 24
def group(object)
  (object.is_a? String) ? (@login = object) : (@cookies = object)

  abset.each do |i, val|
    return i if val > seed
  end
end
tracking_script(group) click to toggle source
# File lib/kissable/ab.rb, line 36
def tracking_script(group)
  "<script>_kmq.push(['set', {'#{test_name}' : '#{group}'}]);</script>"
end

Private Instance Methods

abset() click to toggle source
# File lib/kissable/ab.rb, line 61
def abset
  return @abset if @abset

  sum = 0
  @abset = {}
  @abset = groups.zip(ratios.map { |i| sum += i })
end
login_sha() click to toggle source
# File lib/kissable/ab.rb, line 57
def login_sha
  @login_sha = Digest::SHA1.hexdigest(login).to_i(16)
end
seed() click to toggle source
# File lib/kissable/ab.rb, line 50
def seed
  return @seed if @seed

  xor = @login ? login_sha : ab_cookie_value
  @seed = (sha ^ xor) % 100
end
sha() click to toggle source
# File lib/kissable/ab.rb, line 46
def sha
  @sha ||= Digest::SHA1.hexdigest(test_name).to_i(16)
end
validate_groups() click to toggle source
# File lib/kissable/ab.rb, line 108
def validate_groups
  raise ArgumentError, 'A minimium of two groups are required' if groups.length < 2
  raise ArgumentError, "The max number of split groups is #{MAX_GROUP_COUNT}" if groups.length > MAX_GROUP_COUNT
end
validate_ratios() click to toggle source
# File lib/kissable/ab.rb, line 113
def validate_ratios
  unless ratios.length == groups.length
    raise ArgumentError, 'Mismatch with groups and ratios'
  end
  total = ratios.inject(0) { |tot, rate| tot + rate.to_i }
  raise ArgumentError, "Kissable ratios sum to #{total} not 100" unless total == 100
end