columbo 0.0.1 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +10 -6
- data/lib/columbo/capture.rb +8 -4
- data/lib/columbo/db_client.rb +3 -3
- data/lib/columbo/inspector.rb +23 -5
- data/lib/columbo/version.rb +1 -1
- data/lib/columbo.rb +1 -3
- metadata +2 -2
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
#
|
1
|
+
# columbo-rb
|
2
2
|
|
3
|
-
The *columbo* gem
|
3
|
+
The *columbo* gem includes a middleware
|
4
4
|
that captures users browsing sessions for Rack applications.
|
5
5
|
|
6
6
|
Tribute to [Inspector Columbo](http://www.imdb.com/title/tt1466074/)
|
@@ -13,8 +13,8 @@ If your application includes a rackup file
|
|
13
13
|
or uses *Rack::Builder* to construct the application pipeline,
|
14
14
|
simply require and use as follows:
|
15
15
|
|
16
|
-
require '
|
17
|
-
use
|
16
|
+
require 'columbo/capture'
|
17
|
+
use Columbo::Capture
|
18
18
|
run app
|
19
19
|
|
20
20
|
## Using with Rails 3
|
@@ -26,8 +26,12 @@ In order to use, include the following in a Rails application
|
|
26
26
|
|
27
27
|
*config/application.rb* file:
|
28
28
|
|
29
|
-
require '
|
30
|
-
config.middleware.insert_before ActionDispatch::ShowExceptions, Columbo::Capture, {
|
29
|
+
require 'columbo/capture'
|
30
|
+
config.middleware.insert_before ActionDispatch::ShowExceptions, Columbo::Capture, {
|
31
|
+
capture: Rails.env.production?,
|
32
|
+
bench: true,
|
33
|
+
mongo_uri: 'mongodb://user:password@mongodb_host:port/database'
|
34
|
+
}
|
31
35
|
|
32
36
|
Check the Rack configuration:
|
33
37
|
|
data/lib/columbo/capture.rb
CHANGED
@@ -10,10 +10,14 @@ module Columbo
|
|
10
10
|
|
11
11
|
def initialize(app, opts={})
|
12
12
|
@app = app
|
13
|
-
@capture
|
14
|
-
@bench
|
15
|
-
@logger
|
16
|
-
@
|
13
|
+
@capture = opts[:capture] || false
|
14
|
+
@bench = (opts[:capture] && opts[:bench]) || false
|
15
|
+
@logger = opts[:logger]
|
16
|
+
@mongo_uri = opts[:mongo_uri]
|
17
|
+
|
18
|
+
raise ArgumentError, 'mongo URI missing.' if @mongo_uri.nil?
|
19
|
+
|
20
|
+
@inspector = Columbo::Inspector.new @mongo_uri
|
17
21
|
end
|
18
22
|
|
19
23
|
def call(env)
|
data/lib/columbo/db_client.rb
CHANGED
@@ -6,9 +6,9 @@ module Columbo
|
|
6
6
|
|
7
7
|
attr_accessor :coll
|
8
8
|
|
9
|
-
def initialize
|
10
|
-
client = Mongo::MongoClient.from_uri(
|
11
|
-
db
|
9
|
+
def initialize(uri)
|
10
|
+
client = Mongo::MongoClient.from_uri(uri)
|
11
|
+
db = client[uri.split('/').last]
|
12
12
|
@coll = db[Columbo::MONGO_COLLECTION]
|
13
13
|
end
|
14
14
|
|
data/lib/columbo/inspector.rb
CHANGED
@@ -6,12 +6,13 @@ require 'html_mini'
|
|
6
6
|
module Columbo
|
7
7
|
class Inspector
|
8
8
|
|
9
|
-
def initialize
|
9
|
+
def initialize(mongo_uri)
|
10
|
+
@mongo_uri = mongo_uri
|
10
11
|
end
|
11
12
|
|
12
13
|
def investigate(env, status, headers, body, start, stop)
|
13
14
|
# Lazy connection to MongoDB
|
14
|
-
client = Columbo::DbClient.new
|
15
|
+
client = Columbo::DbClient.new @mongo_uri
|
15
16
|
# Normalise request from env
|
16
17
|
request = Rack::Request.new(env)
|
17
18
|
html = ''
|
@@ -45,7 +46,7 @@ module Columbo
|
|
45
46
|
},
|
46
47
|
status: status,
|
47
48
|
headers: headers,
|
48
|
-
|
49
|
+
size: html.length,
|
49
50
|
body: HtmlMini.minify(html),
|
50
51
|
text: text,
|
51
52
|
title: title,
|
@@ -54,18 +55,35 @@ module Columbo
|
|
54
55
|
time: stop-start
|
55
56
|
}
|
56
57
|
# Insert data in MongoDB
|
57
|
-
|
58
|
+
# TODO: error logging
|
59
|
+
client.insert sanitize(data)
|
58
60
|
end
|
59
61
|
|
60
62
|
def to_plain_text(html)
|
61
63
|
html_doc = Nokogiri::HTML(html)
|
62
64
|
html_doc.xpath('//script').each {|node| node.remove}
|
63
65
|
html_doc.xpath('//style').each {|node| node.remove}
|
64
|
-
text =
|
66
|
+
text = ''
|
65
67
|
html_doc.xpath('//body').each {|node| text += node.text.gsub(/\s{2,}/, ' ')}
|
66
68
|
title = html_doc.xpath('//title').first.text
|
67
69
|
[text, title]
|
68
70
|
end
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
def sanitize(data)
|
75
|
+
Hash[
|
76
|
+
data.map do |key, value|
|
77
|
+
value = sanitize(value) if value.is_a? Hash
|
78
|
+
# replace $ and . in keys by Unicode full width equivalent
|
79
|
+
# http://docs.mongodb.org/manual/faq/developers/#faq-dollar-sign-escaping
|
80
|
+
key = key.gsub('.', 'U+FF0E').gsub('$', 'U+FF04') if key.is_a? String
|
81
|
+
# transform symbol into string to avoid auto transformation into $symbol
|
82
|
+
value = value.to_s if value.is_a? Symbol
|
83
|
+
[key, value]
|
84
|
+
end
|
85
|
+
]
|
86
|
+
end
|
69
87
|
|
70
88
|
end
|
71
89
|
end
|
data/lib/columbo/version.rb
CHANGED
data/lib/columbo.rb
CHANGED
@@ -3,7 +3,5 @@ require 'columbo/db_client'
|
|
3
3
|
require 'columbo/inspector'
|
4
4
|
|
5
5
|
module Columbo
|
6
|
-
|
7
|
-
MONGO_DB = "columbo_test".freeze
|
8
|
-
MONGO_COLLECTION = "tests".freeze
|
6
|
+
MONGO_COLLECTION = "hits".freeze
|
9
7
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: columbo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-03-
|
12
|
+
date: 2013-03-26 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rack
|