rack-analytics 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -49,7 +49,20 @@ You can also change the parser, to remove some of the fields (on the future, you
49
49
 
50
50
  use Rack::Analytics::RequestLogger
51
51
 
52
- Be sure to just use one of those, since they are mutually excludent (and `only` has a preference over `except`).
52
+ Be sure to just use one of those, since they are mutually excludent (and `only` has a preference over `except`). You can also create custom parsers, too, you just need to feed the parser with lambdas, like that:
53
+
54
+ require 'rack/analytics'
55
+
56
+ parser = Rack::Analytics::RequestParser.new
57
+
58
+ parser << lambda { |env, data| data['port'] = env['SERVER_PORT'] }
59
+ parser << lambda { |env, data| data['test'] = env['rack.test'] }
60
+
61
+ Rack::Analytics.parser = parser
62
+
63
+ use Rack::Analytics::RequestLogger
64
+
65
+ The env is a hash with all the parameters of the request, and the data is the fields that are saved on the MongoDB. Just be creative. You can use any kind of object, though, as long that they respond to `call` with two arguments.
53
66
 
54
67
  ## Notes on Patches/Pull Requests ##
55
68
 
@@ -12,6 +12,11 @@ module Rack
12
12
  def only=(values)
13
13
  @only = values.to_a
14
14
  end
15
+
16
+ def <<(field)
17
+ @custom_fields ||= []
18
+ @custom_fields << field
19
+ end
15
20
 
16
21
  def parse request
17
22
  @data = {}
@@ -21,13 +26,18 @@ module Rack
21
26
  @data['user_agent'] = request['HTTP_USER_AGENT'] if to_parse.include? 'user_agent'
22
27
  @data['referral'] = request['HTTP_REFERER'] if to_parse.include? 'referral'
23
28
 
29
+ @custom_fields.to_a.each do |field|
30
+ raise TypeError unless field.respond_to? :call
31
+ field.call(request, @data)
32
+ end
33
+
24
34
  return self
25
35
  end
26
36
 
27
37
  private
28
38
  def to_parse
29
- @only ? @only.to_a : DEFAULT_KEYS - @except.to_a
39
+ @to_parse ||= (@only ? @only.to_a : DEFAULT_KEYS - @except.to_a)
30
40
  end
31
41
  end
32
42
  end
33
- end
43
+ end
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  module Analytics
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
@@ -1,9 +1,9 @@
1
1
  require 'teststrap'
2
2
 
3
3
  context "Rack::Analytics::RequestLogger" do
4
- helper(:db) { mongo }
4
+ setup { db.drop_collection 'views' }
5
5
  teardown { Rack::Analytics.finish! }
6
-
6
+
7
7
  context "should render a get request correctly" do
8
8
  setup { get '/' }
9
9
 
@@ -40,19 +40,13 @@ context "Rack::Analytics::RequestLogger" do
40
40
  end
41
41
 
42
42
  context "should create a access document when visiting the page" do
43
- setup do
44
- db.drop_collection 'views'
45
-
46
- get '/'
47
- end
43
+ setup { get '/' }
48
44
 
49
45
  asserts('counter has incremented') { db['views'].count }.equals 1
50
46
  end
51
47
 
52
48
  context "shouldn't create a access document when with post, put and delete" do
53
49
  setup do
54
- db.drop_collection 'views'
55
-
56
50
  post '/'
57
51
  put '/'
58
52
  delete '/'
@@ -62,44 +56,28 @@ context "Rack::Analytics::RequestLogger" do
62
56
  end
63
57
 
64
58
  context "should save the path of the access" do
65
- setup do
66
- db.drop_collection 'views'
67
-
68
- get '/'
69
- end
59
+ setup { get '/' }
70
60
 
71
61
  asserts('it should have a time key') { db['views'].find_one }.includes 'path'
72
62
  asserts('it should have a time set') { db['views'].find_one['path'] }.equals '/'
73
63
  end
74
64
 
75
65
  context "should save the time of the access" do
76
- setup do
77
- db.drop_collection 'views'
78
-
79
- get '/'
80
- end
66
+ setup { get '/' }
81
67
 
82
68
  asserts('it should have a time key') { db['views'].find_one }.includes 'time'
83
69
  asserts('it should have a time set') { db['views'].find_one['time'] }.kind_of Time
84
70
  end
85
71
 
86
72
  context "should save the referral information" do
87
- setup do
88
- db.drop_collection 'views'
89
-
90
- get '/', {}, 'HTTP_REFERER' => 'http://www.google.com'
91
- end
73
+ setup { get '/', {}, 'HTTP_REFERER' => 'http://www.google.com' }
92
74
 
93
75
  asserts('it should have a referral key') { db['views'].find_one }.includes 'referral'
94
76
  asserts('it should have a correct referral set') { db['views'].find_one['referral'] }.equals 'http://www.google.com'
95
77
  end
96
78
 
97
79
  context "should save the user agent information" do
98
- setup do
99
- db.drop_collection 'views'
100
-
101
- get 'views', {}, 'HTTP_USER_AGENT' => 'Firefox'
102
- end
80
+ setup { get 'views', {}, 'HTTP_USER_AGENT' => 'Firefox' }
103
81
 
104
82
  asserts('it should have a user agent key') { db['views'].find_one }.includes 'user_agent'
105
83
  asserts('it should have a correct user agent set') { db['views'].find_one['user_agent'] }.equals 'Firefox'
@@ -24,7 +24,7 @@ context 'Rack::Analytics::RequestParser' do
24
24
  topic.except = 'time'
25
25
  topic.parse(request).data['time']
26
26
  end.nil
27
-
27
+
28
28
  asserts ('it should accept multiple values as arguments') do
29
29
  topic.except = ['time']
30
30
  topic.parse(request).data['time']
@@ -38,10 +38,24 @@ context 'Rack::Analytics::RequestParser' do
38
38
  topic.only = 'time'
39
39
  topic.parse(request).data['path']
40
40
  end.nil
41
-
41
+
42
42
  asserts ('it should accept multiple values as arguments') do
43
43
  topic.except = ['time', 'path']
44
44
  topic.parse(request).data['user_agent']
45
45
  end.nil
46
46
  end
47
- end
47
+
48
+ context "should accept custom fields" do
49
+ setup { Rack::Analytics::RequestParser.new }
50
+
51
+ asserts('it should receive the custom fields') do
52
+ topic << lambda { |env, data| data['port'] = env['SERVER_PORT'] }
53
+ topic.parse(request).data['port']
54
+ end.equals '80'
55
+
56
+ asserts('it should raise TypeError when receiving non-callables') do
57
+ topic << 0
58
+ topic.parse request
59
+ end.raises TypeError
60
+ end
61
+ end
@@ -1,15 +1,15 @@
1
1
  module TestHelpers
2
2
  def app
3
3
  Rack::Builder.new do
4
- Rack::Analytics.db = mongo
4
+ Rack::Analytics.db = db
5
5
 
6
6
  use Rack::Analytics::RequestLogger
7
7
  run DummyApp
8
8
  end
9
9
  end
10
10
 
11
- def mongo
11
+ def db
12
12
  connection = Mongo::Connection.new
13
13
  connection.db 'rack-analytics-test'
14
14
  end
15
- end
15
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-analytics
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - "Cain\xC3\xA3 Costa"
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-31 00:00:00 -02:00
18
+ date: 2011-02-04 00:00:00 -02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency