ab_tester 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'http://rubygems.org'
2
+
3
+ group :development do
4
+ gem 'jeweler'
5
+ gem 'rcov'
6
+ end
@@ -0,0 +1,17 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.6.0)
6
+ bundler (~> 1.0.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ rake (0.9.0)
10
+ rcov (0.9.9)
11
+
12
+ PLATFORMS
13
+ ruby
14
+
15
+ DEPENDENCIES
16
+ jeweler
17
+ rcov
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Dalton Pinto
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,64 @@
1
+ # ABTester
2
+
3
+ A stupid simple gem that helps to do very simple AB Tests with Rails 3 pages and emails.
4
+
5
+ ## Install
6
+
7
+ Criando um projeto do zero:
8
+
9
+ gem install ab_tester
10
+
11
+ ## Usage
12
+
13
+ On controller or mailer
14
+
15
+ class SomeController < ApplicationController
16
+ ab_tester 'choice_a' => 3, 'choice_b' => 2, 'choice_c' => 6
17
+
18
+ protected
19
+ def identity_hash
20
+ # Some way to identity uniquely a user, put something on session, cookie, or simple use some data from somewhere
21
+ current_user.id
22
+ end
23
+ end
24
+
25
+ On view:
26
+
27
+ <p>Choice selected:</p>
28
+ <% ab_choice 'choice_a' %>
29
+ Choice A
30
+ <% end %>
31
+ <% ab_choice 'choice_a' %>
32
+ Choice B
33
+ <% end %>
34
+ <% ab_choice 'choice_a' %>
35
+ Choice C
36
+ <% end %>
37
+
38
+ It's possible to get data on controller using choices:
39
+
40
+ def index
41
+ ab_choice 'all_data' do
42
+ @users = User.all
43
+ end
44
+
45
+ ab_choice 'some_users' do
46
+ @users = User.page(params[:page])
47
+ end
48
+ end
49
+
50
+ ## Statistics
51
+
52
+ rake ab_tester:stats
53
+
54
+ ## Reseting Stats
55
+
56
+ rake ab_tester:reset
57
+
58
+ ## Dumping Data
59
+
60
+ rake ab_tester:dump
61
+
62
+ ## TODO
63
+
64
+ * Tests, shame on me!
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "ab_tester"
18
+ gem.homepage = "http://github.com/efqdalton/ab_tester"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{A simple gem for AB Tests}
21
+ gem.description = %Q{Yet another AB Test gem, but this supports AbstractController, not only ActionController}
22
+ gem.email = "dalthon@aluno.ita.br"
23
+ gem.authors = ["Dalton Pinto"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ require 'rcov/rcovtask'
36
+ Rcov::RcovTask.new do |test|
37
+ test.libs << 'test'
38
+ test.pattern = 'test/**/test_*.rb'
39
+ test.verbose = true
40
+ test.rcov_opts << '--exclude "gems/*"'
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "ab_tester #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,58 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{ab_tester}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Dalton Pinto"]
12
+ s.date = %q{2011-06-01}
13
+ s.description = %q{Yet another AB Test gem, but this supports AbstractController, not only ActionController}
14
+ s.email = %q{dalthon@aluno.ita.br}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.mdown"
18
+ ]
19
+ s.files = [
20
+ "Gemfile",
21
+ "Gemfile.lock",
22
+ "LICENSE.txt",
23
+ "README.mdown",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "ab_tester.gemspec",
27
+ "lib/ab_choice.rb",
28
+ "lib/ab_tester.rb",
29
+ "lib/ab_tester/choice_options.rb",
30
+ "lib/helpers/ab_choice_helper.rb",
31
+ "lib/rails/generators/a_b_tester/a_b_tester_generator.rb",
32
+ "lib/rails/generators/a_b_tester/templates/create_ab_choices.rb",
33
+ "lib/tasks/ab_tester.rake",
34
+ "test/helper.rb",
35
+ "test/test_ab_tester.rb"
36
+ ]
37
+ s.homepage = %q{http://github.com/efqdalton/ab_tester}
38
+ s.licenses = ["MIT"]
39
+ s.require_paths = ["lib"]
40
+ s.rubygems_version = %q{1.6.2}
41
+ s.summary = %q{A simple gem for AB Tests}
42
+
43
+ if s.respond_to? :specification_version then
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
47
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
48
+ s.add_development_dependency(%q<rcov>, [">= 0"])
49
+ else
50
+ s.add_dependency(%q<jeweler>, [">= 0"])
51
+ s.add_dependency(%q<rcov>, [">= 0"])
52
+ end
53
+ else
54
+ s.add_dependency(%q<jeweler>, [">= 0"])
55
+ s.add_dependency(%q<rcov>, [">= 0"])
56
+ end
57
+ end
58
+
@@ -0,0 +1,18 @@
1
+ class ABChoice < ActiveRecord::Base
2
+ scope :succeed, where(:success => true )
3
+ scope :unsucceed, where(:success => false)
4
+
5
+ def success!
6
+ unless success?
7
+ update_attributes :success => true, :succeeded_at => DateTime.now
8
+ end
9
+ end
10
+
11
+ def self.succeed(identity_hash)
12
+ self.find_by_identity_hash(identity_hash).success!
13
+ end
14
+
15
+ def self.reset!
16
+ self.destroy_all
17
+ end
18
+ end
@@ -0,0 +1,56 @@
1
+ require 'rails'
2
+
3
+ autoload :ABChoice, 'ab_choice'
4
+ autoload :ABChoiceHelper, 'helpers/ab_choice_helper'
5
+
6
+ ActionView::Base.class_eval do
7
+ include ABChoiceHelper
8
+ end
9
+
10
+ module ABTester
11
+ autoload :ChoiceOptions, 'ab_tester/choice_options'
12
+
13
+ def find_or_create_choice(choice_value = self.class.ab_options.shuffle)
14
+ ab_choice = ABChoice.find_or_create_by_identity_hash identity_hash
15
+ ab_choice.update_attribute(:choice, @selected_choice_value || choice_value) unless ab_choice.choice
16
+ ab_choice
17
+ end
18
+
19
+ def choose(selected_choice_value)
20
+ @selected_choice_value = selected_choice_value
21
+ end
22
+
23
+ def ab_choice(selected_choice_value, &block)
24
+ @ab_choice ||= find_or_create_choice
25
+
26
+ if @ab_choice.choice == selected_choice_value.to_s
27
+ yield
28
+ else
29
+ nil
30
+ end
31
+ end
32
+
33
+ module ClassMethods
34
+ def ab_test(options_hash)
35
+ @ab_options = ChoiceOptions.new options_hash
36
+ end
37
+
38
+ def ab_options
39
+ @ab_options or raise "Choices definitions missing! Call at_test method on controller!"
40
+ end
41
+ end
42
+
43
+ def self.included(base)
44
+ base.send :extend, ABTester::ClassMethods
45
+ base.send :protected, :ab_choice
46
+ base.send :helper_method, :choose
47
+ base.send :helper_method, :identity_hash
48
+ base.send :helper_method, :find_or_create_choice
49
+ end
50
+
51
+ class Railtie < Rails::Railtie
52
+ rake_tasks do
53
+ load "tasks/ab_tester.rake"
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,16 @@
1
+ class ABTester::ChoiceOptions
2
+ def initialize(options_hash)
3
+ @total_rank = 0
4
+ @ranked_keys = []
5
+
6
+ options_hash.each do |key, value|
7
+ @total_rank += value
8
+ @ranked_keys << [key, @total_rank]
9
+ end
10
+ end
11
+
12
+ def shuffle
13
+ random_value = rand @total_rank
14
+ @ranked_keys.find{ |key, rank| random_value < rank }[0]
15
+ end
16
+ end
@@ -0,0 +1,11 @@
1
+ module ABChoiceHelper
2
+ def ab_choice(selected_choice_value, &block)
3
+ @ab_choice ||= find_or_create_choice
4
+
5
+ if @ab_choice.choice == selected_choice_value.to_s
6
+ capture &block
7
+ else
8
+ nil
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,23 @@
1
+ require 'rails/generators'
2
+
3
+ class ABTesterGenerator < Rails::Generators::Base
4
+ include Rails::Generators::Migration
5
+
6
+ def self.source_root
7
+ File.join(File.dirname(__FILE__), 'templates')
8
+ end
9
+
10
+ def self.next_migration_number(dirname) #:nodoc:
11
+ migration_number_attempt = Time.now.utc.strftime("%Y%m%d%H%M%S")
12
+
13
+ if ActiveRecord::Base.timestamped_migrations && migration_number_attempt > current_migration_number(dirname).to_s
14
+ migration_number_attempt
15
+ else
16
+ serial_migration_number(dirname)
17
+ end
18
+ end
19
+
20
+ def copy_migration
21
+ migration_template 'create_ab_choices.rb', 'db/migrate/create_ab_choices.rb'
22
+ end
23
+ end
@@ -0,0 +1,15 @@
1
+ class CreateAbChoices < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :ab_choices do |t|
4
+ t.string :identity_hash
5
+ t.boolean :success, :default => false
6
+ t.string :choice
7
+ t.datetime :succeeded_at
8
+ t.timestamps
9
+ end
10
+ end
11
+
12
+ def self.down
13
+ drop_table :ab_choices
14
+ end
15
+ end
@@ -0,0 +1,26 @@
1
+ namespace :ab_tester do
2
+ desc "Generate Statistics"
3
+ task :stats => :environment do
4
+ all_successful_choices = ABChoice.group(:choice).where(:success => true).count
5
+ all_choices = ABChoice.group(:choice).count
6
+
7
+ max_choice_char_size = all_choices.keys.map{ |c| c.to_s.size }.max + 1
8
+
9
+ puts "Choices:"
10
+ all_choices.each do |choice, all_count|
11
+ puts format(" %-#{max_choice_char_size}s: %2.2f (%d/%d)", choice, all_successful_choices[choice].to_f*100/all_count, all_successful_choices[choice], all_count)
12
+ end
13
+ end
14
+
15
+ desc "I cant believe that this description is required if you want to know what it does!"
16
+ task :reset => :environment do
17
+ ABChoice.reset!
18
+ end
19
+
20
+ desc "Outputs a tsv file that has all data"
21
+ task :dump => :environment do
22
+ ABChoice.all.each do |c|
23
+ puts [c.identity_hash, c.success, c.choice, c.succeeded_at, c.created_at].join("\t")
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'ab_tester'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestAbTester < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ab_tester
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Dalton Pinto
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-06-01 00:00:00 -03:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ prerelease: false
23
+ type: :development
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ name: jeweler
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ prerelease: false
37
+ type: :development
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ name: rcov
48
+ version_requirements: *id002
49
+ description: Yet another AB Test gem, but this supports AbstractController, not only ActionController
50
+ email: dalthon@aluno.ita.br
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files:
56
+ - LICENSE.txt
57
+ - README.mdown
58
+ files:
59
+ - Gemfile
60
+ - Gemfile.lock
61
+ - LICENSE.txt
62
+ - README.mdown
63
+ - Rakefile
64
+ - VERSION
65
+ - ab_tester.gemspec
66
+ - lib/ab_choice.rb
67
+ - lib/ab_tester.rb
68
+ - lib/ab_tester/choice_options.rb
69
+ - lib/helpers/ab_choice_helper.rb
70
+ - lib/rails/generators/a_b_tester/a_b_tester_generator.rb
71
+ - lib/rails/generators/a_b_tester/templates/create_ab_choices.rb
72
+ - lib/tasks/ab_tester.rake
73
+ - test/helper.rb
74
+ - test/test_ab_tester.rb
75
+ has_rdoc: true
76
+ homepage: http://github.com/efqdalton/ab_tester
77
+ licenses:
78
+ - MIT
79
+ post_install_message:
80
+ rdoc_options: []
81
+
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ hash: 3
90
+ segments:
91
+ - 0
92
+ version: "0"
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ hash: 3
99
+ segments:
100
+ - 0
101
+ version: "0"
102
+ requirements: []
103
+
104
+ rubyforge_project:
105
+ rubygems_version: 1.6.2
106
+ signing_key:
107
+ specification_version: 3
108
+ summary: A simple gem for AB Tests
109
+ test_files: []
110
+