status_dogs 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. data/.gitignore +4 -0
  2. data/.rspec +2 -0
  3. data/.travis.yml +3 -0
  4. data/Gemfile +4 -0
  5. data/README.md +69 -0
  6. data/Rakefile +7 -0
  7. data/config.ru +8 -0
  8. data/dogs/100.jpg +0 -0
  9. data/dogs/200.jpg +0 -0
  10. data/dogs/201.jpg +0 -0
  11. data/dogs/202.jpg +0 -0
  12. data/dogs/203.jpg +0 -0
  13. data/dogs/204.jpg +0 -0
  14. data/dogs/206.jpg +0 -0
  15. data/dogs/300.jpg +0 -0
  16. data/dogs/301.jpg +0 -0
  17. data/dogs/302.jpg +0 -0
  18. data/dogs/303.jpg +0 -0
  19. data/dogs/304.jpg +0 -0
  20. data/dogs/305.jpg +0 -0
  21. data/dogs/306.jpg +0 -0
  22. data/dogs/400.jpg +0 -0
  23. data/dogs/401.jpg +0 -0
  24. data/dogs/402.jpg +0 -0
  25. data/dogs/403.jpg +0 -0
  26. data/dogs/404.jpg +0 -0
  27. data/dogs/405.jpg +0 -0
  28. data/dogs/406.jpg +0 -0
  29. data/dogs/408.jpg +0 -0
  30. data/dogs/409.jpg +0 -0
  31. data/dogs/410.jpg +0 -0
  32. data/dogs/411.jpg +0 -0
  33. data/dogs/413.jpg +0 -0
  34. data/dogs/414.jpg +0 -0
  35. data/dogs/416.jpg +0 -0
  36. data/dogs/417.jpg +0 -0
  37. data/dogs/418.jpg +0 -0
  38. data/dogs/420.jpg +0 -0
  39. data/dogs/422.jpg +0 -0
  40. data/dogs/423.jpg +0 -0
  41. data/dogs/424.jpg +0 -0
  42. data/dogs/425.jpg +0 -0
  43. data/dogs/426.jpg +0 -0
  44. data/dogs/429.jpg +0 -0
  45. data/dogs/431.jpg +0 -0
  46. data/dogs/444.jpg +0 -0
  47. data/dogs/450.jpg +0 -0
  48. data/dogs/500.jpg +0 -0
  49. data/dogs/501.jpg +0 -0
  50. data/dogs/502.jpg +0 -0
  51. data/dogs/503.jpg +0 -0
  52. data/dogs/504.jpg +0 -0
  53. data/dogs/506.jpg +0 -0
  54. data/dogs/507.jpg +0 -0
  55. data/dogs/508.jpg +0 -0
  56. data/lib/status_dogs.rb +31 -0
  57. data/spec/spec_helper.rb +21 -0
  58. data/spec/status_dogs_spec.rb +69 -0
  59. data/spec/test_app.rb +11 -0
  60. data/status_dogs.gemspec +22 -0
  61. metadata +173 -0
data/.gitignore ADDED
@@ -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/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in status_dogs.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # Status Dogs
2
+
3
+ Rack middleware replacing responses with relevant pictures of dogs, based on the status code.
4
+
5
+ The pictures are made/collected by [Mike Lee](https://twitter.com/mikeleeorg) on [HTTP Status Dogs](http://httpstatusdogs.com/).
6
+
7
+ This is a shameless fork of [status_cats](https://github.com/iain/status_cats) by @iain.
8
+
9
+ ## Usage
10
+
11
+ Install:
12
+
13
+ ``` sh
14
+ gem install status_dogs
15
+ ```
16
+
17
+ Place inside your rackup file:
18
+
19
+ ``` ruby
20
+ require 'status_dogs'
21
+ use StatusDogs
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 dogs, by using the `:only` and `:except`
27
+ options, which take arrays or ranges.
28
+
29
+ ``` ruby
30
+ use StatusDogs, :only => [ 403, 404, 500 ]
31
+
32
+ # or ...
33
+
34
+ use StatusDogs, :except => 200...300
35
+ ```
36
+
37
+ ## Development
38
+
39
+ Install dependencies:
40
+
41
+ ``` sh
42
+ bundle install
43
+ ```
44
+
45
+ Run tests:
46
+
47
+ ``` sh
48
+ rake
49
+ ```
50
+
51
+ You can run the demo application:
52
+
53
+ ``` sh
54
+ bundle exec rackup
55
+ ```
56
+
57
+ The application will trigger the response code depending on the path you visit, so visiting
58
+ [http://localhost:9292/418](http://localhost:9292/418) will respond with a teapot.
59
+
60
+
61
+ ## Known issues
62
+
63
+ Status codes `100` (Continue), `204` (No Content) and `304` (Not Modified) cannot have a response body. Triggering them will cause Rack to throw up.
64
+
65
+ ## License
66
+
67
+ The Ruby code in this project is released under the MIT License.
68
+
69
+ The pictures of dogs are copywrited by [Mike Lee](https://twitter.com/mikeleeorg) from http://httpstatusdogs.com/
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
data/config.ru ADDED
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'status_dogs'
4
+
5
+ require File.expand_path('../spec/test_app', __FILE__)
6
+
7
+ use StatusDogs
8
+ run TestApp.new
data/dogs/100.jpg ADDED
Binary file
data/dogs/200.jpg ADDED
Binary file
data/dogs/201.jpg ADDED
Binary file
data/dogs/202.jpg ADDED
Binary file
data/dogs/203.jpg ADDED
Binary file
data/dogs/204.jpg ADDED
Binary file
data/dogs/206.jpg ADDED
Binary file
data/dogs/300.jpg ADDED
Binary file
data/dogs/301.jpg ADDED
Binary file
data/dogs/302.jpg ADDED
Binary file
data/dogs/303.jpg ADDED
Binary file
data/dogs/304.jpg ADDED
Binary file
data/dogs/305.jpg ADDED
Binary file
data/dogs/306.jpg ADDED
Binary file
data/dogs/400.jpg ADDED
Binary file
data/dogs/401.jpg ADDED
Binary file
data/dogs/402.jpg ADDED
Binary file
data/dogs/403.jpg ADDED
Binary file
data/dogs/404.jpg ADDED
Binary file
data/dogs/405.jpg ADDED
Binary file
data/dogs/406.jpg ADDED
Binary file
data/dogs/408.jpg ADDED
Binary file
data/dogs/409.jpg ADDED
Binary file
data/dogs/410.jpg ADDED
Binary file
data/dogs/411.jpg ADDED
Binary file
data/dogs/413.jpg ADDED
Binary file
data/dogs/414.jpg ADDED
Binary file
data/dogs/416.jpg ADDED
Binary file
data/dogs/417.jpg ADDED
Binary file
data/dogs/418.jpg ADDED
Binary file
data/dogs/420.jpg ADDED
Binary file
data/dogs/422.jpg ADDED
Binary file
data/dogs/423.jpg ADDED
Binary file
data/dogs/424.jpg ADDED
Binary file
data/dogs/425.jpg ADDED
Binary file
data/dogs/426.jpg ADDED
Binary file
data/dogs/429.jpg ADDED
Binary file
data/dogs/431.jpg ADDED
Binary file
data/dogs/444.jpg ADDED
Binary file
data/dogs/450.jpg ADDED
Binary file
data/dogs/500.jpg ADDED
Binary file
data/dogs/501.jpg ADDED
Binary file
data/dogs/502.jpg ADDED
Binary file
data/dogs/503.jpg ADDED
Binary file
data/dogs/504.jpg ADDED
Binary file
data/dogs/506.jpg ADDED
Binary file
data/dogs/507.jpg ADDED
Binary file
data/dogs/508.jpg ADDED
Binary file
@@ -0,0 +1,31 @@
1
+ require 'rack/file'
2
+
3
+ class StatusDogs
4
+
5
+ DOGS = [ 100, 200, 201, 202, 203, 206, 300, 301, 302, 303, 305, 306, 400, 401, 402, 403, 404, 405, 406, 408, 409, 410, 411, 413, 414, 416, 417, 418, 420, 422, 423, 424, 425, 426, 429, 431, 444, 450, 500, 501, 502, 503, 504, 506, 507, 508 ].freeze
6
+ DOGS_DIR = File.expand_path("../../dogs", __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_dog?
15
+ _, @headers, @response = Rack::File.new(DOGS_DIR).call("REQUEST_METHOD" => "GET", "PATH_INFO" => "#{@status}.jpg")
16
+ end
17
+ [ @status, @headers, @response ]
18
+ end
19
+
20
+ def display_dog?
21
+ dogs = DOGS
22
+ if @options.has_key?(:only)
23
+ dogs = Array(@options[:only]) & dogs
24
+ end
25
+ if @options.has_key?(:except)
26
+ dogs = dogs - Array(@options[:except])
27
+ end
28
+ dogs.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_dogs'
5
+ require 'test_app'
6
+
7
+ RSpec::Matchers.define :be_replaced_with_dog do |code|
8
+ match do |actual|
9
+ if code.nil?
10
+ actual.body != "Hello, World!"
11
+ else
12
+ path = File.expand_path("../../dogs/#{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 StatusDogs do
4
+
5
+ include Rack::Test::Methods
6
+
7
+ describe "dog responses" do
8
+
9
+ let(:app) { StatusDogs.new(TestApp.new) }
10
+
11
+ StatusDogs::DOGS.each do |code|
12
+
13
+ it "displays dog for status #{code}" do
14
+ get code.to_s
15
+ last_response.should be_replaced_with_dog(code)
16
+ end
17
+
18
+ end
19
+
20
+ it "won't replace response with a dog if there is no file for it" do
21
+ get "600"
22
+ last_response.should_not be_replaced_with_dog
23
+ end
24
+
25
+ end
26
+
27
+ describe ":only" do
28
+
29
+ let(:app) { StatusDogs.new(TestApp.new, :only => 200...300) }
30
+
31
+ it "will replace response with a dog if status is within range" do
32
+ get "206"
33
+ last_response.should be_replaced_with_dog(206)
34
+ end
35
+
36
+ it "will not replace if there is no file for it" do
37
+ get "299"
38
+ last_response.should_not be_replaced_with_dog
39
+ end
40
+
41
+ it "will not replace if out of range" do
42
+ get "404"
43
+ last_response.should_not be_replaced_with_dog(404)
44
+ end
45
+
46
+ end
47
+
48
+ describe ":except" do
49
+
50
+ let(:app) { StatusDogs.new(TestApp.new, :except => 200...300) }
51
+
52
+ it "will replace response with a dog if status is not in range" do
53
+ get "404"
54
+ last_response.should be_replaced_with_dog(404)
55
+ end
56
+
57
+ it "will not replace response with a dog if status is in range" do
58
+ get "206"
59
+ last_response.should_not be_replaced_with_dog(206)
60
+ end
61
+
62
+ it "won't replace dogs that are not known" do
63
+ get "201"
64
+ last_response.should_not be_replaced_with_dog
65
+ end
66
+
67
+ end
68
+
69
+ end
data/spec/test_app.rb ADDED
@@ -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,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "status_dogs"
5
+ s.version = "0.0.1"
6
+ s.authors = ["Jan Vlnas", "iain"]
7
+ s.email = ["gems@jan.vlnas.cz", "iain@iain.nl"]
8
+ s.homepage = "https://github.com/jnv/status_dogs"
9
+ s.summary = %q{Rack middleware; replaces responses with dogs}
10
+ s.description = %q{Rack middleware replacing responses with relevant pictures of dogs, based on the status code.}
11
+
12
+ s.files = `git ls-files`.split("\n")
13
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
15
+ s.require_paths = ["lib"]
16
+
17
+ # specify any dependencies here; for example:
18
+ s.add_development_dependency "rspec", ">= 2"
19
+ s.add_development_dependency "rack-test"
20
+ s.add_development_dependency "rake"
21
+ s.add_runtime_dependency "rack"
22
+ end
metadata ADDED
@@ -0,0 +1,173 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: status_dogs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jan Vlnas
9
+ - iain
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-12-11 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ type: :development
17
+ name: rspec
18
+ prerelease: false
19
+ requirement: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ! '>='
22
+ - !ruby/object:Gem::Version
23
+ version: '2'
24
+ none: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '2'
30
+ none: false
31
+ - !ruby/object:Gem::Dependency
32
+ type: :development
33
+ name: rack-test
34
+ prerelease: false
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ none: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ none: false
47
+ - !ruby/object:Gem::Dependency
48
+ type: :development
49
+ name: rake
50
+ prerelease: false
51
+ requirement: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ none: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ none: false
63
+ - !ruby/object:Gem::Dependency
64
+ type: :runtime
65
+ name: rack
66
+ prerelease: false
67
+ requirement: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ none: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ none: false
79
+ description: Rack middleware replacing responses with relevant pictures of dogs, based
80
+ on the status code.
81
+ email:
82
+ - gems@jan.vlnas.cz
83
+ - iain@iain.nl
84
+ executables: []
85
+ extensions: []
86
+ extra_rdoc_files: []
87
+ files:
88
+ - .gitignore
89
+ - .rspec
90
+ - .travis.yml
91
+ - Gemfile
92
+ - README.md
93
+ - Rakefile
94
+ - config.ru
95
+ - dogs/100.jpg
96
+ - dogs/200.jpg
97
+ - dogs/201.jpg
98
+ - dogs/202.jpg
99
+ - dogs/203.jpg
100
+ - dogs/204.jpg
101
+ - dogs/206.jpg
102
+ - dogs/300.jpg
103
+ - dogs/301.jpg
104
+ - dogs/302.jpg
105
+ - dogs/303.jpg
106
+ - dogs/304.jpg
107
+ - dogs/305.jpg
108
+ - dogs/306.jpg
109
+ - dogs/400.jpg
110
+ - dogs/401.jpg
111
+ - dogs/402.jpg
112
+ - dogs/403.jpg
113
+ - dogs/404.jpg
114
+ - dogs/405.jpg
115
+ - dogs/406.jpg
116
+ - dogs/408.jpg
117
+ - dogs/409.jpg
118
+ - dogs/410.jpg
119
+ - dogs/411.jpg
120
+ - dogs/413.jpg
121
+ - dogs/414.jpg
122
+ - dogs/416.jpg
123
+ - dogs/417.jpg
124
+ - dogs/418.jpg
125
+ - dogs/420.jpg
126
+ - dogs/422.jpg
127
+ - dogs/423.jpg
128
+ - dogs/424.jpg
129
+ - dogs/425.jpg
130
+ - dogs/426.jpg
131
+ - dogs/429.jpg
132
+ - dogs/431.jpg
133
+ - dogs/444.jpg
134
+ - dogs/450.jpg
135
+ - dogs/500.jpg
136
+ - dogs/501.jpg
137
+ - dogs/502.jpg
138
+ - dogs/503.jpg
139
+ - dogs/504.jpg
140
+ - dogs/506.jpg
141
+ - dogs/507.jpg
142
+ - dogs/508.jpg
143
+ - lib/status_dogs.rb
144
+ - spec/spec_helper.rb
145
+ - spec/status_dogs_spec.rb
146
+ - spec/test_app.rb
147
+ - status_dogs.gemspec
148
+ homepage: https://github.com/jnv/status_dogs
149
+ licenses: []
150
+ post_install_message:
151
+ rdoc_options: []
152
+ require_paths:
153
+ - lib
154
+ required_ruby_version: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - ! '>='
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ none: false
160
+ required_rubygems_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ! '>='
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ none: false
166
+ requirements: []
167
+ rubyforge_project:
168
+ rubygems_version: 1.8.23
169
+ signing_key:
170
+ specification_version: 3
171
+ summary: Rack middleware; replaces responses with dogs
172
+ test_files: []
173
+ has_rdoc: