rack-audit 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.md CHANGED
@@ -18,7 +18,7 @@ Or install it yourself as:
18
18
 
19
19
  ## Releasing
20
20
 
21
- gem bump -trv patch --key engineyard --host http://rubygems.engineyard.com
21
+ gem bump -trv patch
22
22
 
23
23
  ## Usage
24
24
 
@@ -2,6 +2,7 @@
2
2
  require 'thread'
3
3
  require 'net/http'
4
4
  require 'uri'
5
+ require 'logger'
5
6
 
6
7
  # gems
7
8
  require 'rack'
@@ -9,81 +10,80 @@ require 'uuidtools'
9
10
  require 'multi_json'
10
11
 
11
12
  class Rack::Audit
12
- attr_reader :queue, :host, :port, :name
13
- def initialize(app, name, uri)
14
- @app = app
15
- @queue = Queue.new
16
- @uri = URI(uri)
17
- @host = @uri.host
18
- @port = @uri.port
19
- @name = name
13
+ attr_reader :queue, :host, :port, :name, :url
14
+
15
+ def initialize(app, name, url, options={})
16
+ @app, @name, @url = app, name, url
17
+ @queue = Queue.new
18
+ @logger = options[:logger] || Logger.new(STDOUT)
20
19
 
21
20
  @consumer = Thread.new do
22
- while true do
23
- body = @queue.pop
21
+ loop do
22
+ body = self.queue.pop
24
23
  begin
25
- post(@uri, body)
24
+ post(body)
26
25
  rescue => e
27
- STDERR.puts e.inspect
26
+ self.logger.error("#{e.inspect}\n#{e.backtrace.join("\n\t")}")
28
27
  end
29
28
  end
30
29
  end
31
30
  end
32
31
 
33
32
  def call(env)
34
- uuid = UUIDTools::UUID.random_create.to_s
35
- log_request(uuid, env)
33
+ id = UUIDTools::UUID.random_create.to_s
34
+ log_request(id, env)
36
35
  response = @app.call(env)
37
- log_response(uuid, response)
36
+ log_response(id, response)
38
37
  response
39
38
  end
40
39
 
41
40
  private
42
41
 
43
- def log_request(uuid, env)
42
+ def log_request(id, env)
44
43
  request = Rack::Request.new(env)
45
44
  loggable_request = {
46
45
  :request_method => request.request_method,
47
46
  :url => request.url,
48
47
  :params => request.params,
49
- :headers => Rack::Utils::HeaderHash.new(env).to_hash,
48
+ :body => request.body.read,
49
+ :headers => env.inject({}) do |r,(k,v)|
50
+ k.match(/^HTTP_/) ? r.merge(k.gsub("HTTP_") => v) : r
51
+ end,
50
52
  }
51
53
 
52
- # FIXME: necessary?
53
54
  request.body.rewind
54
55
 
55
56
  queue << MultiJson.encode(
56
- :uuid => uuid,
57
+ :id => id,
57
58
  :request => loggable_request,
58
59
  :time => Time.new.to_i,
59
- :from => name
60
+ :from => self.name,
60
61
  )
61
- rescue => e
62
- STDERR.puts e.inspect
63
- ensure
64
- true
65
62
  end
66
63
 
67
- def log_response(uuid, response)
64
+ def log_response(id, response)
68
65
  status, headers, body = response
69
66
  full_body = ''
70
67
  body.each { |b| full_body += b.to_s; b.rewind if b.respond_to?(:rewind) }
71
- queue << MultiJson.encode(
72
- :uuid => uuid,
68
+ self.queue << MultiJson.encode(
69
+ :id => id,
73
70
  :response => [status, headers, full_body],
74
71
  :time => Time.new.to_i,
75
- :from => name
72
+ :from => self.name,
76
73
  )
77
- rescue => e
78
- STDERR.puts e.inspect
79
- ensure
80
- true
81
74
  end
82
75
 
83
- def post(uri, body)
84
- req = Net::HTTP::Post.new(uri.to_s)
85
- req["content-type"] = "application/json"
86
- req.body = body
87
- Net::HTTP.start(host, port){|http| http.request(req)}
76
+ def post(body)
77
+ uri = URI.parse(self.url)
78
+ host, port = uri.host, uri.port
79
+
80
+ http = Net::HTTP.new(uri.host, uri.port)
81
+ http.use_ssl= (uri.port == 443)
82
+
83
+ request = Net::HTTP::Post.new(uri.to_s)
84
+ request["Content-Type"]= "application/json"
85
+ request.body= body
86
+
87
+ http.request(request)
88
88
  end
89
89
  end
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  class Audit
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-audit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: