Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

adding AARRR middleware

  • Loading branch information...
commit 34546c512d1207fbde426b411234924a886fe9a1 1 parent bf3628a
@railsjedi authored
View
16 lib/aarrr.rb
@@ -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
21 lib/aarrr/middleware.rb
@@ -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
View
20 spec/functional/aarrr_spec.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.