blackbeard 0.0.1.4 → 0.0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1db1bf5dab6fe74ffa7fac144296458b91e11776
4
- data.tar.gz: cb171a43b4679602dea4b7e008e3fd7868fe88da
3
+ metadata.gz: 613ebf477638192eb2ab160882e277bb3b6d0f1a
4
+ data.tar.gz: 55fea491870b73d48e81ff393a7ae2ba22f337d1
5
5
  SHA512:
6
- metadata.gz: a4fe376bbdb3eea72f1b85ef22f33ddd4a2ff7b2e1d9adc9e496882252e9cf8816655f08239313c1432b0bac98143d0129a81d5d4032b94297e676afff3a3003
7
- data.tar.gz: 7060781fefba2dff053a79e8c18d051600864579838503b71c24a7ba8ca807434d23071b3b8d2a787faf9b265f7d10e2ff6aebe441c48e61d23914f5e8b2cfdc
6
+ metadata.gz: 05a94bcd373c3c302a64179489a6b1efc3bd58980684f7afcd8200a690663fb9e7daed6ab01c9d5d4f3fae946d11e03326298d938ac94e24057a6da8e08d38dc
7
+ data.tar.gz: 49f3612b4a6c53f0a5c8b6910ee585e17473e30071ec778318aa6a4715ea3a756ef025f8fb091f4a522a490cae6b87f0686828f72f7e1788f0202e7f79e30b7d
data/README.md CHANGED
@@ -56,11 +56,39 @@ $pirate.context(:user_id => user_id, :cookies => cookies).add_unique(:logged_in_
56
56
  Non-unique counts are for metrics wherein a user may trigger the same metric multiple times and the amounts are summed up.
57
57
 
58
58
  ```ruby
59
- $pirate.context(...).add(:like, +1) # increment a like
60
- $pirate.context(...).add(:like, -1) # de-increment a like
61
- $pirate.context(...).add(:revenue, +119.95) # can also accept floats
59
+ $pirate.context(...).add_total(:like, +1) # increment a like
60
+ $pirate.context(...).add_total(:like, -1) # de-increment a like
61
+ $pirate.context(...).add_total(:revenue, +119.95) # can also accept floats
62
62
  ```
63
63
 
64
+ ### Setting Context
65
+
66
+ Most of Blackbeard's calls are done via a context.
67
+
68
+ ```ruby
69
+ $pirate.context(:user_id => current_user.id, :bot => false, :cookies => app_cookie_jar)
70
+ ```
71
+
72
+ To establish identity, a context must have a user_id or a cookie_jar where blackbeard will cookie a visitor with it's own uid. As cookie_jar is optional, you can collect metrics outside a web request. You can also increment metrics for users other than the one currently logged in. For example, if user A refers visitor B and vistor B joins, then you can increment User A's referrals.
73
+
74
+ It gets pretty tedious for a web app to constantly set the context. To make that more paletable you can use a before filter in Rails (or your framework's equivalent) and the $pirate.set_context method.
75
+
76
+ ```ruby
77
+ before_action do |controller|
78
+ $pirate.set_context(
79
+ :user_id => controller.current_user.id,
80
+ :bot => controller.bot?,
81
+ :cookies => controller.cookies)
82
+ end
83
+ ```
84
+
85
+ If you `set_context` you can now make calls directly on $pirate and they will be delegate to that context.
86
+
87
+ ```ruby
88
+ $pirate.add_total(:like, +1)
89
+ ```
90
+
91
+
64
92
  ## Contributing
65
93
 
66
94
  1. Fork it
@@ -5,15 +5,18 @@ module Blackbeard
5
5
  @pirate = pirate
6
6
  @user_id = options[:user_id]
7
7
  @bot = options[:bot] || false
8
- @blackbeard_identifier = blackbeard_visitor_id(options[:cookies] || {}) unless bot?
8
+ @cookies = options[:cookies] || {}
9
+ raise NonIdentifyingContextError unless @cookies || @user_id
9
10
  end
10
11
 
11
12
  def add_total(name, amount = 1)
12
- @pirate.total_metric(name.to_s).add(amount) unless bot?
13
+ @pirate.total_metric(name.to_s).add(unique_identifier, amount) unless bot?
14
+ self
13
15
  end
14
16
 
15
17
  def add_unique(name)
16
18
  @pirate.unique_metric(name.to_s).add(unique_identifier) unless bot?
19
+ self
17
20
  end
18
21
 
19
22
  def feature(name, options = {})
@@ -22,23 +25,23 @@ module Blackbeard
22
25
  options[:variation_to_show]
23
26
  end
24
27
 
25
- private
26
-
27
28
  def bot?
28
29
  @bot
29
30
  end
30
31
 
31
32
  def unique_identifier
32
- @user_id.present? ? "a#{@user_id}" : "b#{@blackbeard_identifier}"
33
+ @user_id.nil? ? "b#{blackbeard_visitor_id}" : "a#{@user_id}"
33
34
  end
34
35
 
36
+ private
37
+
35
38
  def blackbeard_visitor_id(cookies)
36
- cookies[:bbd] ||= generate_blackbeard_visitor_id(cookies)
39
+ @cookies[:bbd] ||= generate_blackbeard_visitor_id(cookies)
37
40
  end
38
41
 
39
42
  def generate_blackbeard_visitor_id(cookies)
40
43
  id = Blackbeard.db.increment("visitor_id")
41
- cookies[:bbd] = { :value => id, :expires => Time.now + 31536000 }
44
+ @cookies[:bbd] = { :value => id, :expires => Time.now + 31536000 }
42
45
  id
43
46
  end
44
47
 
@@ -0,0 +1,4 @@
1
+ module Blackbeard
2
+ class MissingContextError < StandardError; end
3
+ class NonIdentifyingContextError < StandardError; end
4
+ end
@@ -3,6 +3,7 @@ require "blackbeard/metric"
3
3
  require "blackbeard/metric/unique"
4
4
  require "blackbeard/metric/total"
5
5
  require "blackbeard/feature"
6
+ require "blackbeard/errors"
6
7
 
7
8
  module Blackbeard
8
9
  class Pirate
@@ -28,5 +29,19 @@ module Blackbeard
28
29
  Context.new(self, options)
29
30
  end
30
31
 
32
+ def set_context(options = {})
33
+ @set_context = Context.new(self, options)
34
+ end
35
+
36
+ def add_unique(name)
37
+ raise MissingContextError unless @set_context
38
+ @set_context.add_unique(name)
39
+ end
40
+
41
+ def add_total(name, amount)
42
+ raise MissingContextError unless @set_context
43
+ @set_context.add_total(name, amount)
44
+ end
45
+
31
46
  end
32
47
  end
@@ -1,3 +1,3 @@
1
1
  module Blackbeard
2
- VERSION = "0.0.1.4"
2
+ VERSION = "0.0.2.0"
3
3
  end
@@ -0,0 +1,26 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Blackbeard::Context do
4
+ let(:pirate) { Blackbeard::Pirate.new }
5
+ let(:context) { Blackbeard::Context.new(pirate, :user_id => 9) }
6
+ let(:uid) { context.unique_identifier }
7
+ let(:total_metric) { Blackbeard::Metric::Total.new(:total_things) }
8
+ let(:unique_metric) { Blackbeard::Metric::Unique.new(:unique_things) }
9
+
10
+ describe "#add_total" do
11
+ it "should call add on the total metric" do
12
+ pirate.should_receive(:total_metric).with(total_metric.name){ total_metric }
13
+ total_metric.should_receive(:add).with(uid, 3)
14
+ context.add_total( total_metric.name, 3 )
15
+ end
16
+ end
17
+
18
+ describe "#add_unique" do
19
+ it "should call add on the unique metric" do
20
+ pirate.should_receive(:unique_metric).with(unique_metric.name){ unique_metric }
21
+ unique_metric.should_receive(:add).with(uid)
22
+ context.add_unique( unique_metric.name )
23
+ end
24
+ end
25
+
26
+ end
data/spec/pirate_spec.rb CHANGED
@@ -23,4 +23,36 @@ describe Blackbeard::Pirate do
23
23
 
24
24
  end
25
25
 
26
+ describe "#context" do
27
+ it "should return a brand new context" do
28
+ new_context = double
29
+ Blackbeard::Context.should_receive(:new).and_return(new_context)
30
+ pirate.context.should == new_context
31
+ end
32
+ end
33
+
34
+ describe "set context delegations" do
35
+ context "with no set context" do
36
+ it "should raise Blackbeard::MissingContextError" do
37
+ expect{ pirate.add_unique(:exmaple) }.to raise_error( Blackbeard::MissingContextError )
38
+ end
39
+ it "should raise Blackbeard::MissingContextError" do
40
+ expect{ pirate.add_total(:exmaple, 1) }.to raise_error( Blackbeard::MissingContextError )
41
+ end
42
+ end
43
+ context "with context set" do
44
+ let!(:set_context){ pirate.set_context(:user_id => 1) }
45
+
46
+ it "should delegate #add_unique" do
47
+ set_context.should_receive(:add_unique).with(:example_metric).and_return(set_context)
48
+ pirate.add_unique(:example_metric)
49
+ end
50
+
51
+ it "should delegate #add_total" do
52
+ set_context.should_receive(:add_total).with(:example_metric, 1).and_return(set_context)
53
+ pirate.add_total(:example_metric, 1)
54
+ end
55
+ end
56
+ end
57
+
26
58
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blackbeard
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.4
4
+ version: 0.0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Graff
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-20 00:00:00.000000000 Z
11
+ date: 2014-01-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -157,6 +157,7 @@ files:
157
157
  - lib/blackbeard/dashboard/views/layout.erb
158
158
  - lib/blackbeard/dashboard/views/metrics/index.erb
159
159
  - lib/blackbeard/dashboard/views/metrics/show.erb
160
+ - lib/blackbeard/errors.rb
160
161
  - lib/blackbeard/feature.rb
161
162
  - lib/blackbeard/metric.rb
162
163
  - lib/blackbeard/metric/total.rb
@@ -166,6 +167,7 @@ files:
166
167
  - lib/blackbeard/storable.rb
167
168
  - lib/blackbeard/version.rb
168
169
  - spec/blackbeard_spec.rb
170
+ - spec/context_spec.rb
169
171
  - spec/dashboard_spec.rb
170
172
  - spec/metric_spec.rb
171
173
  - spec/pirate_spec.rb
@@ -198,6 +200,7 @@ specification_version: 4
198
200
  summary: Blackbeard is a Redis backed metrics collection system with a Rack dashboard
199
201
  test_files:
200
202
  - spec/blackbeard_spec.rb
203
+ - spec/context_spec.rb
201
204
  - spec/dashboard_spec.rb
202
205
  - spec/metric_spec.rb
203
206
  - spec/pirate_spec.rb