Module: Split::Helper

Defined in:
lib/split/helper.rb

Instance Method Summary collapse

Instance Method Details

#ab_test(experiment_name, *alternatives) ⇒ Object



3
4
5
6
7
8
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
# File 'lib/split/helper.rb', line 3

def ab_test(experiment_name, *alternatives)
  begin
    experiment = Split::Experiment.find_or_create(experiment_name, *alternatives)
    if experiment.winner
      ret = experiment.winner.name
    else
      if forced_alternative = override(experiment.name, alternatives)
        ret = forced_alternative
      else
        begin_experiment(experiment, experiment.control.name) if exclude_visitor?

        if ab_user[experiment.key]
          ret = ab_user[experiment.key]
        else
          alternative = experiment.next_alternative
          alternative.increment_participation
          begin_experiment(experiment, alternative.name)
          ret = alternative.name
        end
      end
    end
  rescue Errno::ECONNREFUSED => e
    raise unless Split.configuration.db_failover
    Split.configuration.db_failover_on_db_error.call(e)
    ret = Hash === (a0 = alternatives.first) ? a0.keys.first : a0
  end
  if block_given?
    if defined?(capture) # a block in a rails view
      block = Proc.new { yield(ret) }
      concat(capture(ret, &block))
      false
    else
      yield(ret)
    end
  else
    ret
  end
end

#ab_userObject



63
64
65
# File 'lib/split/helper.rb', line 63

def ab_user
  session[:split] ||= {}
end

#begin_experiment(experiment, alternative_name) ⇒ Object



59
60
61
# File 'lib/split/helper.rb', line 59

def begin_experiment(experiment, alternative_name)
  ab_user[experiment.key] = alternative_name
end

#exclude_visitor?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/split/helper.rb', line 67

def exclude_visitor?
  is_robot? or is_ignored_ip_address?
end

#finished(experiment_name, options = {:reset => true}) ⇒ Object



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

def finished(experiment_name, options = {:reset => true})
  return if exclude_visitor?
  return unless (experiment = Split::Experiment.find(experiment_name))
  if alternative_name = ab_user[experiment.key]
    alternative = Split::Alternative.new(alternative_name, experiment_name)
    alternative.increment_completion
    session[:split].delete(experiment_name) if options[:reset]
  end
rescue Errno::ECONNREFUSED => e
  raise unless Split.configuration.db_failover
  Split.configuration.db_failover_on_db_error.call(e)
end

#is_ignored_ip_address?Boolean

Returns:

  • (Boolean)


75
76
77
78
79
80
81
# File 'lib/split/helper.rb', line 75

def is_ignored_ip_address?
  if Split.configuration.ignore_ip_addresses.any?
    Split.configuration.ignore_ip_addresses.include?(request.ip)
  else
    false
  end
end

#is_robot?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/split/helper.rb', line 71

def is_robot?
  request.user_agent =~ Split.configuration.robot_regex
end

#override(experiment_name, alternatives) ⇒ Object



55
56
57
# File 'lib/split/helper.rb', line 55

def override(experiment_name, alternatives)
  params[experiment_name] if defined?(params) && alternatives.include?(params[experiment_name])
end