smooch 0.1.3 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,13 @@
1
1
  module Smooch
2
2
  API_KEY = ''
3
+
4
+ def self.ab_static?
5
+ !!@ab_static
6
+ end
7
+ def self.ab_static=val
8
+ @ab_static=val
9
+ end
10
+
3
11
  end
4
12
 
5
13
  begin
@@ -5,28 +5,43 @@ module Smooch
5
5
  def initialize(controller=nil)
6
6
  self.controller = controller
7
7
  self.view = view
8
- @choices = {}
9
- @records = {}
10
- @sets = {}
11
-
12
8
  init_flash
13
9
  end
14
10
 
11
+ FLASH_KEY = :smooch
15
12
  def record(property, hash={})
16
- flash[:kiss_metrics] = property.to_s
17
13
  @records[property.to_s] = hash
14
+ write_flash
18
15
  end
19
16
  def init_flash
20
- property = flash[:kiss_metrics]
21
- return unless property
22
- record(property)
17
+ hash = flash[FLASH_KEY] || {}
18
+ @records = hash[:r] || {}
19
+ @sets = hash[:s] || {}
20
+ @choices = hash[:c] || {}
21
+ end
22
+ def write_flash
23
+ hash = nil
24
+ unless @records.empty?
25
+ hash ||= {}
26
+ hash[:r] = @records
27
+ end
28
+ unless @sets.empty?
29
+ hash ||= {}
30
+ hash[:s] = @sets
31
+ end
32
+ unless @choices.empty?
33
+ hash ||= {}
34
+ hash[:c] = @choices
35
+ end
36
+ flash[FLASH_KEY] = hash
23
37
  end
24
38
  def clear_flash
25
- flash[:kiss_metrics] = nil
39
+ flash[FLASH_KEY] = nil
26
40
  end
27
41
 
28
42
  def set(property, value)
29
43
  @sets[property.to_s] = value
44
+ write_flash
30
45
  end
31
46
 
32
47
  def ab(name, choices=nil)
@@ -48,6 +63,10 @@ module Smooch
48
63
  val = get_ab_cookie(name) unless val
49
64
  val = nil if val and choices and not choices.include?(val)
50
65
 
66
+ if choices and Smooch.ab_static?
67
+ val = choices.first unless val
68
+ end
69
+
51
70
  # pick a random one
52
71
  val = get_ab_random_choice(choices) unless val
53
72
 
@@ -69,6 +88,9 @@ module Smooch
69
88
  def get_ab_cached(name)
70
89
  @choices[name]
71
90
  end
91
+ def get_ab_cache_key
92
+ [@records, @sets, @choices].to_param
93
+ end
72
94
  def get_ab_cookie(name)
73
95
  get_cookie(key(name))
74
96
  end
@@ -76,6 +98,7 @@ module Smooch
76
98
  set_ab_database(name, val)
77
99
  set_cookie(key(name), val)
78
100
  @choices[name] = val
101
+ write_flash
79
102
  val
80
103
  end
81
104
 
@@ -96,7 +119,9 @@ module Smooch
96
119
  controller.kiss_identity
97
120
  end
98
121
  def set_cookie(key, value)
99
- cookies[key] = { :value => value, :expires => 2.months.from_now }
122
+ unless cookies[key] == value
123
+ cookies[key] = { :value => value, :expires => 2.months.from_now }
124
+ end
100
125
  end
101
126
  def get_cookie(key)
102
127
  cookies[key]
@@ -113,7 +138,7 @@ module Smooch
113
138
  hash.each do |key, value|
114
139
  out += "_kmq.push(['record', '#{js(key)}'"
115
140
  unless value.empty?
116
- out += ", #{value.to_json}"
141
+ out += ", #{value.to_json.gsub(/<\/?script>/i, "")}"
117
142
  end
118
143
  out += "]);\n"
119
144
  end
@@ -1,3 +1,3 @@
1
1
  module Smooch
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.5"
3
3
  end
@@ -7,6 +7,16 @@ class SmoochUser
7
7
  end
8
8
  end
9
9
  class SmoochController < ActionController::Base
10
+ def record_one
11
+ km.record("one")
12
+ end
13
+ def record_lots
14
+ km.record("one")
15
+ km.record("two")
16
+ km.set("three", "val3")
17
+ km.set("four", "val4")
18
+ end
19
+
10
20
  def get_km
11
21
  @results = km
12
22
  end
@@ -55,10 +65,38 @@ describe SmoochController, :type => :controller do
55
65
  end
56
66
 
57
67
  describe "records" do
68
+ it "should set the flash record property" do
69
+ flash[:smooch].should be_nil
70
+ get :record_one
71
+ flash[:smooch].should_not be_nil
72
+ flash[:smooch].should == {:r => { "one" => {}}}
73
+
74
+ get :get_km
75
+ assigns[:results].has_record?("one").should == true
76
+ end
77
+ it "should set everything via flash" do
78
+ flash[:smooch].should be_nil
79
+ get :record_lots
80
+ flash[:smooch].should_not be_nil
81
+
82
+ get :get_km
83
+ assigns[:results].has_record?("one").should == true
84
+ assigns[:results].has_record?("two").should == true
85
+ assigns[:results].has_set?("three").should == true
86
+ assigns[:results].has_set?("four").should == true
87
+ assigns[:results].has_set?("five").should == false
88
+ end
89
+ it "should render script from flash" do
90
+ get :record_lots
91
+ end
92
+
58
93
  it "should transfer flash property" do
59
- flash[:kiss_metrics] = "whatever"
94
+ flash[:smooch] = {:r => { "whatever" => {}, "else" => {:ok => true}}, :s => {"something" => {}}, :c => {"ab" => "one"}}
60
95
  get :get_km
96
+ assigns[:results].has_record?("else").should == true
61
97
  assigns[:results].has_record?("whatever").should == true
98
+ assigns[:results].has_set?("something").should == true
99
+ assigns[:results].ab("ab").should == "one"
62
100
  end
63
101
  end
64
102
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smooch
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 17
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 3
10
- version: 0.1.3
9
+ - 5
10
+ version: 0.1.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Brian Leonard
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-02-28 00:00:00 -08:00
18
+ date: 2011-12-09 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies: []
21
21