Permalink
Browse files

adding AARRR middleware

  • Loading branch information...
jacquescrocker committed Jun 2, 2011
1 parent bf3628a commit 34546c512d1207fbde426b411234924a886fe9a1
Showing with 56 additions and 1 deletion.
  1. +15 −1 lib/aarrr.rb
  2. +21 −0 lib/aarrr/middleware.rb
  3. +20 −0 spec/functional/aarrr_spec.rb
View
@@ -4,13 +4,27 @@
require "aarrr/config"
require "aarrr/session"
+require "rack"
+require "aarrr/middleware"
+
# add railtie
if defined?(Rails)
require "aarrr/railtie"
end
+# helper method to initialize an AARRR session
def AARRR(env_or_model)
- AARRR::Session.new(env_or_model)
+ if env_or_model.is_a?(Hash) and env_or_model["rack.aarrr"]
+ env_or_model["rack.aarrr"]
+ else
+ session = AARRR::Session.new(env_or_model)
+
+ # add to the rack env (if applicable)
+ env_or_model["rack.aarrr"] = session if env_or_model.is_a?(Hash)
+
+ session
+ end
+
end
module AARRR
View
@@ -0,0 +1,21 @@
+module AARRR
+ class Middleware
+
+ def initialize(app)
+ @app = app
+ end
+
+ def call(env)
+ aarrr_session = AARRR(env)
+
+ status, headers, body = @app.call(env)
+
+ # sets a tracking cookie on the response
+ response = Rack::Response.new body, status, headers
+ aarrr_session.save(response)
+
+ response.finish
+ end
+
+ end
+end
@@ -4,4 +4,24 @@
it "should run the specs without error" do
true.should be_true
end
+end
+
+describe "AARRR()" do
+
+ context "with an request.env" do
+ before(:each) do
+ @env = {}
+ AARRR(@env)
+ end
+
+ it "should create a session" do
+ AARRR.users.count.should eq(1)
+ end
+
+ it "should set the rack.aarrr env variable" do
+ user_attributes = AARRR.users.find_one
+ @env["rack.aarrr"].id.should eq(user_attributes["_id"])
+ end
+ end
+
end

0 comments on commit 34546c5

Please sign in to comment.