Skip to content
This repository has been archived by the owner on Nov 14, 2020. It is now read-only.

Commit

Permalink
Merge pull request #1 from jgraichen/patch-path-filter
Browse files Browse the repository at this point in the history
Add option to specify a path that must match to track request.
  • Loading branch information
datenimperator committed Jul 12, 2014
2 parents e5c520c + c24753c commit 4b192ec
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/rakwik/tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ def token_auth
@options[:token_auth]
end

def path
@options[:path] || ""
end

def match_path?(url)
return path.call(url) if path.is_a? Proc
return url =~ path if path.is_a? Regexp
return url.start_with?(path.to_s)
end

def extract(request)
header = {
'User-Agent' => (request.user_agent || self.class.user_agent)
Expand Down Expand Up @@ -93,6 +103,8 @@ def extract(request)
end

def track(request)
return unless match_path?(request.path)

h, d = extract(request)
EventMachine.schedule do
http = connection(piwik_url).post :head => h, :body => d
Expand Down
104 changes: 104 additions & 0 deletions spec/lib/rakwik/tracker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,108 @@
end
end

context 'with path option' do
let(:app) {
Rakwik::Tracker.new(
lambda { |env| [200, {"Content-Type"=>"text/plain"}, ["Hello."]] },
tracker_data
)
}

let(:tracker_data) {
{
:piwik_url => 'http://example.com/piwik.php',
:site_id => 1,
:token_auth => 'foobar',
:path => path
}
}

context 'as regexp' do
let(:path) { /^\/foo.*$/ }

it 'should track foo* pathes' do
get '/foo'
sleep 0.01

get '/foo/bar'
sleep 0.01

get '/foobar'
sleep 0.01

expect(WebMock).to have_requested(:post, tracker_data[:piwik_url]).times(3)
end

it 'should not track other pathes' do
get '/fo'
sleep 0.01

get '/index'
sleep 0.01

get '/bar/foo'
sleep 0.01

expect(WebMock).to have_requested(:post, tracker_data[:piwik_url]).times(0)
end
end

context 'as proc' do
let(:path) { Proc.new { |path| path.length == 5 }}

it 'should track pathes of length 5' do
get '/foos'
sleep 0.01

get '/a/bc'
sleep 0.01

expect(WebMock).to have_requested(:post, tracker_data[:piwik_url]).times(2)
end

it 'should not track other pathes' do
get '/fo'
sleep 0.01

get '/index'
sleep 0.01

get '/bar/foo'
sleep 0.01

expect(WebMock).to have_requested(:post, tracker_data[:piwik_url]).times(0)
end
end

context 'as string' do
let(:path) { "/api" }

it 'should track pathes that start with given string' do
get '/api/v2/users'
sleep 0.01

get '/api'
sleep 0.01

get '/api/v2/users.json'
sleep 0.01

expect(WebMock).to have_requested(:post, tracker_data[:piwik_url]).times(3)
end

it 'should not track other pathes' do
get '/'
sleep 0.01

get '/users.html'
sleep 0.01

get '/downloads/session.json'
sleep 0.01

expect(WebMock).to have_requested(:post, tracker_data[:piwik_url]).times(0)
end
end
end
end

0 comments on commit 4b192ec

Please sign in to comment.