snogmetrics 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -15,10 +15,10 @@ tmtags
15
15
 
16
16
  ## PROJECT::GENERAL
17
17
  /coverage
18
- /rdoc
19
- /yardoc
18
+ /doc
20
19
  /coverage
21
20
  /pkg
22
21
  /.bundle
22
+ /.yardoc
23
23
 
24
24
  ## PROJECT::SPECIFIC
data/CHANGELOG.mdown ADDED
@@ -0,0 +1,13 @@
1
+ # SNOGmetrics changelog
2
+
3
+ ## 0.1.2
4
+
5
+ API documentation and a way to override the logic that determines when the stub API is used instead of the real KISSmetrics API.
6
+
7
+ ## 0.1.1
8
+
9
+ Refactored and removed 40% of the code.
10
+
11
+ ## 0.1.0
12
+
13
+ First release.
data/Gemfile CHANGED
@@ -4,6 +4,7 @@ group :development do
4
4
  gem 'rake'
5
5
  gem 'jeweler'
6
6
  gem 'yard'
7
+ gem 'BlueCloth'
7
8
  gem 'rails', '>= 2.3.5'
8
9
  end
9
10
 
data/Rakefile CHANGED
@@ -38,7 +38,7 @@ begin
38
38
  require 'yard'
39
39
  YARD::Rake::YardocTask.new
40
40
  rescue LoadError
41
- task :yardoc do
41
+ task :yard do
42
42
  abort 'YARD is not available. In order to run yardoc, you must: sudo gem install yard'
43
43
  end
44
44
  end
data/lib/snogmetrics.rb CHANGED
@@ -1,8 +1,26 @@
1
+ # If SNOGmetrics is used as a Rails plugin, this module is automatically mixed
2
+ # into ActionController::Base, so that it's #km method is available in
3
+ # controllers and in views.
4
+ #
5
+ # If not used as a Rails plugin, make sure that the context where Snogmetrics
6
+ # is mixed in defines #session (which should return hash).
7
+ #
8
+ # You should override #kissmetrics_api_key in an initializer to set the
9
+ # KISSmetrics API key.
10
+ #
11
+ # You can also override #use_fake_kissmetrics_api? to provide your own logic for
12
+ # when the real KISSmetrics API and when the fake should be used. The fake API
13
+ # simply outputs all events to the console (if the console is defined). The
14
+ # the default implementation outputs the real API only when
15
+ # `Rails.env.production?` is true.
1
16
  module Snogmetrics
2
- VERSION = '0.1.1'
3
-
17
+ VERSION = '0.1.2'
18
+
19
+ # Returns an instance of KissmetricsApi, which is an interface to the
20
+ # KISSmetrics API. It has the methods #record and #identify, which work just
21
+ # like the corresponding methods in the JavaScript API.
4
22
  def km
5
- @km_api ||= KissmetricsApi.new(kissmetrics_api_key, session)
23
+ @km_api ||= KissmetricsApi.new(kissmetrics_api_key, session, use_fake_kissmetrics_api?)
6
24
  end
7
25
 
8
26
  # Override this method to set the KISSmetrics API key
@@ -10,14 +28,30 @@ module Snogmetrics
10
28
  ''
11
29
  end
12
30
 
31
+ # Override this method to customize when the real API and when the stub API
32
+ # will be outputted.
33
+ def use_fake_kissmetrics_api?
34
+ if defined? Rails
35
+ ! Rails.env.production?
36
+ else
37
+ false
38
+ end
39
+ end
40
+
13
41
  private
14
42
 
15
43
  class KissmetricsApi
16
- def initialize(api_key, session)
44
+ # Do not instantiate KissmetricsApi yourself, instead mix in Snogmetrics
45
+ # and use it's #km method to get an instance of KissmetricsApi.
46
+ def initialize(api_key, session, fake_it)
17
47
  @session = session
18
48
  @api_key = api_key
49
+ @fake_it = fake_it
19
50
  end
20
51
 
52
+ # The equivalent of the `KM.record` method of the JavaScript API. You can
53
+ # pass either an event name, an event name and a hash of properties, or only
54
+ # a hash of properties.
21
55
  def record(*args)
22
56
  raise 'Not enough arguments' if args.size == 0
23
57
  raise 'Too many arguments' if args.size > 2
@@ -25,6 +59,7 @@ private
25
59
  queue << ['record', *args]
26
60
  end
27
61
 
62
+ # The equivalent of the `KM.identify` method of the JavaScript API.
28
63
  def identify(identity)
29
64
  unless @session[:km_identity] == identity
30
65
  queue.delete_if { |e| e.first == 'identify' }
@@ -33,6 +68,23 @@ private
33
68
  end
34
69
  end
35
70
 
71
+ # Equivalent to `js(:reset => true)`, i.e. returns the JavaScript code
72
+ # needed to load the KISSmetrics API and send the current state, and reset
73
+ # the state afterwards.
74
+ def js!
75
+ js(:reset => true)
76
+ end
77
+
78
+ # In most situations you want the #js! method instead of this one.
79
+ #
80
+ # Returns the JavaScript needed to send the current state to KISSmetrics.
81
+ # This includes the a JavaScript tag that loads the API code (or a fake API
82
+ # that sends events to the console if the `fake_it` parameter to #initialize
83
+ # is true), as well as statements that push the current state onto the
84
+ # `_kmq` array.
85
+ #
86
+ # You can pass the option `:reset` to reset the state, this makes sure that
87
+ # subsequent calls to #js will not send events that have already been sent.
36
88
  def js(options={})
37
89
  options = {:reset => false}.merge(options)
38
90
 
@@ -52,10 +104,6 @@ private
52
104
  JAVASCRIPT
53
105
  end
54
106
  end
55
-
56
- def js!
57
- js(:reset => true)
58
- end
59
107
 
60
108
  private
61
109
 
@@ -64,9 +112,7 @@ private
64
112
  end
65
113
 
66
114
  def api_js
67
- if Rails.env.production?
68
- %((function(){function _kms(u,d){if(navigator.appName.indexOf("Microsoft")==0 && d)document.write("<scr"+"ipt defer='defer' async='true' src='"+u+"'></scr"+"ipt>");else{var s=document.createElement('script');s.type='text/javascript';s.async=true;s.src=u;(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(s);}}_kms('https://i.kissmetrics.com/i.js');_kms('http'+('https:'==document.location.protocol ? 's://s3.amazonaws.com/' : '://')+'scripts.kissmetrics.com/#{@api_key}.1.js',1);})();)
69
- else
115
+ if @fake_it
70
116
  <<-JS
71
117
  var KM = {
72
118
  record: function() {
@@ -91,6 +137,8 @@ private
91
137
  })(_kmq);
92
138
  }
93
139
  JS
140
+ else
141
+ %((function(){function _kms(u,d){if(navigator.appName.indexOf("Microsoft")==0 && d)document.write("<scr"+"ipt defer='defer' async='true' src='"+u+"'></scr"+"ipt>");else{var s=document.createElement('script');s.type='text/javascript';s.async=true;s.src=u;(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(s);}}_kms('https://i.kissmetrics.com/i.js');_kms('http'+('https:'==document.location.protocol ? 's://s3.amazonaws.com/' : '://')+'scripts.kissmetrics.com/#{@api_key}.1.js',1);})();)
94
142
  end
95
143
  end
96
144
  end
data/snogmetrics.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{snogmetrics}
8
- s.version = "0.1.1"
8
+ s.version = "0.1.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Theo"]
12
- s.date = %q{2010-03-31}
12
+ s.date = %q{2010-04-02}
13
13
  s.description = %q{SNOGmetrics gives you the best of both worlds: access to KISSmetrics' JavaScript API through Ruby}
14
14
  s.email = %q{theo@iconara.net}
15
15
  s.extra_rdoc_files = [
@@ -19,6 +19,7 @@ Gem::Specification.new do |s|
19
19
  s.files = [
20
20
  ".document",
21
21
  ".gitignore",
22
+ "CHANGELOG.mdown",
22
23
  "Gemfile",
23
24
  "LICENSE",
24
25
  "README.mdown",
@@ -50,9 +50,11 @@ 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
+ first = '_kmq.push(["record","An important event",{"p":3}]);'
54
+ second = '_kmq.push(["record","An important event",{"p":4}]);'
55
+ js.should include(first)
56
+ js.should include(second)
57
+ js.index(first).should < js.index(second)
56
58
  end
57
59
  end
58
60
 
@@ -82,9 +84,19 @@ describe Snogmetrics do
82
84
  Rails.stub!(:env).and_return(mock('env', :production? => true))
83
85
  end
84
86
 
85
- it 'outputs a JavaScript tag that loads the KISSmetrics API' do
87
+ it 'outputs a JavaScript tag that loads the KISSmetrics API for the provided API key' do
88
+ @context.stub!(:kissmetrics_api_key).and_return('cab1ebeef')
86
89
  @context.km.identify('Phil')
87
- @context.km.js.should include('scripts.kissmetrics.com/abc123.1.js')
90
+ @context.km.js.should include('scripts.kissmetrics.com/cab1ebeef.1.js')
91
+ end
92
+ end
93
+
94
+ context 'overriding #use_fake_kissmetrics_api?' do
95
+ it 'will do your bidding, and not be influenced by Rails.env' do
96
+ Rails.stub!(:env).and_return(mock('env', :production? => true))
97
+ @context.stub!(:use_fake_kissmetrics_api?).and_return(true)
98
+ @context.km.identify('Joyce')
99
+ @context.km.js.should_not include('scripts.kissmetrics.com')
88
100
  end
89
101
  end
90
102
 
@@ -97,7 +109,7 @@ describe Snogmetrics do
97
109
  @context.km.record('1')
98
110
  @context.km.record('2')
99
111
  @context.km.record('3')
100
- @context.km.js.scan(/_kmq.push\(\["record","\d"\]\)/).size.should == 3
112
+ @context.km.js.scan(/_kmq.push\(\["record","\d"\]\)/).should have(3).items
101
113
  end
102
114
 
103
115
  it 'resets the session when passed :reset => true' do
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 1
9
- version: 0.1.1
8
+ - 2
9
+ version: 0.1.2
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-31 00:00:00 +02:00
17
+ date: 2010-04-02 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -30,6 +30,7 @@ extra_rdoc_files:
30
30
  files:
31
31
  - .document
32
32
  - .gitignore
33
+ - CHANGELOG.mdown
33
34
  - Gemfile
34
35
  - LICENSE
35
36
  - README.mdown