snogmetrics 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.mdown CHANGED
@@ -27,7 +27,7 @@ Yes it does.
27
27
 
28
28
  ## What if I want to call KISSmetrics from both my controllers _and_ JavaScript?
29
29
 
30
- Just do it. You need to make sure that the `_kmq` array exists, though. SNOGmetrics will define it where you place <%= km.js! %>, but that is usually at the bottom of the page (with the async API you could probably put it at the top if you wanted to), so if you want to add things to `_kmq` before that you need to define it yourself. Just add `var _kmq = _kmq || []` and you're set.
30
+ Just do it. You need to make sure that the `_kmq` array exists, though. SNOGmetrics will define it where you place `<%= km.js! %>`, but that is usually at the bottom of the page (with the async API you could probably put it at the top if you wanted to), so if you want to add things to `_kmq` before that you need to define it yourself. Just add `var _kmq = _kmq || []` and you're set.
31
31
 
32
32
  ## Anything else I should know?
33
33
 
data/lib/snogmetrics.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Snogmetrics
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
 
4
4
  def km
5
5
  @km_api ||= KissmetricsApi.new(kissmetrics_api_key, session)
@@ -13,8 +13,6 @@ module Snogmetrics
13
13
  private
14
14
 
15
15
  class KissmetricsApi
16
- include ERB::Util
17
-
18
16
  def initialize(api_key, session)
19
17
  @session = session
20
18
  @api_key = api_key
@@ -23,55 +21,28 @@ private
23
21
  def record(*args)
24
22
  raise 'Not enough arguments' if args.size == 0
25
23
  raise 'Too many arguments' if args.size > 2
26
-
27
- @session[:km_events] ||= []
28
-
29
- if args.size == 1 && args.first.is_a?(Hash)
30
- @session[:km_events] << {:properties => args.first}
31
- elsif args.size == 1
32
- @session[:km_events] << {:name => args.first}
33
- else
34
- @session[:km_events] << {:name => args.first, :properties => args.last}
35
- end
24
+
25
+ queue << ['record', *args]
36
26
  end
37
27
 
38
28
  def identify(identity)
39
- unless user_identified?(identity)
29
+ unless @session[:km_identity] == identity
30
+ queue.delete_if { |e| e.first == 'identify' }
31
+ queue << ['identify', identity]
40
32
  @session[:km_identity] = identity
41
- @session[:km_user_identified] = nil
42
33
  end
43
34
  end
44
35
 
45
36
  def js(options={})
46
37
  options = {:reset => false}.merge(options)
47
-
48
- buffer = []
49
-
50
- unless user_identified? || @session[:km_identity].blank?
51
- identity = html_escape(@session[:km_identity])
52
-
53
- buffer << push_call('identify', identity)
54
-
55
- user_identified! if options[:reset]
56
- end
57
-
58
- unless events.empty?
59
- safe_events.each do |event|
60
- if event[:name].blank?
61
- buffer << push_call('record', event[:properties])
62
- elsif event[:properties].blank?
63
- buffer << push_call('record', event[:name])
64
- else
65
- buffer << push_call('record', event[:name], event[:properties])
66
- end
67
- end
68
-
69
- reset_events! if options[:reset]
70
- end
71
-
72
- if buffer.empty?
38
+
39
+ if queue.empty?
73
40
  ''
74
41
  else
42
+ buffer = queue.map { |item| %(_kmq.push(#{item.to_json});) }
43
+
44
+ queue.clear if options[:reset]
45
+
75
46
  <<-JAVASCRIPT
76
47
  <script type="text/javascript">
77
48
  var _kmq = _kmq || [];
@@ -87,17 +58,9 @@ private
87
58
  end
88
59
 
89
60
  private
90
-
91
- def user_identified?(who=nil)
92
- if who
93
- @session[:km_user_identified] == who
94
- else
95
- !!@session[:km_user_identified]
96
- end
97
- end
98
-
99
- def push_call(*args)
100
- %(_kmq.push(#{args.to_json});)
61
+
62
+ def queue
63
+ @session[:km_queue] ||= []
101
64
  end
102
65
 
103
66
  def api_js
@@ -130,35 +93,5 @@ private
130
93
  JS
131
94
  end
132
95
  end
133
-
134
- def events
135
- @session[:km_events] || []
136
- end
137
-
138
- def reset_events!
139
- @session[:km_events] = []
140
- end
141
-
142
- def user_identified!
143
- @session[:km_user_identified] = @session[:km_identity]
144
- @session[:km_identity] = nil
145
- end
146
-
147
- def safe_properties(hash)
148
- return {} if hash.nil? || hash.empty?
149
- hash.keys.inject({}) do |h, k|
150
- h[html_escape(k)] = html_escape(hash[k])
151
- h
152
- end
153
- end
154
-
155
- def safe_events
156
- events.map do |event|
157
- safe_event = {}
158
- safe_event[:name] = html_escape(event[:name].strip) unless event[:name].blank?
159
- safe_event[:properties] = safe_properties(event[:properties]) unless event[:properties].blank?
160
- safe_event
161
- end
162
- end
163
96
  end
164
97
  end
@@ -0,0 +1,79 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{snogmetrics}
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Theo"]
12
+ s.date = %q{2010-03-31}
13
+ s.description = %q{SNOGmetrics gives you the best of both worlds: access to KISSmetrics' JavaScript API through Ruby}
14
+ s.email = %q{theo@iconara.net}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.mdown"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "Gemfile",
23
+ "LICENSE",
24
+ "README.mdown",
25
+ "Rakefile",
26
+ "example/snoggy/.gitignore",
27
+ "example/snoggy/README.mdown",
28
+ "example/snoggy/Rakefile",
29
+ "example/snoggy/app/controllers/application_controller.rb",
30
+ "example/snoggy/app/controllers/snogs_controller.rb",
31
+ "example/snoggy/app/views/layouts/application.html.erb",
32
+ "example/snoggy/app/views/snogs/new.html.erb",
33
+ "example/snoggy/app/views/snogs/thank_you.html.erb",
34
+ "example/snoggy/config/boot.rb",
35
+ "example/snoggy/config/environment.rb",
36
+ "example/snoggy/config/environments/development.rb",
37
+ "example/snoggy/config/environments/production.rb",
38
+ "example/snoggy/config/environments/test.rb",
39
+ "example/snoggy/config/initializers/snogmetrics.rb",
40
+ "example/snoggy/config/routes.rb",
41
+ "example/snoggy/script/about",
42
+ "example/snoggy/script/console",
43
+ "example/snoggy/script/dbconsole",
44
+ "example/snoggy/script/destroy",
45
+ "example/snoggy/script/generate",
46
+ "example/snoggy/script/performance/benchmarker",
47
+ "example/snoggy/script/performance/profiler",
48
+ "example/snoggy/script/plugin",
49
+ "example/snoggy/script/runner",
50
+ "example/snoggy/script/server",
51
+ "example/snoggy/vendor/plugins/snogmetrics/init.rb",
52
+ "lib/snogmetrics.rb",
53
+ "rails/init.rb",
54
+ "snogmetrics.gemspec",
55
+ "spec/snogmetrics_spec.rb",
56
+ "spec/spec.opts",
57
+ "spec/spec_helper.rb"
58
+ ]
59
+ s.homepage = %q{http://github.com/iconara/snogmetrics}
60
+ s.rdoc_options = ["--charset=UTF-8"]
61
+ s.require_paths = ["lib"]
62
+ s.rubygems_version = %q{1.3.6}
63
+ s.summary = %q{SNOGmetrics is a KISSmetrics helper for Rails}
64
+ s.test_files = [
65
+ "spec/snogmetrics_spec.rb",
66
+ "spec/spec_helper.rb"
67
+ ]
68
+
69
+ if s.respond_to? :specification_version then
70
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
71
+ s.specification_version = 3
72
+
73
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
74
+ else
75
+ end
76
+ else
77
+ end
78
+ end
79
+
@@ -50,9 +50,9 @@ describe Snogmetrics do
50
50
  @context.km.record('An important event', :p => 3)
51
51
  @context.km.record('An important event', :p => 4)
52
52
  js = @context.km.js
53
- js.should include('_kmq.push(["record","An important event",{"p":"3"}]);')
54
- js.should include('_kmq.push(["record","An important event",{"p":"4"}]);')
55
- js.index('_kmq.push(["record","An important event",{"p":"3"}]);').should < js.index('_kmq.push(["record","An important event",{"p":"4"}]);')
53
+ js.should include('_kmq.push(["record","An important event",{"p":3}]);')
54
+ js.should include('_kmq.push(["record","An important event",{"p":4}]);')
55
+ js.index('_kmq.push(["record","An important event",{"p":3}]);').should < js.index('_kmq.push(["record","An important event",{"p":4}]);')
56
56
  end
57
57
  end
58
58
 
@@ -105,6 +105,11 @@ describe Snogmetrics do
105
105
  @context.km.js(:reset => true)
106
106
  @context.km.js.should be_empty
107
107
  end
108
+
109
+ it 'does not let HTML slip through' do
110
+ @context.km.identify('</html>')
111
+ @context.km.js.should_not include('</html>')
112
+ end
108
113
  end
109
114
 
110
115
  describe '#js!' do
data/spec/spec.opts CHANGED
@@ -1 +1,2 @@
1
1
  --color
2
+ --backtrace
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 0
9
- version: 0.1.0
8
+ - 1
9
+ version: 0.1.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Theo
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-30 00:00:00 +02:00
17
+ date: 2010-03-31 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -62,6 +62,7 @@ files:
62
62
  - example/snoggy/vendor/plugins/snogmetrics/init.rb
63
63
  - lib/snogmetrics.rb
64
64
  - rails/init.rb
65
+ - snogmetrics.gemspec
65
66
  - spec/snogmetrics_spec.rb
66
67
  - spec/spec.opts
67
68
  - spec/spec_helper.rb