sixpack-client 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *~
19
+ *#*
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 SeatGeek
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # Sixpack
2
+
3
+ Ruby client library for SeatGeak's Sixpack ab testing framework.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'sixpack-client'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install sixpack-client
18
+
19
+ ## Usage
20
+
21
+ Basic example:
22
+
23
+ ```ruby
24
+ require 'sixpack'
25
+
26
+ session = Sixpack::Session.new
27
+
28
+ # Participate in a test (creates the test if necessary)
29
+ session.participate("new-test", ["alternative-1", "alternative-2"])
30
+
31
+ # Convert
32
+ session.convert("new-test")
33
+ ```
34
+
35
+ Each session has a `client_id` associated with it that must be preserved across requests. Here's what the first request might look like:
36
+
37
+ ```ruby
38
+ session = Sixpack::Session.new
39
+ session.participate("new-test", ["alternative-1", "alternative-2"])
40
+ set_cookie_in_your_web_framework("sixpack-id", session.client_id)
41
+ ```
42
+
43
+ For future requests, create the `Session` using the `client_id` stored in the cookie:
44
+
45
+ ```ruby
46
+ client_id = get_cookie_from_web_framework("sixpack-id")
47
+ session = Sixpack::Session.new client_id
48
+ session.convert("new-test")
49
+ ```
50
+
51
+ If you already have a client_id (you can generate one using `Sixpack.generate_client_id()`) you can use the `simple_participate()` and `simple_convert()` methods to avoid instantiating a `Session`:
52
+
53
+ ```ruby
54
+ Sixpack::simple_partipate("new-test", ["alternative-1", "alternative-2"], client_id)
55
+
56
+ Sixpack::simple_convert("new-test", client_id)
57
+ ```
58
+
59
+ ## Contributing
60
+
61
+ 1. Fork it
62
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
63
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
64
+ 4. Push to the branch (`git push origin my-new-feature`)
65
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require "rspec/core/rake_task"
4
+
5
+ RSpec::Core::RakeTask.new
6
+
7
+ task :default => :spec
8
+ task :test => :spec
data/lib/sixpack.rb ADDED
@@ -0,0 +1,87 @@
1
+ require "net/http"
2
+ require "json"
3
+
4
+ require "uuid"
5
+
6
+ require "sixpack/version"
7
+
8
+ module Sixpack
9
+ def self.simple_participate(experiment_name, alternatives, client_id=nil, force=nil)
10
+ session = Session.new(client_id)
11
+ res = session.participate(experiment_name, alternatives, force)
12
+ res["alternative"]
13
+ end
14
+
15
+ def self.simple_convert(experiment_name, client_id)
16
+ session = Session.new(client_id)
17
+ session.convert(experiment_name)["status"]
18
+ end
19
+
20
+ def self.generate_client_id
21
+ uuid = UUID.new
22
+ uuid.generate
23
+ end
24
+
25
+ class Session
26
+ attr_accessor :host, :port, :client_id
27
+
28
+ def initialize(client_id=nil, options={})
29
+ default_options = {:host => "localhost", :port => 5000}
30
+ options = default_options.merge(options)
31
+ @host = options[:host]
32
+ @port = options[:port]
33
+
34
+ if client_id.nil?
35
+ @client_id = Sixpack::generate_client_id()
36
+ else
37
+ @client_id = client_id
38
+ end
39
+ end
40
+
41
+ def participate(experiment_name, alternatives, force=nil)
42
+ if !(experiment_name =~ /[a-z0-9][a-z0-9\-_ ]*/)
43
+ raise ArgumentError, "Bad experiment_name"
44
+ end
45
+
46
+ if alternatives.length < 2
47
+ raise ArgumentError, "Must specify at least 2 alternatives"
48
+ end
49
+
50
+ alternatives.each { |alt|
51
+ if !(alt =~ /[a-z0-9][a-z0-9\-_ ]*/)
52
+ raise ArgumentError, "Bad alternative name: #{alt}"
53
+ end
54
+ }
55
+
56
+ params = {
57
+ :client_id => @client_id,
58
+ :experiment => experiment_name,
59
+ :alternatives => alternatives
60
+ }
61
+ if !force.nil? && alternatives.include?(force)
62
+ params[:force] = force
63
+ end
64
+
65
+ self.get_response("/participate", params)
66
+ end
67
+
68
+ def convert(experiment_name)
69
+ params = {
70
+ :client_id => @client_id,
71
+ :experiment => experiment_name
72
+ }
73
+ self.get_response("/convert", params)
74
+ end
75
+
76
+ def get_response(endpoint, params)
77
+ uri = URI("http://#{@host}:#{@port}" + endpoint)
78
+ uri.query = URI.encode_www_form(params)
79
+ res = Net::HTTP.get_response(uri)
80
+ if res.code == "500"
81
+ {"status" => "failed", "response" => res.body}
82
+ else
83
+ JSON.parse(res.body)
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,3 @@
1
+ module Sixpack
2
+ VERSION = "0.0.1"
3
+ end
data/sixpack.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/sixpack/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["SeatGeek"]
6
+ gem.email = ["hi@seatgeek.com"]
7
+ gem.description = %q{Ruby library for interacting with SeatGeek's sixpack.}
8
+ gem.summary = %q{Ruby library for interacting with SeatGeek's sixpack.}
9
+ gem.homepage = "http://www.seatgeek.com"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "sixpack-client"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Sixpack::VERSION
17
+
18
+ gem.add_development_dependency 'rake'
19
+ gem.add_development_dependency 'rspec'
20
+ gem.add_development_dependency 'redis'
21
+
22
+ gem.add_runtime_dependency 'uuid'
23
+ end
@@ -0,0 +1,82 @@
1
+ require 'redis'
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Sixpack do
6
+ before(:each) do
7
+ redis = Redis.new
8
+ redis.keys("*").each do |k|
9
+ redis.del(k)
10
+ end
11
+ end
12
+
13
+ it "should return an alternative for simple_participate" do
14
+ alternative = Sixpack.simple_participate('show-bieber', ['trolled', 'not-trolled'], "mike")
15
+ ['trolled', 'not-trolled'].should include(alternative)
16
+ end
17
+
18
+ it "should auto generate a client_id" do
19
+ alternative = Sixpack.simple_participate('show-bieber', ['trolled', 'not-trolled'], nil)
20
+ ['trolled', 'not-trolled'].should include(alternative)
21
+ end
22
+
23
+ it "should return ok for simple_convert" do
24
+ alternative = Sixpack.simple_participate('show-bieber', ['trolled', 'not-trolled'], "mike")
25
+ Sixpack.simple_convert("show-bieber", "mike").should == "ok"
26
+ end
27
+
28
+ it "should return ok for multiple_converts" do
29
+ alternative = Sixpack.simple_participate('show-bieber', ['trolled', 'not-trolled'], "mike")
30
+ Sixpack.simple_convert("show-bieber", "mike").should == "ok"
31
+ Sixpack.simple_convert("show-bieber", "mike").should == "ok"
32
+ end
33
+
34
+ it "should not return ok for simple_convert with new id" do
35
+ Sixpack.simple_convert("show-bieber", "unknown_id").should == "failed"
36
+ end
37
+
38
+ it "should not return ok for simple_convert with new experiment" do
39
+ alternative = Sixpack.simple_participate('show-bieber', ['trolled', 'not-trolled'], "mike")
40
+ Sixpack.simple_convert("show-blieber", "mike").should == "failed"
41
+ end
42
+
43
+ it "should not allow bad experiment names" do
44
+ expect {
45
+ Sixpack.simple_participate('%%', ['trolled', 'not-trolled'], nil)
46
+ }.to raise_error
47
+ end
48
+
49
+ it "should not allow bad alternatives names" do
50
+ expect {
51
+ Sixpack.simple_participate('show-bieber', ['trolled'], nil)
52
+ }.to raise_error
53
+
54
+ expect {
55
+ Sixpack.simple_participate('show-bieber', ['trolled', '%%'], nil)
56
+ }.to raise_error
57
+ end
58
+
59
+ it "should work without using the simple methods" do
60
+ session = Sixpack::Session.new
61
+ session.convert("testing")["status"].should == "failed"
62
+ alt_one = session.participate("testing", ["one", "two"])["alternative"]
63
+ 3.times do |n|
64
+ session.participate("testing", ["one", "two"])["alternative"].should == alt_one
65
+ end
66
+ session.convert("testing")["status"].should == "ok"
67
+
68
+ old_client_id = session.client_id
69
+ session.client_id = Sixpack::generate_client_id()
70
+ session.convert("testing")["status"].should == "failed"
71
+ alt_two = session.participate("testing", ["one", "two"])["alternative"]
72
+ 3.times do |n|
73
+ session.participate("testing", ["one", "two"])["alternative"].should == alt_two
74
+ end
75
+ session.convert("testing")["status"].should == "ok"
76
+
77
+ session = Sixpack::Session.new old_client_id
78
+ 3.times do |n|
79
+ session.participate("testing", ["one", "two"])["alternative"].should == alt_one
80
+ end
81
+ end
82
+ end
@@ -0,0 +1 @@
1
+ require 'sixpack'
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sixpack-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - SeatGeek
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: redis
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: uuid
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Ruby library for interacting with SeatGeek's sixpack.
79
+ email:
80
+ - hi@seatgeek.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - Gemfile
87
+ - LICENSE
88
+ - README.md
89
+ - Rakefile
90
+ - lib/sixpack.rb
91
+ - lib/sixpack/version.rb
92
+ - sixpack.gemspec
93
+ - spec/lib/sixpack_spec.rb
94
+ - spec/spec_helper.rb
95
+ homepage: http://www.seatgeek.com
96
+ licenses: []
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ! '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 1.8.24
116
+ signing_key:
117
+ specification_version: 3
118
+ summary: Ruby library for interacting with SeatGeek's sixpack.
119
+ test_files:
120
+ - spec/lib/sixpack_spec.rb
121
+ - spec/spec_helper.rb