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 CHANGED
@@ -1,6 +1,6 @@
1
- # Columbo
1
+ # columbo-rb
2
2
 
3
- The *columbo* gem goal is to include a middleware
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 'rack/capture'
17
- use Rack::Capture
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 'rack/capture'
30
- config.middleware.insert_before ActionDispatch::ShowExceptions, Columbo::Capture, {capture: Rails.env.production?}
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
 
@@ -10,10 +10,14 @@ module Columbo
10
10
 
11
11
  def initialize(app, opts={})
12
12
  @app = app
13
- @capture = opts[:capture] || false
14
- @bench = (opts[:capture] && opts[:bench]) || false
15
- @logger = opts[:logger]
16
- @inspector = Columbo::Inspector.new
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)
@@ -6,9 +6,9 @@ module Columbo
6
6
 
7
7
  attr_accessor :coll
8
8
 
9
- def initialize
10
- client = Mongo::MongoClient.from_uri(Columbo::MONGO_URI)
11
- db = client[Columbo::MONGO_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
 
@@ -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
- length: html.length,
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
- client.insert data
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
@@ -1,3 +1,3 @@
1
1
  module Columbo
2
- VERSION = "0.0.1"
2
+ VERSION = '0.1.0'
3
3
  end
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
- MONGO_URI = "mongodb://columbo:inspector@linus.mongohq.com:10025/columbo_test".freeze
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.1
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-25 00:00:00.000000000 Z
12
+ date: 2013-03-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack