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

Commit

Permalink
Add first specs
Browse files Browse the repository at this point in the history
  • Loading branch information
datenimperator committed May 30, 2013
1 parent 12b5fbe commit 9b3a32d
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 28 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ gemspec

group :test do
gem 'rack-test'
gem 'webmock'
end
25 changes: 16 additions & 9 deletions lib/rakwik/tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

module Rakwik
class Tracker

include Rack::Response::Helpers


attr_accessor :status

DEFAULT = {
:track_404 => true
}
Expand All @@ -16,13 +18,15 @@ def initialize(app, options = {})
end

def call(env)
start = Time.now
@status, @headers, @body = @app.call(env)
env['rakwik.duration'] = (Time.now - start)*1000
track Rack::Request.new(env) if track?
[@status, @headers, @body]
end

private

def track?
ok? || (not_found? && @options[:track_404] === true)
end
Expand All @@ -38,10 +42,10 @@ def piwik_id
def token_auth
@options[:token_auth]
end

def extract(request)
header = {
'User-Agent' => request.user_agent
'User-Agent' => (request.user_agent || "Rakwik Tracker #{Rakwik::VERSION}")
}
header['Accept-Language'] = request.env['HTTP_ACCEPT_LANGUAGE'] unless request.env['HTTP_ACCEPT_LANGUAGE'].nil?
header['DNT'] = request.env['HTTP_DNT'] unless request.env['HTTP_DNT'].nil?
Expand All @@ -61,22 +65,25 @@ def extract(request)
'url' => request.url,
'cip' => request.ip,
'rand' => rand(1000000),
'apiv' => 1
'apiv' => 1,
'gt_ms' => request.env['rakwik.duration'],
'ua' => request.user_agent
}
data['action_name'] = request.env['rakwik.action_name'] unless request.env['rakwik.action_name'].nil?
data['urlref'] = request.referer unless request.referer.nil?

data['gt_ms'] = request.env['rakwik.duration']

if not_found? && @options[:track_404] === true
data['action_name'] = "404/URL = #{data['url'].gsub(/\//, '%2f')}/From = #{data['urlref'].gsub(/\//, '%2f')}"
end

[header, data]
end

def track(request)
h, d = extract(request)
EventMachine.schedule do
http = connection(piwik_url).get :head => h, :query => d
http = connection(piwik_url).post :head => h, :body => d
http.errback {
time = Time.now.strftime("%Y-%m-%d %H:%M:%S")
request.env['rack.errors'].puts "[#{time}] ERROR Rakwik::Tracker: #{http.error}"
Expand Down
13 changes: 0 additions & 13 deletions spec/lib/rakwik/test/application.rb

This file was deleted.

33 changes: 28 additions & 5 deletions spec/lib/rakwik/tracker_spec.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
require 'spec_helper'
require 'lib/rakwik/test/application'

describe Rakwik::Tracker do
extend Rack::Test::Methods

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

def app
Rakwik::Tracker.new(Rakwik::Test::Application.new, :piwik_url => 'http://example.com/piwik.php', :site_id => 1, :token_auth => 'foobar' )
Rakwik::Tracker.new(
lambda { |env| [200, {"Content-Type"=>"text/plain"}, ["Hello. The time is #{Time.now}"]] },
tracker_data
)
end

before(:each) do
stub_request(:post, tracker_data[:piwik_url]).to_return(:status => 200, :body => lambda{|req| req.body})
end

it "says hello" do
it "tracks requests asynchronously" do
# Trigger a request to our inner app that should be tracked
get '/'
last_response.should.be.ok
last_response.body.should.equal 'Hello World'

# wait a little while to let EventMachine send the request
sleep 0.01

# What now?
WebMock.should have_requested(:post, tracker_data[:piwik_url]).with{|req|
posted_data = URI::decode_www_form(req.body).inject(Hash.new){|h, raw| h[raw[0]] = raw[1]; h}
posted_data.should include("token_auth"=>"foobar", "idsite"=>"1", "rec"=>"1", "url" => "http://example.org/", "apiv"=>"1")
true
}
end
end
4 changes: 3 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
require 'bundler/setup'
require 'rspec'
require 'rack/test'
#require 'rack'
require 'webmock/rspec'
require 'rakwik'

WebMock.disable_net_connect!

RSpec.configure do |config|
config.include Rack::Test::Methods
end

0 comments on commit 9b3a32d

Please sign in to comment.