Class: Smooch::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/smooch/base.rb

Constant Summary collapse

FLASH_KEY =
:smooch

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller = nil) ⇒ Base

Returns a new instance of Base.



5
6
7
8
9
# File 'lib/smooch/base.rb', line 5

def initialize(controller=nil)
  self.controller = controller
  self.view = view
  init_flash
end

Instance Attribute Details

#controllerObject

Returns the value of attribute controller.



3
4
5
# File 'lib/smooch/base.rb', line 3

def controller
  @controller
end

#viewObject

Returns the value of attribute view.



4
5
6
# File 'lib/smooch/base.rb', line 4

def view
  @view
end

Instance Method Details

#ab(name, choices = nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/smooch/base.rb', line 47

def ab(name, choices=nil)
  val = nil

  # get from parameter passed in
  val = get_ab_param(name) unless val
  val = nil if val and choices and not choices.include?(val)

  # get from database
  val = get_ab_database(name) unless val
  val = nil if val and choices and not choices.include?(val)

  # get from local storage
  val = get_ab_cached(name) unless val
  val = nil if val and choices and not choices.include?(val)

  # get from cookie
  val = get_ab_cookie(name) unless val
  val = nil if val and choices and not choices.include?(val)

  if choices and Smooch.ab_static?
    val = choices.first unless val
  end
  
  # pick a random one
  val = get_ab_random_choice(choices) unless val

  set_ab_value(name, val)
end

#api_keyObject



157
158
159
# File 'lib/smooch/base.rb', line 157

def api_key
  Smooch::API_KEY.blank? ? nil : Smooch::API_KEY
end

#clear_flashObject



38
39
40
# File 'lib/smooch/base.rb', line 38

def clear_flash
  flash[FLASH_KEY] = nil
end

#cookiesObject



204
205
206
207
# File 'lib/smooch/base.rb', line 204

def cookies
  return {} unless controller
  controller.send(:cookies)
end

#flashObject



209
210
211
212
# File 'lib/smooch/base.rb', line 209

def flash
  return {} unless controller
  controller.send(:flash)
end

#get_ab_cache_keyObject



91
92
93
# File 'lib/smooch/base.rb', line 91

def get_ab_cache_key
  [@records, @sets, @choices].to_param
end

#get_ab_cached(name) ⇒ Object



88
89
90
# File 'lib/smooch/base.rb', line 88

def get_ab_cached(name)
  @choices[name]
end


94
95
96
# File 'lib/smooch/base.rb', line 94

def get_ab_cookie(name)
  get_cookie(key(name))
end

#get_ab_database(name) ⇒ Object

TODO db support



106
107
108
109
# File 'lib/smooch/base.rb', line 106

def get_ab_database(name)
  # get_identity / key(name)
  nil 
end

#get_ab_param(name) ⇒ Object



81
82
83
# File 'lib/smooch/base.rb', line 81

def get_ab_param(name)
  return params["_ab"]
end

#get_ab_random_choice(choices) ⇒ Object



84
85
86
87
# File 'lib/smooch/base.rb', line 84

def get_ab_random_choice(choices)
  return nil unless choices
  choices[rand(choices.size)]
end


126
127
128
# File 'lib/smooch/base.rb', line 126

def get_cookie(key)
  cookies[key]
end

#get_kiss_identityObject



118
119
120
# File 'lib/smooch/base.rb', line 118

def get_kiss_identity
  controller.kiss_identity
end

#get_smooch_identityObject

—— from controller



115
116
117
# File 'lib/smooch/base.rb', line 115

def get_smooch_identity
  controller.smooch_identity
end

#has_record?(property) ⇒ Boolean

—— for tests

Returns:

  • (Boolean)


197
198
199
# File 'lib/smooch/base.rb', line 197

def has_record?(property)
  !@records[property.to_s].nil?
end

#has_set?(property) ⇒ Boolean

Returns:

  • (Boolean)


200
201
202
# File 'lib/smooch/base.rb', line 200

def has_set?(property)
  !@sets[property.to_s].nil?
end

#init_flashObject



16
17
18
19
20
21
# File 'lib/smooch/base.rb', line 16

def init_flash
  hash = flash[FLASH_KEY] || {}
  @records = hash[:r] || {}
  @sets = hash[:s] || {}
  @choices = hash[:c] || {}
end

#js(text) ⇒ Object

—— for view



131
132
133
134
135
# File 'lib/smooch/base.rb', line 131

def js(text)
  text = view.send('h', text)
  text = view.send('escape_javascript', text)
  text
end

#key(name) ⇒ Object



76
77
78
79
80
# File 'lib/smooch/base.rb', line 76

def key(name)
  # TODO: by identity?
  md5 = Digest::MD5.hexdigest("#{name}")[-10,10]
  "ab_#{md5}"
end

#paramsObject



214
215
216
217
# File 'lib/smooch/base.rb', line 214

def params
  return {} unless controller
  controller.send(:params)
end

#push_record(hash) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/smooch/base.rb', line 136

def push_record(hash)
  out = ""
  hash.each do |key, value|
    out += "_kmq.push(['record', '#{js(key)}'"
    unless value.empty?
      out += ", #{value.to_json.gsub(/<\/?script>/i, "")}"
    end
    out += "]);\n"
  end
  out = out.html_safe if out.respond_to?(:html_safe) 
  out
end

#push_set(hash) ⇒ Object



148
149
150
151
152
153
154
155
# File 'lib/smooch/base.rb', line 148

def push_set(hash)
  out = ""
  hash.each do |key, value|
     out += "_kmq.push(['set', {'#{js(key)}' : '#{js(value)}'}]);\n"
  end
  out = out.html_safe if out.respond_to?(:html_safe) 
  out
end

#record(property, hash = {}) ⇒ Object



12
13
14
15
# File 'lib/smooch/base.rb', line 12

def record(property, hash={})
  @records[property.to_s] = hash
  write_flash
end

#script(send = true) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/smooch/base.rb', line 161

def script(send=true)
  clear_flash

  out = <<-JAVASCRIPT
    <script type="text/javascript">
      var _kmq = _kmq || [];
      function _kms(u) {
        setTimeout(function() {
          var s = document.createElement('script');
          var f = document.getElementsByTagName('script')[0];
          s.type = 'text/javascript';
          s.async = true;
          s.src = u;
          f.parentNode.insertBefore(s, f);
        }, 1);
      }
  JAVASCRIPT
  if send and api_key.present?
    out += "_kms('//i.kissmetrics.com/i.js');\n"
    out += "_kms('//doug1izaerwt3.cloudfront.net/#{api_key}.1.js');\n"
  end

  identity = get_kiss_identity
  out += "_kmq.push(['identify', '#{js(identity)}']);\n" if identity   

  out += push_record(@records)
  out += push_set(@sets)
  out += push_set(@choices)

  out += "</script>\n"
  out = out.html_safe if out.respond_to?(:html_safe)  
  out
end

#set(property, value) ⇒ Object



42
43
44
45
# File 'lib/smooch/base.rb', line 42

def set(property, value)
  @sets[property.to_s] = value
  write_flash
end

#set_ab_database(name, val) ⇒ Object



110
111
112
# File 'lib/smooch/base.rb', line 110

def set_ab_database(name, val)
  # get_identity / key(name)
end

#set_ab_value(name, val) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/smooch/base.rb', line 97

def set_ab_value(name, val)
  set_ab_database(name, val)
  set_cookie(key(name), val)
  @choices[name] = val
  write_flash
  val
end


121
122
123
124
125
# File 'lib/smooch/base.rb', line 121

def set_cookie(key, value)
  unless cookies[key] == value
    cookies[key] = { :value => value, :expires => 2.months.from_now }
  end
end

#write_flashObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/smooch/base.rb', line 22

def write_flash
  hash = nil
  unless @records.empty?
    hash ||= {}
    hash[:r] = @records
  end
  unless @sets.empty?
     hash ||= {}
     hash[:s] = @sets
   end
   unless @choices.empty?
      hash ||= {}
      hash[:c] = @choices
    end
  flash[FLASH_KEY] = hash
end