diff --git a/lib/ruby_optimize/common_controllers_and_helpers.rb b/lib/ruby_optimize/common_controllers_and_helpers.rb index 60d476f..88e34f0 100644 --- a/lib/ruby_optimize/common_controllers_and_helpers.rb +++ b/lib/ruby_optimize/common_controllers_and_helpers.rb @@ -4,7 +4,7 @@ def ruby_optimize(versions, **params) @ruby_optimize = {} if @ruby_optimize.nil? scope = params[:scope] || :default raise "RubyOptimize - scope already defined: #{scope.inspect}" if @ruby_optimize.has_key?(scope) - @ruby_optimize[scope] = AbTestHandler.new(versions, scope, request.user_agent, params[:cookie_expiration], params[:version_for_crawler]) + @ruby_optimize[scope] = AbTestHandler.new(cookies, versions, scope, request.user_agent, params[:domain], params[:cookie_expiration], params[:version_for_crawler]) end end end diff --git a/lib/ruby_optimize/helpers/action_view_extension.rb b/lib/ruby_optimize/helpers/action_view_extension.rb index 40e6762..36fb647 100644 --- a/lib/ruby_optimize/helpers/action_view_extension.rb +++ b/lib/ruby_optimize/helpers/action_view_extension.rb @@ -2,12 +2,6 @@ module RubyOptimize module ActionViewExtension include CommonControllersAndHelpers - def ruby_optimize_init(scope=:default) - raise "RubyOptimize - A/B test not initialized" if @ruby_optimize.nil? - raise "RubyOptimize - scope not found: #{scope.inspect}" if !@ruby_optimize.has_key?(scope) - @ruby_optimize[scope].init_script - end - def ruby_optimize_wrap(*version_and_scope, **params, &block) scope = version_and_scope[1] || :default raise "RubyOptimize - A/B test not initialized" if @ruby_optimize.nil? diff --git a/lib/ruby_optimize/models/ab_test_handler.rb b/lib/ruby_optimize/models/ab_test_handler.rb index ba58125..f0e5763 100644 --- a/lib/ruby_optimize/models/ab_test_handler.rb +++ b/lib/ruby_optimize/models/ab_test_handler.rb @@ -12,41 +12,36 @@ class AbTestHandler 'y!j-asr', 'AddThis' ].join('|')}/i - attr_reader :init_script - - def initialize(some_versions, scope, agent, a_cookie_expiration=nil, a_version_for_crawler=nil) + def initialize(cookies, some_versions, scope, agent, domain=nil, cookie_expiration=nil, a_version_for_crawler=nil) @versions = some_versions validate_versions validate_scope(scope) - @cookie_name = "ruby-optimize-cookie-#{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 - @js_object_id = SecureRandom.uuid - setup_init_script + return if is_crawler + if cookies.has_key?(cookie_name) + @version = cookies[cookie_name].to_sym + else + @version = versions.sample + cookies[cookie_name] = { + value: version, + expires: cookie_expiration || 180.days, + domain: domain || :all, + } + end end - def wrap(html, version, for_crawler) + def wrap(html, a_version, for_crawler) raise "RubyOptimize - for_crawler must be a boolean: #{for_crawler.inspect}" if for_crawler != !!for_crawler if is_crawler return html.html_safe if for_crawler - return html.html_safe if !version_for_crawler.nil? && version == version_for_crawler + return html.html_safe if !version_for_crawler.nil? && a_version == version_for_crawler return '' end - raise "RubyOptimize - version must be one of the available versions: #{version.inspect}" if !version.nil? && !versions.include?(version) - return '' if !version.present? - id = SecureRandom.uuid - wrapped = <<-HTML -
- - HTML - wrapped.html_safe + raise "RubyOptimize - version must be one of the available versions: #{a_version.inspect}" if !a_version.nil? && !versions.include?(a_version) + (a_version === version) ? html.html_safe : '' end private @@ -62,61 +57,10 @@ def validate_scope(scope) raise "RubyOptimize - scope needs to be an alphanumeric symbol: #{scope.inspect}" if !scope.is_a?(Symbol) || ALPHANUMERIC_STRING.match(scope.to_s).nil? end - def validate_cookie_expiration - raise "RubyOptimize - cookie_expiration needs to be an integer greater than zero: #{cookie_expiration.inspect}" if cookie_expiration <= 0 - end - def validate_version_for_crawler raise "RubyOptimize - version_for_crawler must be one of the available versions: #{version_for_crawler.inspect}" if !version_for_crawler.nil? && !versions.include?(version_for_crawler) end - def setup_init_script - if is_crawler - @init_script = '' - return - end - js_object_id_for_class_name = "RubyOptimize#{js_object_id.gsub('-', '')}" - @init_script = <<-HTML - - HTML - @init_script = init_script.html_safe - end - - attr_reader :cookie_expiration, :cookie_name, :version_for_crawler, :is_crawler, :js_object_id, :versions + attr_reader :version_for_crawler, :version, :is_crawler, :versions end end