status_cats 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm 1.9.3@status_cats
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in status_cats.gemspec
4
+ gemspec
@@ -0,0 +1,46 @@
1
+ # Status Cats
2
+
3
+ Rack middleware replacing responses with relevant pictures of cats, based on the status code.
4
+
5
+ The pictures are made/collected by girliemac on [Flickr](http://www.flickr.com/photos/girliemac/sets/72157628409467125/detail/)
6
+
7
+ There is also an [apache module](https://gist.github.com/1476499).
8
+
9
+ ## Usage
10
+
11
+ Install:
12
+
13
+ ```
14
+ gem install status_cats
15
+ ```
16
+
17
+ Place inside your rackup file:
18
+
19
+ ``` ruby
20
+ require 'status_cats'
21
+ use StatusCats
22
+ ```
23
+
24
+ This will also replace valid responses, even `200`, basically making your application useless.
25
+
26
+ You can manage which status codes should be replaced with cats, by using the `:only` and `:except`
27
+ options, which take arrays or ranges.
28
+
29
+ ``` ruby
30
+ use StatusCats, :only => [ 403, 404, 500 ]
31
+
32
+ # or ...
33
+
34
+ use StatusCats, :except => 200...300
35
+ ```
36
+
37
+ ## Known issues
38
+
39
+ Status code 100 (Continue) cannot have a response body. Triggering it will case Rack to throw up.
40
+
41
+ ## License
42
+
43
+ The Ruby code in this project is released under the MIT License.
44
+
45
+ The pictures of cats are by Flickr user [girliemac](http://www.flickr.com/photos/girliemac/),
46
+ released under [this](http://creativecommons.org/licenses/by/2.0/deed.en) Creative Commons License.
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'status_cats'
4
+
5
+ require File.expand_path('../spec/test_app', __FILE__)
6
+
7
+ use StatusCats
8
+ run TestApp.new
@@ -0,0 +1,31 @@
1
+ require 'rack/file'
2
+
3
+ class StatusCats
4
+
5
+ CATS = [ 100, 200, 206, 207, 300, 301, 302, 303, 307, 401, 402, 403, 404, 405, 406, 408, 409, 410, 413, 414, 416, 417, 418, 422, 423, 424, 426, 429, 431, 444, 450, 500, 502, 507, 508, 599 ].freeze
6
+ CATS_DIR = File.expand_path("../../cats", __FILE__)
7
+
8
+ def initialize(app, options = {})
9
+ @app, @options = app, options
10
+ end
11
+
12
+ def call(env)
13
+ @status, @headers, @response = @app.call(env)
14
+ if display_cat?
15
+ _, @headers, @response = Rack::File.new(CATS_DIR).call("PATH_INFO" => "#{@status}.jpg")
16
+ end
17
+ [ @status, @headers, @response ]
18
+ end
19
+
20
+ def display_cat?
21
+ cats = CATS
22
+ if @options.has_key?(:only)
23
+ cats = Array(@options[:only]) & cats
24
+ end
25
+ if @options.has_key?(:except)
26
+ cats = cats - Array(@options[:except])
27
+ end
28
+ cats.include?(@status.to_i)
29
+ end
30
+
31
+ end
@@ -0,0 +1,21 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'rack/test'
4
+ require 'status_cats'
5
+ require 'test_app'
6
+
7
+ RSpec::Matchers.define :be_replaced_with_cat do |code|
8
+ match do |actual|
9
+ if code.nil?
10
+ actual.body != "Hello, World!"
11
+ else
12
+ path = File.expand_path("../../cats/#{code}.jpg", __FILE__)
13
+ file = File.open(path, 'rb')
14
+ actual.body == file.read &&
15
+ actual.status == code &&
16
+ actual.headers["Content-Length"] == file.size.to_s &&
17
+ actual.headers["Content-Type"] == "image/jpeg"
18
+ end
19
+ end
20
+
21
+ end
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+
3
+ describe StatusCats do
4
+
5
+ include Rack::Test::Methods
6
+
7
+ describe "cat responses" do
8
+
9
+ let(:app) { StatusCats.new(TestApp.new) }
10
+
11
+ StatusCats::CATS.each do |code|
12
+
13
+ it "displays cat for status #{code}" do
14
+ get code.to_s
15
+ last_response.should be_replaced_with_cat(code)
16
+ end
17
+
18
+ end
19
+
20
+ it "won't replace response with a cat if there is no file for it" do
21
+ get "201"
22
+ last_response.should_not be_replaced_with_cat
23
+ end
24
+
25
+ end
26
+
27
+ describe ":only" do
28
+
29
+ let(:app) { StatusCats.new(TestApp.new, :only => 200...300) }
30
+
31
+ it "will replace response with a cat if status is within range" do
32
+ get "206"
33
+ last_response.should be_replaced_with_cat(206)
34
+ end
35
+
36
+ it "will not replace if there is no file for it" do
37
+ get "201"
38
+ last_response.should_not be_replaced_with_cat
39
+ end
40
+
41
+ it "will not replace if out of range" do
42
+ get "404"
43
+ last_response.should_not be_replaced_with_cat(404)
44
+ end
45
+
46
+ end
47
+
48
+ describe ":except" do
49
+
50
+ let(:app) { StatusCats.new(TestApp.new, :except => 200...300) }
51
+
52
+ it "will replace response with a cat if status is not in range" do
53
+ get "404"
54
+ last_response.should be_replaced_with_cat(404)
55
+ end
56
+
57
+ it "will not replace response with a cat if status is in range" do
58
+ get "206"
59
+ last_response.should_not be_replaced_with_cat(206)
60
+ end
61
+
62
+ it "won't replace cats that are not known" do
63
+ get "201"
64
+ last_response.should_not be_replaced_with_cat
65
+ end
66
+
67
+ end
68
+
69
+ end
@@ -0,0 +1,11 @@
1
+ class TestApp
2
+
3
+ def call(env)
4
+ status = env["PATH_INFO"].gsub(/\D/, '').to_i
5
+ status = 404 if status < 100
6
+ headers = {"Content-Type" => "text/html"}
7
+ body = ["Hello, World!"]
8
+ [ status, headers, body ]
9
+ end
10
+
11
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "status_cats"
5
+ s.version = "0.0.1"
6
+ s.authors = ["iain"]
7
+ s.email = ["iain@iain.nl"]
8
+ s.homepage = "https://github.com/iain/status_cats"
9
+ s.summary = %q{Rack middleware; replaces responses with cats}
10
+ s.description = %q{Rack middleware replacing responses with relevant pictures of cats, based on the status code.}
11
+
12
+ s.rubyforge_project = "status_cats"
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+
19
+ # specify any dependencies here; for example:
20
+ s.add_development_dependency "rspec", ">= 2"
21
+ s.add_development_dependency "rack-test"
22
+ s.add_runtime_dependency "rack"
23
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: status_cats
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - iain
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &2163992540 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '2'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *2163992540
25
+ - !ruby/object:Gem::Dependency
26
+ name: rack-test
27
+ requirement: &2163992140 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2163992140
36
+ - !ruby/object:Gem::Dependency
37
+ name: rack
38
+ requirement: &2163991680 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *2163991680
47
+ description: Rack middleware replacing responses with relevant pictures of cats, based
48
+ on the status code.
49
+ email:
50
+ - iain@iain.nl
51
+ executables: []
52
+ extensions: []
53
+ extra_rdoc_files: []
54
+ files:
55
+ - .gitignore
56
+ - .rspec
57
+ - .rvmrc
58
+ - Gemfile
59
+ - README.md
60
+ - Rakefile
61
+ - cats/100.jpg
62
+ - cats/200.jpg
63
+ - cats/206.jpg
64
+ - cats/207.jpg
65
+ - cats/300 (1).jpg
66
+ - cats/300.jpg
67
+ - cats/301.jpg
68
+ - cats/302.jpg
69
+ - cats/303.jpg
70
+ - cats/307.jpg
71
+ - cats/401.jpg
72
+ - cats/402.jpg
73
+ - cats/403.jpg
74
+ - cats/404.jpg
75
+ - cats/405.jpg
76
+ - cats/406.jpg
77
+ - cats/408.jpg
78
+ - cats/409.jpg
79
+ - cats/410.jpg
80
+ - cats/413.jpg
81
+ - cats/414.jpg
82
+ - cats/416.jpg
83
+ - cats/417.jpg
84
+ - cats/418.jpg
85
+ - cats/422.jpg
86
+ - cats/423.jpg
87
+ - cats/424.jpg
88
+ - cats/426.jpg
89
+ - cats/429.jpg
90
+ - cats/431.jpg
91
+ - cats/444.jpg
92
+ - cats/450.jpg
93
+ - cats/500.jpg
94
+ - cats/502.jpg
95
+ - cats/507.jpg
96
+ - cats/508.jpg
97
+ - cats/599.jpg
98
+ - config.ru
99
+ - lib/status_cats.rb
100
+ - spec/spec_helper.rb
101
+ - spec/status_cats_spec.rb
102
+ - spec/test_app.rb
103
+ - status_cats.gemspec
104
+ homepage: https://github.com/iain/status_cats
105
+ licenses: []
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ! '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ none: false
118
+ requirements:
119
+ - - ! '>='
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project: status_cats
124
+ rubygems_version: 1.8.10
125
+ signing_key:
126
+ specification_version: 3
127
+ summary: Rack middleware; replaces responses with cats
128
+ test_files:
129
+ - spec/spec_helper.rb
130
+ - spec/status_cats_spec.rb
131
+ - spec/test_app.rb