captivity 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "rack"
4
+
5
+ # Add dependencies to develop your gem here.
6
+ # Include everything needed to run rake, tests, features, etc.
7
+ group :development do
8
+ gem "jeweler", "~> 1.8.4"
9
+ gem "rack-test"
10
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Julik Tarkhanov
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,20 @@
1
+ = captivity
2
+
3
+ Captivity is a simplistick Rack middleware for capturing and logging exceptions. It is a replacement for the CommonLogger
4
+ middleware which I found lacking and abandoned. It's compatible with any Logger implementation.
5
+
6
+ == Contributing to captivity
7
+
8
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
9
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
10
+ * Fork the project.
11
+ * Start a feature/bugfix branch.
12
+ * Commit and push until you are happy with your contribution.
13
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
14
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
15
+
16
+ == Copyright
17
+
18
+ Copyright (c) 2012 Julik Tarkhanov. See LICENSE.txt for
19
+ further details.
20
+
data/Rakefile ADDED
@@ -0,0 +1,38 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+ require 'jeweler'
14
+
15
+ require File.dirname(__FILE__) + "/lib/captivity"
16
+
17
+ Jeweler::Tasks.new do |gem|
18
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
19
+ gem.version = Captivity::VERSION
20
+ gem.name = "captivity"
21
+ gem.homepage = "http://github.com/julik/captivity"
22
+ gem.license = "MIT"
23
+ gem.summary = %Q{One-line Logger attachment for Rack apps}
24
+ gem.description = %Q{A simple logging wrapper for Rack}
25
+ gem.email = "me@julik.nl"
26
+ gem.authors = ["Julik Tarkhanov"]
27
+ # dependencies defined in Gemfile
28
+ end
29
+ Jeweler::RubygemsDotOrgTasks.new
30
+
31
+ require 'rake/testtask'
32
+ Rake::TestTask.new(:test) do |test|
33
+ test.libs << 'lib' << 'test'
34
+ test.pattern = 'test/**/test_*.rb'
35
+ test.verbose = true
36
+ end
37
+
38
+ task :default => :test
data/captivity.gemspec ADDED
@@ -0,0 +1,54 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "captivity"
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Julik Tarkhanov"]
12
+ s.date = "2012-07-08"
13
+ s.description = "A simple logging wrapper for Rack"
14
+ s.email = "me@julik.nl"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "LICENSE.txt",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "captivity.gemspec",
26
+ "lib/captivity.rb",
27
+ "test/helper.rb",
28
+ "test/test_captivity.rb"
29
+ ]
30
+ s.homepage = "http://github.com/julik/captivity"
31
+ s.licenses = ["MIT"]
32
+ s.require_paths = ["lib"]
33
+ s.rubygems_version = "1.8.24"
34
+ s.summary = "One-line Logger attachment for Rack apps"
35
+
36
+ if s.respond_to? :specification_version then
37
+ s.specification_version = 3
38
+
39
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
40
+ s.add_runtime_dependency(%q<rack>, [">= 0"])
41
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
42
+ s.add_development_dependency(%q<rack-test>, [">= 0"])
43
+ else
44
+ s.add_dependency(%q<rack>, [">= 0"])
45
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
46
+ s.add_dependency(%q<rack-test>, [">= 0"])
47
+ end
48
+ else
49
+ s.add_dependency(%q<rack>, [">= 0"])
50
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
51
+ s.add_dependency(%q<rack-test>, [">= 0"])
52
+ end
53
+ end
54
+
data/lib/captivity.rb ADDED
@@ -0,0 +1,58 @@
1
+ require 'delegate'
2
+ require 'logger'
3
+ require 'rack/commonlogger'
4
+
5
+ class Captivity
6
+ VERSION = '0.0.1'
7
+
8
+ # Rack::CommonLogger is so ingeniously put together that
9
+ # it does not honor the standard logger levels, it uses #write.
10
+ # The #write method is not even defined on the standard Logger,
11
+ # so we flee to a delegate which will convert the logs to
12
+ # info messages
13
+ class LoggerWithWriteMethod < SimpleDelegator
14
+ def write(str)
15
+ info(str)
16
+ end
17
+ end
18
+
19
+ def initialize(app, logger_or_stream = STDERR)
20
+ @logger = configure_logger_or_stream(logger_or_stream)
21
+ @app = Rack::CommonLogger.new(app, @logger)
22
+ end
23
+
24
+ # Calls the contained application. The application will receive
25
+ # the defined Logger object in the "captivity.logger" of the env hash
26
+ def call(env)
27
+ env["captivity.logger"] = @logger
28
+ @app.call(env)
29
+ rescue StandardError, LoadError, SyntaxError => exception
30
+ log_exception(exception)
31
+ # Reraise to display using Rack
32
+ raise exception
33
+ end
34
+
35
+ private
36
+
37
+ def log_exception(exception)
38
+ @logger.fatal(
39
+ "\t#{exception.class} (#{exception.message}):\n " +
40
+ exception.backtrace.join("\n\t\t") +
41
+ "\n\n"
42
+ )
43
+ end
44
+
45
+ def configure_logger_or_stream(logger_or_stream)
46
+ if logger_or_stream.respond_to?(:warn) && logger_or_stream.respond_to?(:write)
47
+ logger_or_stream
48
+ elsif logger_or_stream.respond_to?(:warn)
49
+ LoggerWithWriteMethod.new(logger_or_stream)
50
+ elsif logger_or_stream.respond_to?(:write) # IO-ish
51
+ logger = Logger.new(logger_or_stream)
52
+ logger.level = Logger::DEBUG
53
+ configure_logger_or_stream(logger)
54
+ elsif logger_or_stream.nil?
55
+ configure_logger_or_stream(STDERR)
56
+ end
57
+ end
58
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.require(:default, :development)
4
+ require 'test/unit'
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'captivity'
@@ -0,0 +1,37 @@
1
+ require 'helper'
2
+ require "stringio"
3
+
4
+ ENV['RACK_ENV'] = 'test'
5
+
6
+ class TestCaptivity < Test::Unit::TestCase
7
+ include Rack::Test::Methods
8
+
9
+ def app
10
+ @app
11
+ end
12
+
13
+ def test_captivity_logs_request_with_io
14
+ stream = StringIO.new
15
+
16
+ @app = Rack::Builder.app do
17
+ use Captivity, stream
18
+ run lambda { |env| [200, {'Content-Type' => 'text/plain'}, 'OK'] }
19
+ end
20
+
21
+ get '/boom'
22
+ assert_match /GET \/boom/, stream.string
23
+ end
24
+
25
+ def test_captivity_logs_request_with_logger_object
26
+ stream = StringIO.new
27
+
28
+ @app = Rack::Builder.app do
29
+ use Captivity, Logger.new(stream)
30
+ run lambda { |env| [200, {'Content-Type' => 'text/plain'}, 'OK'] }
31
+ end
32
+
33
+ get '/boom'
34
+ assert_match /GET \/boom/, stream.string
35
+ end
36
+
37
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: captivity
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Julik Tarkhanov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rack
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: jeweler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 1.8.4
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.8.4
46
+ - !ruby/object:Gem::Dependency
47
+ name: rack-test
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: A simple logging wrapper for Rack
63
+ email: me@julik.nl
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files:
67
+ - LICENSE.txt
68
+ - README.rdoc
69
+ files:
70
+ - .document
71
+ - Gemfile
72
+ - LICENSE.txt
73
+ - README.rdoc
74
+ - Rakefile
75
+ - captivity.gemspec
76
+ - lib/captivity.rb
77
+ - test/helper.rb
78
+ - test/test_captivity.rb
79
+ homepage: http://github.com/julik/captivity
80
+ licenses:
81
+ - MIT
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ segments:
93
+ - 0
94
+ hash: -1991590435747958573
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 1.8.24
104
+ signing_key:
105
+ specification_version: 3
106
+ summary: One-line Logger attachment for Rack apps
107
+ test_files: []