Permalink
Browse files

Modifying plugin so that it does not write to cookies, it passes data…

… and send to the javascript call
  • Loading branch information...
1 parent 971e3f2 commit 316f2696ecf362e8ca565ddcee4a0a14971d0b8e @winton committed Oct 23, 2010
View
@@ -86,20 +86,19 @@ window.A_B = new function() {
};
Datastore = function() {
- return new function() {
+ return new function(data, send) {
this.get = get;
this.set = set;
-
- var data, send;
+ this.toCookies = toCookies;
// Get cookie
- data = Cookies.get('a_b');
- send = Cookies.get('a_b_s');
+ data = data || Cookies.get('a_b');
+ send = send || Cookies.get('a_b_s');
// Convert from JSON
- data = eval('(' + (data || '{}') + ')');
- send = eval('(' + (send || '{}') + ')');
+ data = data ? eval('(' + data + ')') : {};
+ send = send ? eval('(' + send + ')') : {};
function diffArray(a1, a2) {
var a = [], diff = [];
@@ -390,6 +389,8 @@ window.A_B = new function() {
categories = options.categories;
env = options.env;
url = options.url;
+
+ Datastore(options.data, options.send).toCookies();
API.request();
};
};
@@ -18,7 +18,19 @@
class ABPlugin
+ attr_reader :data
+ attr_reader :send
+
def initialize(instance=nil)
+ @data = Cookies.get('a_b')
+ @send = Cookies.get('a_b_s')
+
+ @data = @data ? JSON(@data) : {}
+ @send = @send ? JSON(@send) : {}
+
+ @data = symbolize_keys(@data)
+ @send = symbolize_keys(@send)
+
ABPlugin.instance = instance
if Config.binary
@@ -28,6 +40,22 @@ def initialize(instance=nil)
end
end
+ def javascript
+ if Config.categories && Config.url
+ "a_b_setup(#{{
+ :categories => Config.categories,
+ :data => @data,
+ :env => Config.env,
+ :send => @send,
+ :url => Config.url
+ }.to_json});"
+ end
+ end
+
+ def test(category=nil, test=nil, extra=nil)
+ Test.new(@data, @send, category, test, extra)
+ end
+
class <<self
attr_accessor :cached_at
@@ -90,6 +118,15 @@ def write_yaml
end
end
end
+
+ private
+
+ def symbolize_keys(hash)
+ hash.inject({}) do |options, (key, value)|
+ options[(key.to_sym rescue key) || key] = value
+ options
+ end
+ end
end
def ABPlugin(&block)
@@ -21,11 +21,20 @@ def set(key, value)
return unless ABPlugin.instance
if ABPlugin.instance.respond_to?(:cookies)
- ABPlugin.instance.send(:cookies)[key.to_s] = value
+ if value.nil?
+ ABPlugin.instance.send(:cookies).delete(key.to_s)
+ else
+ ABPlugin.instance.send(:cookies)[key.to_s] = value
+ end
elsif ABPlugin.instance.respond_to?(:response)
- ABPlugin.instance.response.set_cookie(key.to_s, :value => value, :path => '/')
- ABPlugin.instance.request.cookies[key.to_s] = value
+ if value.nil?
+ ABPlugin.instance.response.delete_cookie(key.to_s)
+ ABPlugin.instance.request.cookies[key.to_s] = nil
+ else
+ ABPlugin.instance.response.set_cookie(key.to_s, :value => value, :path => '/')
+ ABPlugin.instance.request.cookies[key.to_s] = value
+ end
else
$cookies ||= {}
@@ -1,16 +1,8 @@
class ABPlugin
class Datastore
- def initialize
- # Get cookie
- @data = Cookies.get('a_b')
- @send = Cookies.get('a_b_s')
- # Convert from JSON
- @data = JSON(@data || '{}')
- @send = JSON(@send || '{}')
- # Symbolize keys
- @data = symbolize_keys(@data)
- @send = symbolize_keys(@send)
+ def initialize(data, send)
+ @data, @send = data, send
end
def get(key)
@@ -45,22 +37,6 @@ def set(key, value, extras=nil)
@send[:e] = extras unless extras.empty?
end
end
- # Export data to cookies
- to_cookies
- end
-
- private
-
- def symbolize_keys(hash)
- hash.inject({}) do |options, (key, value)|
- options[(key.to_sym rescue key) || key] = value
- options
- end
- end
-
- def to_cookies
- Cookies.set('a_b', @data.to_json) unless @data.empty?
- Cookies.set('a_b_s', @send.to_json) unless @send.empty?
end
end
end
@@ -7,14 +7,10 @@ def a_b(category=nil, test=nil, extra=nil)
@a_b_plugin ||= ABPlugin.new(self)
if category || test || extra
- Test.new(category, test, extra)
+ @a_b_plugin.test(category, test, extra)
- elsif Config.categories && Config.url
- "a_b_setup(#{{
- :categories => Config.categories,
- :env => Config.env,
- :url => Config.url
- }.to_json});"
+ else
+ @a_b_plugin.javascript
end
end
end
@@ -1,14 +1,14 @@
class ABPlugin
class Test
- def initialize(c, t=nil, e=nil, &block)
+ def initialize(data, send, c, t=nil, e=nil, &block)
# c, t, e = category, test, extras
if c.respond_to?(:keys)
e, c = c, nil
end
- @data = Datastore.new
+ @data = Datastore.new(data, send)
@extras = @data.get(:e)
@extras.merge!(e) if e
@@ -22,6 +22,12 @@ def app
get "/get_cookie"
last_response.body.should == 'test'
end
+
+ it "should destroy cookie" do
+ get "/destroy_cookie"
+ set_cookie = last_response['Set-Cookie']
+ set_cookie.include?('a_b=').should == false
+ end
end
describe :Sinatra do
@@ -42,5 +48,10 @@ def app
get "/get_cookie"
last_response.body.should == 'test'
end
+
+ it "should destroy cookie" do
+ get "/destroy_cookie"
+ last_response['Set-Cookie'].include?('a_b=;').should == true
+ end
end
end
@@ -23,15 +23,15 @@
a_b(:category, :test).visit.should == :v1
end
- it "should set cookie" do
+ it "should set @send" do
a_b(:category, :test).visit
- JSON($cookies['a_b_s']).should == {"v"=>[2]}
+ @a_b_plugin.send.should == {:v=>[2]}
end
it "should maintain state if called more than once" do
a_b(:category, :test).visit
a_b(:category, :test).visit
- JSON($cookies['a_b_s']).should == {"v"=>[2]}
+ @a_b_plugin.send.should == {:v=>[2]}
end
it "should return the variant name if variant specified and selected" do
@@ -70,7 +70,7 @@
it "should accept a hash with extra boolean values" do
a_b(:category, :test, :e => true).visit(:v1)
- JSON($cookies['a_b_s']).should == {"v"=>[2],"e"=>{"e" => true}}
+ @a_b_plugin.send.should == {:v=>[2],:e=>{:e => true}}
end
end
@@ -89,15 +89,15 @@
a_b(:category, :test).convert.should == :v1
end
- it "should set cookie" do
+ it "should set @send" do
a_b(:category, :test).convert
- JSON($cookies['a_b_s']).should == {"v"=>[2], "c"=>[2]}
+ @a_b_plugin.send.should == {:v=>[2], :c=>[2]}
end
it "should maintain state if called more than once" do
a_b(:category, :test).convert
a_b(:category, :test).convert
- JSON($cookies['a_b_s']).should == {"v"=>[2], "c"=>[2]}
+ @a_b_plugin.send.should == {:v=>[2], :c=>[2]}
end
it "should return the variant name if variant specified and selected" do
@@ -136,7 +136,7 @@
it "should accept a hash with extra boolean values" do
a_b(:category, :test, :e => true).convert(:v1)
- JSON($cookies['a_b_s']).should == {"v"=>[2],"c"=>[2],"e"=>{"e" => true}}
+ @a_b_plugin.send.should == {:v=>[2],:c=>[2],:e=>{:e => true}}
end
end
end
@@ -16,6 +16,12 @@ def helper_respond_to
render :inline => "<%= private_methods.collect(&:to_s).include?(params[:method]) ? 1 : 0 %>"
end
+ def destroy_cookie
+ ABPlugin.instance = self
+ ABPlugin::Cookies.set('a_b', nil)
+ render :nothing => true
+ end
+
def get_cookie
ABPlugin.instance = self
render :text => ABPlugin::Cookies.get('a_b')
@@ -10,6 +10,12 @@ class SinatraApp < Sinatra::Base
erb "<%= private_methods.collect(&:to_s).include?(params[:method]) ? 1 : 0 %>"
end
+ get "/destroy_cookie" do
+ ABPlugin.instance = self
+ ABPlugin::Cookies.set('a_b', nil)
+ nil
+ end
+
get "/get_cookie" do
ABPlugin.instance = self
ABPlugin::Cookies.get('a_b')

0 comments on commit 316f269

Please sign in to comment.