Skip to content

Commit

Permalink
Move all the logic in controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Adriano di Lauro committed Oct 25, 2017
1 parent 04293c4 commit c41a5af
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 81 deletions.
2 changes: 1 addition & 1 deletion lib/ruby_optimize/common_controllers_and_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 0 additions & 6 deletions lib/ruby_optimize/helpers/action_view_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
92 changes: 18 additions & 74 deletions lib/ruby_optimize/models/ab_test_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
<div id="#{id}" style="display:none">
#{html}
</div>
<script>
window['#{js_object_id}'].handle('#{version}', '#{id}');
</script>
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
Expand All @@ -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
<script>
function #{js_object_id_for_class_name}(random) {
var n = '#{cookie_name}=', ca, v = undefined, c;
ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1, c.length);
}
if (c.indexOf(n) === 0) {
v = c.substring(n.length, c.length);
}
}
if (v === undefined) {
v = random;
var d = new Date();
d.setTime(d.getTime() + (#{cookie_expiration} * 1000));
document.cookie = '#{cookie_name}=' + v + '; expires=' + d.toGMTString() + '; path=/';
}
this.handle = function(version, id) {
var el = document.getElementById(id);
if (version === v) {
el.style.removeProperty('display');
return;
}
el.parentNode.removeChild(el);
};
};
window['#{js_object_id}'] = new #{js_object_id_for_class_name}('#{versions.sample}');
window.clearRubyOptimizeCookie#{cookie_name.split("-").last.camelcase} = function() {
document.cookie = '#{cookie_name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
};
</script>
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

0 comments on commit c41a5af

Please sign in to comment.