Class: Abongo

Inherits:
Object
  • Object
show all
Defined in:
lib/abongo/sugar.rb,
lib/abongo.rb,
lib/abongo/version.rb,
lib/abongo/view_helper.rb,
lib/abongo/controller/dashboard.rb

Overview

Gives you easy syntax to use ABongo in your views.

Defined Under Namespace

Modules: Controller, Statistics, Sugar, ViewHelper

Constant Summary collapse

VERSION =
'1.0.8'
MAJOR_VERSION =
'1'
@@salt =
'Not really necessary.'

Class Method Summary collapse

Class Method Details

.bongo!(name = nil, options = {}) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/abongo.rb', line 84

def self.bongo!(name = nil, options = {})
  if name.kind_of? Array
    name.map do |single_test|
      self.bongo!(single_test, options)
    end
  else
    if name.nil?
      # Score all participating tests
      participant = Abongo.find_participant(Abongo.identity)
      participating_tests = participant['tests']
      participating_tests.each do |participating_test|
        self.bongo!(participating_test, options)
      end
    else # Could be a test name or conversion name
      tests_listening_to_conversion = Abongo.tests_listening_to_conversion(name)
      if tests_listening_to_conversion
        tests_listening_to_conversion.each do |test|
          self.score_conversion!(test)
        end
      else # No tests listening for this conversion. Assume it is just a test name
        if name.kind_of? BSON::ObjectId
          self.score_conversion!(name)
        else
          self.score_conversion!(name.to_s)
        end
      end
    end
  end
end

.dbObject



21
# File 'lib/abongo.rb', line 21

def self.db; @@db; end

.db=(db) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/abongo.rb', line 22

def self.db=(db)
  @@db = db
  @@experiments = db['abongo_experiments']
  @@conversions = db['abongo_conversions']
  @@participants = db['abongo_participants']
  @@alternatives = db['abongo_alternatives']
end

.end_experiment!(test_name, final_alternative, conversion_name = nil) ⇒ Object



202
203
204
205
206
207
# File 'lib/abongo.rb', line 202

def self.end_experiment!(test_name, final_alternative, conversion_name = nil)
  warn("conversion_name is deprecated") if conversion_name
  test = get_test(test_name)
  Abongo.experiments.update({:name => test_name}, {'$set' => { :final => final_alternative}}, :upsert => true, :safe => true)
  Abongo.conversions.update({'tests' => test['_id']}, {'$pull' => {'tests' => test['_id']}}, :multi => true)
end

.expires_in(known_human = false) ⇒ Object



134
135
136
137
138
139
140
141
142
143
# File 'lib/abongo.rb', line 134

def self.expires_in(known_human = false)
  expires_in = nil
  if (@@options[:expires_in])
    expires_in = @@options[:expires_in]
  end
  if (@@options[:count_humans_only] && @@options[:expires_in_for_bots] && !known_human)
    expires_in = @@options[:expires_in_for_bots]
  end
  expires_in
end

.flip(test_name, options = {}) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/abongo.rb', line 38

def self.flip(test_name, options = {})
  if block_given?
    yield(self.test(test_name, [true, false], options))
  else
    self.test(test_name, [true, false], options)
  end
end

.human!(identity = nil) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/abongo.rb', line 168

def self.human!(identity = nil)
  identity ||= Abongo.identity
  begin
    previous = Abongo.participants.find_and_modify({'query' => {:identity => identity}, 'update' => {'$set' => {:human => true}}, 'upsert' => true})
  rescue Mongo::OperationFailure
    Abongo.participants.update({:identity => identity}, {'$set' => {:human => true}}, :upsert => true)
    previous = Abongo.participants.find_one(:identity => identity)
  end
  
  if !previous['human'] and options[:count_humans_only]
    if options[:expires_in_for_bots] and previous['tests']
      Abongo.set_expiration(Abongo.identity, expires_in(true))
    end
    
    if previous['tests']
      previous['tests'].each do |test_id|
        test = Abongo.experiments.find_one(test_id)
        choice = Abongo.find_alternative_for_user(identity, test)
        Abongo.alternatives.update({:content => choice, :test => test_id}, {:$inc => {:participants => 1}})
        Abongo.experiments.update({:_id => test_id}, {'$inc' => {:participants => 1}})
      end
    end

    if previous['conversions']
      previous['conversions'].each do |test_id|
        test = Abongo.experiments.find_one(:_id => test_id)
        viewed_alternative = Abongo.find_alternative_for_user(identity, test)
        Abongo.alternatives.update({:content => viewed_alternative, :test => test_id}, {'$inc' => {:conversions => 1}})
        Abongo.experiments.update({:_id => test_id}, {'$inc' => {:conversions => 1}})
      end
    end
  end
end

.identityObject



34
35
36
# File 'lib/abongo.rb', line 34

def self.identity
  @@identity ||= rand(10 ** 10)
end

.identity=(new_identity) ⇒ Object



30
31
32
# File 'lib/abongo.rb', line 30

def self.identity=(new_identity)
  @@identity = new_identity.to_s
end

.is_human?(identity = nil) ⇒ Boolean

Returns:

  • (Boolean)


162
163
164
165
166
# File 'lib/abongo.rb', line 162

def self.is_human?(identity = nil)
  identity ||= Abongo.identity
  participant = Abongo.participants.find_one(:identity => identity)
  return !!(participant && participant["human"])
end

.optionsObject



14
# File 'lib/abongo.rb', line 14

def self.options; @@options; end

.options=(options) ⇒ Object



15
# File 'lib/abongo.rb', line 15

def self.options=(options); @@options = options; end

.participating_tests(only_current = true, identity = nil) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/abongo.rb', line 146

def self.participating_tests(only_current = true, identity = nil)
  identity ||= Abongo.identity
  participating_tests = (Abongo.participants.find_one({:identity => identity}) || {} )['tests']
  return {} if participating_tests.nil?
  tests_and_alternatives = participating_tests.inject({}) do |acc, test_id|
    test = Abongo.experiments.find_one(test_id)
    if !only_current or (test['final'].nil? or !test['final'])
      alternative = Abongo.find_alternative_for_user(identity, test)
      acc[test['name']] = alternative
    end
    acc
  end

  tests_and_alternatives
end

.saltObject



18
# File 'lib/abongo.rb', line 18

def self.salt; @@salt; end

.salt=(salt) ⇒ Object



19
# File 'lib/abongo.rb', line 19

def self.salt=(salt); @@salt = salt; end

.score_conversion!(test_name) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/abongo.rb', line 114

def self.score_conversion!(test_name)
  if test_name.kind_of? BSON::ObjectId
    participant = Abongo.find_participant(Abongo.identity)
    expired = participant['expires'] ? (participant['expires'] < Time.now) : false
    if options[:assume_participation] || participant['tests'].include?(test_name)
      if options[:multiple_conversions] || !participant['conversions'].include?(test_name) || expired
        Abongo.add_conversion(Abongo.identity, test_name)
        if !options[:count_humans_only] || participant['human']
          test = Abongo.experiments.find_one(:_id => test_name)
          viewed_alternative = Abongo.find_alternative_for_user(Abongo.identity, test)
          Abongo.alternatives.update({:content => viewed_alternative, :test => test['_id']}, {'$inc' => {:conversions => 1}})
          Abongo.experiments.update({:_id => test_name}, {'$inc' => {:conversions => 1}})
        end
      end
    end 
  else
    Abongo.score_conversion!(Abongo.get_test(test_name)['_id'])
  end
end

.test(test_name, alternatives, options = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/abongo.rb', line 46

def self.test(test_name, alternatives, options = {})
  # Test for short-circuit (the test has been ended)
  test = Abongo.get_test(test_name)
  return test['final'] unless test.nil? or test['final'].nil?

  # Create the test (if necessary)
  unless test
    conversion_name = options[:conversion] || options[:conversion_name]
    test = Abongo.start_experiment!(test_name, self.parse_alternatives(alternatives), conversion_name)
  end

  # Should expired be part of the find_participant?
  participant = Abongo.find_participant(Abongo.identity)
  expired = participant['expires'] ? (participant['expires'] < Time.now) : false

  choice = self.find_alternative_for_user(Abongo.identity, test)
  participating_tests = participant['tests']

  # TODO: Pull participation add out
  if options[:multiple_participation] || !participating_tests.include?(test['_id']) || expired
    unless participating_tests.include?(test['_id'])
      Abongo.add_participation(identity, test['_id'], self.expires_in(participant['human']))
    end
    
    # Small timing issue in here
    if (!@@options[:count_humans_only] || participant['human'])
      Abongo.alternatives.update({:content => choice, :test => test['_id']}, {:$inc => {:participants => 1}})
      Abongo.experiments.update({:_id => test['_id']}, {'$inc' => {:participants => 1}})
    end
  end

  if block_given?
    yield(choice)
  else
    choice
  end
end