Module: Split::Helper

Defined in:
lib/split/helper.rb

Instance Method Summary collapse

Instance Method Details

#ab_test(experiment_name, control, *alternatives) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/split/helper.rb', line 3

def ab_test(experiment_name, control, *alternatives)

  puts 'WARNING: You should always pass the control alternative through as the second argument with any other alternatives as the third because the order of the hash is not preserved in ruby 1.8' if RUBY_VERSION.match(/1\.8/) && alternatives.length.zero?
  ret = if Split.configuration.enabled
          experiment_variable(alternatives, control, experiment_name)
        else
          control_variable(control)
        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



47
48
49
# File 'lib/split/helper.rb', line 47

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

#begin_experiment(experiment, alternative_name = nil) ⇒ Object



42
43
44
45
# File 'lib/split/helper.rb', line 42

def begin_experiment(experiment, alternative_name = nil)
  alternative_name ||= experiment.control.name
  ab_user[experiment.key] = alternative_name
end

#clean_old_versions(experiment) ⇒ Object



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

def clean_old_versions(experiment)
  old_versions(experiment).each do |old_key|
    ab_user.delete old_key
  end
end

#doing_other_tests?(experiment_key) ⇒ Boolean

Returns:

  • (Boolean)


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

def doing_other_tests?(experiment_key)
  ab_user.keys.reject { |k| k == experiment_key }.length > 0
end

#exclude_visitor?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/split/helper.rb', line 51

def exclude_visitor?
  is_robot? or is_ignored_ip_address?
end

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



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/split/helper.rb', line 25

def finished(experiment_name, options = {:reset => true})
  return if exclude_visitor? or !Split.configuration.enabled
  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)


81
82
83
84
85
86
87
# File 'lib/split/helper.rb', line 81

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)


77
78
79
# File 'lib/split/helper.rb', line 77

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

#not_allowed_to_test?(experiment_key) ⇒ Boolean

Returns:

  • (Boolean)


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

def not_allowed_to_test?(experiment_key)
  !Split.configuration.allow_multiple_experiments && doing_other_tests?(experiment_key)
end

#old_versions(experiment) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/split/helper.rb', line 69

def old_versions(experiment)
  if experiment.version > 0
    ab_user.keys.select { |k| k.match(Regexp.new(experiment.name)) }.reject { |k| k == experiment.key }
  else
    []
  end
end

#override(experiment_name, alternatives) ⇒ Object



38
39
40
# File 'lib/split/helper.rb', line 38

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