rack-piwik-0.2.2/0000755000004100000410000000000012122102167013576 5ustar www-datawww-datarack-piwik-0.2.2/test/0000755000004100000410000000000012122102167014555 5ustar www-datawww-datarack-piwik-0.2.2/test/helper.rb0000750000004100000410000000151312122102167016357 0ustar www-datawww-datarequire 'rubygems' require 'test/unit' require 'shoulda' require 'rack' require 'rack/test' require File.expand_path('../../lib/rack/piwik',__FILE__) class Test::Unit::TestCase include Rack::Test::Methods def app; Rack::Lint.new(@app); end def mock_app(options) main_app = lambda { |env| request = Rack::Request.new(env) case request.path when '/head_only' then [200,{ 'Content-Type' => 'application/html' },['head only']] when '/arbitrary.xml' then [200,{'Content-Type' => 'application/xml'}, ['xml only']] when '/body_only' then [200,{'Content-Type' => 'application/html'} ,['body only']] else [404,'Nothing here'] end } builder = Rack::Builder.new builder.use Rack::Piwik, options builder.run main_app @app = builder.to_app end end rack-piwik-0.2.2/test/test_rack_piwik.rb0000750000004100000410000000447212122102167020271 0ustar www-datawww-datarequire File.expand_path('../helper',__FILE__) class TestRackPiwik < Test::Unit::TestCase context "Asynchronous" do context "default" do setup { mock_app :async => true, :tracker => 'somebody', :piwik_url => 'piwik.example.org', :piwik_id => '123' } should "add tracker if body element is present" do get "/body_only" assert_match 'http://piwik.example.org/', last_response.body assert_match '_paq.push(["setSiteId", "123"]);', last_response.body assert_match %r{\n}, last_response.body assert_equal 787, last_response.headers['Content-Length'].to_i end should "omit 404 tracking for other responses with other status" do get "/body_only" assert_no_match %r{.setDocumentTitle\('404/URL}, last_response.body end should "omit addition of tracking code for non-html content" do get "/arbitrary.xml" assert_no_match %r{Piwik}, last_response.body assert_match %r{xml only}, last_response.body end should "omit addition of tracking code if tag is missing" do get "/head_only" assert_no_match %r{Piwik}, last_response.body assert_match %r{head only}, last_response.body end end context "with a number as piwik id" do setup { mock_app :async => true, :tracker => 'somebody', :piwik_url => 'piwik.example.org', :piwik_id => 123 } should "not raise an exception" do assert_nothing_raised do get "/body_only" end end end end =begin context "Synchronous" do setup { mock_app :async => false, :tracker => 'somebody', :piwik_url => 'piwik.example.org', :piwik_id => '123' } should "add tracker if body element is present" do get "/body_only" assert_match %r{https://piwik.example.org/}, last_response.body assert_match %r{123\);}, last_response.body assert_match %r{\n}, last_response.body assert_equal "650", last_response.headers['Content-Length'] end should "show non-asynchronous tracker" do get "/bob" assert_match %r{.getTracker}, last_response.body assert_match %r{}, last_response.body assert_match %r{\"whatthe\"}, last_response.body end end =end end rack-piwik-0.2.2/LICENSE0000750000004100000410000000203712122102167014603 0ustar www-datawww-dataCopyright (c) 2009 Lee Hambley Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. rack-piwik-0.2.2/README.md0000750000004100000410000000340212122102167015052 0ustar www-datawww-data# Rack Piwik Adapted from Rack::Piwik Simple Rack middleware to help injecting the Piwik tracking code into the footer of your websites. ## Usage #### Gemfile: gem 'rack-piwik', :require => 'rack/piwik' #### Sinatra ## app.rb use Rack::Piwik, :piwik_url => '', :piwik_id => '' #### Padrino ## app/app.rb use Rack::Piwik, :piwik_url => '', :piwik_id => '' #### Rails ## application.rb: config.gem 'rack-piwik', :lib => 'rack/piwik' config.middleware.use Rack::Piwik, :piwik_url => '', :piwik_id => '' ## Thread Safety This middleware *should* be thread safe. Although my experience in such areas is limited, having taken the advice of those with more experience; I defer the call to a shallow copy of the environment, if this is of consequence to you please review the implementation. ## Change Log * 0.2.0 Use asynchronous JS tracking code by default * 0.1.0 Initial Release ## Note on Patches/Pull Requests * Fork the project. * Make your feature addition or bug fix. * Add tests for it. This is important so I don't break it in a future version unintentionally. * Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull) * Send me a pull request. Bonus points for topic branches. ## Copyright Copyright (c) 2009-2011 Lee Hambley. See LICENSE for details. With thanks to Ralph von der Heyden http://github.com/ralph/ and Simon `cimm` Schoeters http://github.com/cimm/ - And the biggest hand to Arthur `achiu` Chiu for the huge work that went into the massive 0.9 re-factor. rack-piwik-0.2.2/metadata.yml0000644000004100000410000000236412122102167016106 0ustar www-datawww-data--- !ruby/object:Gem::Specification name: rack-piwik version: !ruby/object:Gem::Version version: 0.2.2 prerelease: platform: ruby authors: - Maxwell Salzberg autorequire: bindir: bin cert_chain: [] date: 2013-03-18 00:00:00.000000000 Z dependencies: [] description: Simple Rack middleware for implementing Piwik Analytics tracking in your Ruby-Rack based project. email: - maxwell@joindiaspora.com executables: [] extensions: [] extra_rdoc_files: [] files: - lib/rack/piwik.rb - lib/rack/templates/sync.erb - lib/rack/templates/async.erb - README.md - LICENSE - test/test_rack_piwik.rb - test/helper.rb homepage: https://github.com/maxwell/rack-piwik licenses: [] post_install_message: rdoc_options: [] require_paths: - lib required_ruby_version: !ruby/object:Gem::Requirement none: false requirements: - - ! '>=' - !ruby/object:Gem::Version version: '0' required_rubygems_version: !ruby/object:Gem::Requirement none: false requirements: - - ! '>=' - !ruby/object:Gem::Version version: '0' requirements: [] rubyforge_project: rubygems_version: 1.8.24 signing_key: specification_version: 3 summary: Rack middleware to inject the Piwik tracking code into outgoing responses. Adapted from rack-google-analytics test_files: [] rack-piwik-0.2.2/lib/0000755000004100000410000000000012122102167014344 5ustar www-datawww-datarack-piwik-0.2.2/lib/rack/0000755000004100000410000000000012122102167015264 5ustar www-datawww-datarack-piwik-0.2.2/lib/rack/templates/0000755000004100000410000000000012122102167017262 5ustar www-datawww-datarack-piwik-0.2.2/lib/rack/templates/async.erb0000644000004100000410000000243612122102167021076 0ustar www-datawww-data rack-piwik-0.2.2/lib/rack/templates/sync.erb0000644000004100000410000000224712122102167020735 0ustar www-datawww-data rack-piwik-0.2.2/lib/rack/piwik.rb0000644000004100000410000000201112122102167016726 0ustar www-datawww-datarequire 'rack' require 'erb' module Rack class Piwik DEFAULT = {} def initialize(app, options = {}) raise ArgumentError, "piwik_url must be present" unless options[:piwik_url] and !options[:piwik_url].empty? raise ArgumentError, "piwik_id must be present" unless options[:piwik_id] and !options[:piwik_id].to_s.empty? @app, @options = app, DEFAULT.merge(options) end def call(env); dup._call(env); end def _call(env) @status, @headers, @response = @app.call(env) return [@status, @headers, @response] unless html? response = Rack::Response.new([], @status, @headers) @response.each { |fragment| response.write inject(fragment) } response.finish end private def html?; @headers['Content-Type'] =~ /html/; end def inject(response) file = 'async' @template ||= ::ERB.new ::File.read ::File.expand_path("../templates/#{file}.erb",__FILE__) response.gsub(%r{}, @template.result(binding) + "") end end end