Class: Abnormal

Inherits:
Object
  • Object
show all
Defined in:
lib/abnormal.rb,
lib/abnormal/version.rb

Constant Summary collapse

VERSION =
'0.0.0'
MAJOR_VERSION =
'0'

Class Method Summary collapse

Class Method Details

.ab_test(identity, test_name, alternatives, conversions) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/abnormal.rb', line 9

def self.ab_test(identity, test_name, alternatives, conversions)
  conversions = [conversions]  unless conversions.is_a? Array

  test_id = Digest::MD5.hexdigest(test_name)
  db['tests'].update(
    {:name => test_name, :_id => test_id},
    {
      :$set => {
        :alternatives => alternatives,
      },
      :$addToSet => {
        :conversions => {:$each => conversions}
      }
    },
    :upsert => true
  )

  conversions.each do |conversion|
    db['participations'].update(
      {
        :participant => identity,
        :test_id => test_id,
        :conversion => conversion
      },
      {
        :$set => {:conversions => 0}
      },
      :upsert => true
    )
  end

  chose_alternative(identity, test_name, alternatives)
end

.chose_alternative(identity, test_name, alternatives) ⇒ Object



76
77
78
79
80
# File 'lib/abnormal.rb', line 76

def self.chose_alternative(identity, test_name, alternatives)
  alternatives_array = normalize_alternatives(alternatives)
  index = Digest::MD5.hexdigest(test_name + identity).to_i(16) % alternatives_array.size
  alternatives_array[index]
end

.convert!(identity, conversion) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/abnormal.rb', line 43

def self.convert!(identity, conversion)
  db['participations'].update(
    {
      :participant => identity,
      :conversion => conversion
    },
    {
      :$inc => {:conversions => 1}
    },
    :multi => true
  )
end

.dbObject



4
# File 'lib/abnormal.rb', line 4

def self.db; @@db; end

.db=(db) ⇒ Object



5
6
7
# File 'lib/abnormal.rb', line 5

def self.db=(db)
  @@db = db
end

.get_participation(id, test_name, conversion) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/abnormal.rb', line 64

def self.get_participation(id, test_name, conversion)
  db['participations'].find_one(
    :participant => id,
    :test_id => Digest::MD5.hexdigest(test_name),
    :conversion => conversion
  )
end

.get_test(test_id) ⇒ Object



56
57
58
# File 'lib/abnormal.rb', line 56

def self.get_test(test_id)
  db['tests'].find_one(:_id => test_id)
end

.normalize_alternatives(alternatives) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/abnormal.rb', line 82

def self.normalize_alternatives(alternatives)
  case alternatives
  when Array
    alternatives
  when Hash
    alternatives_array = []
    idx = 0
    alternatives.each{|k,v| alternatives_array.fill(k, idx, v); idx += v}
    alternatives_array
  when Range
    alternatives.to_a
  end
end

.participationsObject



72
73
74
# File 'lib/abnormal.rb', line 72

def self.participations
  db['participations'].find.to_a
end

.testsObject



60
61
62
# File 'lib/abnormal.rb', line 60

def self.tests
  db['tests'].find.to_a
end