From 7d04aa36ddfb3e19ac566ee480a0b1dfe5cd64e5 Mon Sep 17 00:00:00 2001 From: Jason L Perry Date: Thu, 13 Aug 2009 16:06:55 -0400 Subject: [PATCH] Implemented --- lib/rack-google_analytics.rb | 0 lib/rack/google_analytics.rb | 40 ++++++++++++++++++++ test/rack-google_analytics_test.rb | 7 ---- test/rack/google_analytics_test.rb | 61 ++++++++++++++++++++++++++++++ test/test_helper.rb | 5 +-- 5 files changed, 102 insertions(+), 11 deletions(-) delete mode 100644 lib/rack-google_analytics.rb create mode 100644 lib/rack/google_analytics.rb delete mode 100644 test/rack-google_analytics_test.rb create mode 100644 test/rack/google_analytics_test.rb diff --git a/lib/rack-google_analytics.rb b/lib/rack-google_analytics.rb deleted file mode 100644 index e69de29..0000000 diff --git a/lib/rack/google_analytics.rb b/lib/rack/google_analytics.rb new file mode 100644 index 0000000..39a1b00 --- /dev/null +++ b/lib/rack/google_analytics.rb @@ -0,0 +1,40 @@ +module Rack #:nodoc: + class GoogleAnalytics < Struct.new :app, :options + + def call env + status, headers, response = app.call(env) + + if headers["Content-Type"] =~ /text\/html|application\/xhtml\+xml/ + body = "" + response.each { |part| body += part } + index = body.rindex("") + if index + body.insert(index, tracking_code(options[:web_property_id])) + headers["Content-Length"] = body.length.to_s + response = body + end + end + + [status, headers, response] + end + + private + + # Returns JS to be embeded. This takes one argument, a Web Property ID + # (aka UA number). + def tracking_code web_property_id + return <<-EOF + + +EOF + end + + end +end \ No newline at end of file diff --git a/test/rack-google_analytics_test.rb b/test/rack-google_analytics_test.rb deleted file mode 100644 index 6951151..0000000 --- a/test/rack-google_analytics_test.rb +++ /dev/null @@ -1,7 +0,0 @@ -require 'test_helper' - -class RackGoogleAnalyticsTest < Test::Unit::TestCase - should "probably rename this file and start testing for real" do - flunk "hey buddy, you should probably rename this file and start testing for real" - end -end diff --git a/test/rack/google_analytics_test.rb b/test/rack/google_analytics_test.rb new file mode 100644 index 0000000..30d3d02 --- /dev/null +++ b/test/rack/google_analytics_test.rb @@ -0,0 +1,61 @@ +require 'test_helper' +require 'rack/mock' + +class Rack::GoogleAnalyticsTest < Test::Unit::TestCase + + def test_embed_tracking_code_at_the_end_of_html_body + assert_match TRACKER_EXPECT, request.body + end + + def test_embed_tracking_code_in_xhtml_documents + assert_match TRACKER_EXPECT, request(:content_type => "application/xhtml+xml").body + end + + def test_dont_embed_code_in_non_html_documents + assert_no_match TRACKER_EXPECT, request(:content_type => "text/xml", :body => XML_DOC).body + end + + def test_should_not_raise_exception_if_theres_no_html_body_tag + assert_nothing_raised { request(:body => "") } + end + + def test_shoud_buff_content_length_by_the_size_of_tracker_code + assert_equal HTML_DOC.length + 406, request.content_length + end + + private + + TRACKER_EXPECT = /\s?<\/body>/m + + HTML_DOC = <<-EOF + + + Rack::GoogleAnalytics + + +

Rack::GoogleAnalytics

+ + + EOF + + XML_DOC = <<-EOF + + + Old Pond + Matsuo Basho + an ancient pond / a frog jumps in / the splash of water + + EOF + + def request opts = {} + Rack::MockRequest.new(app(opts)).get("/") + end + + def app opts = {} + opts[:content_type] ||= "text/html" + opts[:body] ||= HTML_DOC + rack_app = lambda { |env| [200, { 'Content-Type' => opts[:content_type] }, opts[:body]] } + Rack::GoogleAnalytics.new(rack_app, :web_property_id => "UA-0000000-0") + end + +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 8d24187..776d018 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,10 +1,7 @@ require 'rubygems' require 'test/unit' -require 'shoulda' $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) $LOAD_PATH.unshift(File.dirname(__FILE__)) -require 'rack-google_analytics' +require 'rack/google_analytics' -class Test::Unit::TestCase -end