Class: RubyOptimize::AbTestHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_optimize/models/ab_test_handler.rb

Constant Summary collapse

ALPHANUMERIC_STRING =
/\A[a-zA-Z0-9]+\z/
CRAWLER =
/#{[
  'bot', 'crawl', 'slurp', 'spider', 'Google favicon', 'Mediapartners-Google', 'java', 'wget', 'curl', 'Commons-HttpClient',
  'Python-urllib', 'libwww', 'httpunit', 'nutch', 'biglotron', 'teoma', 'convera', 'gigablast', 'ia_archiver', 'webmon',
  'httrack', 'grub.org', 'netresearchserver', 'speedy', 'fluffy', 'bibnum.bnf', 'findlink', 'panscient', 'IOI', 'ips-agent',
  'yanga', 'Voyager', 'CyberPatrol', 'postrank', 'page2rss', 'linkdex', 'ezooms', 'heritrix', 'findthatfile', 'europarchive.org',
  'Aboundex', 'summify', 'ec2linkfinder', 'facebookexternalhit', 'yeti', 'RetrevoPageAnalyzer', 'sogou', 'wotbox', 'ichiro',
  'drupact', 'coccoc', 'integromedb', 'siteexplorer.info', 'proximic', 'changedetection', 'WeSEE:SearchLipperhey SEO Service',
  'CC Metadata Scaper', 'g00g1e.net', 'binlar', 'A6-Indexer', 'ADmantX', 'MegaIndex', 'ltx71', 'BUbiNG', 'Qwantify', 'lipperhey',
  'y!j-asr', 'AddThis'
].join('|')}/i

Instance Method Summary collapse

Constructor Details

#initialize(cookies, some_versions, scope, agent, domain = nil, a_cookie_expiration = nil, a_version_for_crawler = nil, use_session_cookies = false, custom_weights = nil) ⇒ AbTestHandler

Returns a new instance of AbTestHandler.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ruby_optimize/models/ab_test_handler.rb', line 15

def initialize(cookies, some_versions, scope, agent, domain=nil, a_cookie_expiration=nil, a_version_for_crawler=nil, use_session_cookies=false, custom_weights=nil)
  @versions = some_versions
  validate_versions
  validate_scope(scope)
  @cookie_name = "ruby-optimize-cookie-#{scope}"
  @is_crawler = !CRAWLER.match(agent).nil?
  @cookie_expiration = (a_cookie_expiration || 180.days).to_i
  validate_cookie_expiration
  @version_for_crawler = a_version_for_crawler
  validate_version_for_crawler
  return if is_crawler
  if cookies.has_key?(cookie_name)
    @version = cookies[cookie_name].to_sym
  else
    @version = extract_random_version(custom_weights || {})
    cookie_hash = {
      value: version,
      domain: domain || :all,
    }
    cookie_hash[:expires] = cookie_expiration.from_now if !use_session_cookies
    cookies[cookie_name] = cookie_hash
  end
end

Instance Method Details

#show?(a_version, for_crawler) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
# File 'lib/ruby_optimize/models/ab_test_handler.rb', line 39

def show?(a_version, for_crawler)
  raise "RubyOptimize - for_crawler must be a boolean: #{for_crawler.inspect}" if for_crawler != !!for_crawler
  return for_crawler || (!version_for_crawler.nil? && a_version == version_for_crawler) if is_crawler
  raise "RubyOptimize - version must be one of the available versions: #{a_version.inspect}" if !versions.include?(a_version)
  a_version == version
end