http-cat-rails 1.0.0 → 2.0.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.
- checksums.yaml +4 -4
- data/README.md +101 -4
- data/app/controllers/errors_controller.rb +1 -1
- data/app/views/errors/error.html.erb +1 -5
- data/app/views/layouts/layout_default.html.erb +5 -0
- data/lib/http/cat/configuration.rb +11 -2
- data/lib/http/cat/rails/version.rb +1 -1
- data/lib/http/cat/rails.rb +1 -0
- data/lib/http/cat/static_page.rb +37 -0
- data/lib/tasks/http/cat/rails_tasks.rake +16 -4
- metadata +26 -10
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 5c225ac7bccff14f39c6835fd933f4f2b43e9fa49c571c95060864cc90f3639a
|
|
4
|
+
data.tar.gz: 35f190ea28763b2f74dfe62e0c9773338949595b57c7668c4bf2aa228b1f2995
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: '07776684bffaad475619e00b90513f53fca45f38516db4897fba6f235d7119a30b40f04455a66daa144a061e8ee0ef62a5ca266648aa1d881e0b803375df8fb1'
|
|
7
|
+
data.tar.gz: d355d14a3bba21df8f98adb0e9f0ea5b5debd2629bdce2e1d59daeaa9401e0c0808a5295d1dfe4e38528e9e33e95d85ff5f342c6645e1023fe6c0895eb13a4eb
|
data/README.md
CHANGED
|
@@ -1,10 +1,62 @@
|
|
|
1
1
|
# Http::Cat::Rails
|
|
2
|
-
Short description and motivation.
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
http-cat-rails is a configurable gem that changes every http-error page in your rails application with an image of a cute
|
|
4
|
+
little cat, that describes _purrfectly_ the error code (credits for the default images to https://http.cat)
|
|
5
|
+
|
|
6
|
+

|
|
7
|
+
|
|
8
|
+
## Configuration
|
|
9
|
+
|
|
10
|
+
This gem is highly customizable: you could even decide to self host your own error images, and use them.
|
|
11
|
+
To change this gem's behaviour, you can create a http_cat_rails.rb inside /config/initializers, and structure it as
|
|
12
|
+
follows:
|
|
13
|
+
|
|
14
|
+
```ruby
|
|
15
|
+
Http::Cat::Rails.configure do |c|
|
|
16
|
+
c.base_url = "https://www.myurl.com/"
|
|
17
|
+
c.status_codes = { "404": "code_name", "500": "code_name_2" }
|
|
18
|
+
c.layout = "application"
|
|
19
|
+
c.extension = :jpg
|
|
20
|
+
end
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
In this example, if an error 404 is raised, your page will load an image inside your application.html.erb from the following
|
|
24
|
+
url: "https://www.myurl.com/404.jpg"
|
|
25
|
+
|
|
26
|
+
### Base Url (default: "https://http.cat/")
|
|
27
|
+
|
|
28
|
+
This option is used to set the base url to retrieve images
|
|
29
|
+
|
|
30
|
+
### Status Codes (default: _a very long hash_)
|
|
31
|
+
|
|
32
|
+
This option is used to pass an Hash of status codes, to limit them, or change the url visited. The value of each
|
|
33
|
+
element must be **unique**, since it's used to define a function.
|
|
34
|
+
|
|
35
|
+
### Layout (default: "layout_default")
|
|
36
|
+
|
|
37
|
+
- "application" to embed http error page inside your application.html.erb
|
|
38
|
+
- false to don't have a layout
|
|
39
|
+
- "layout_default" (default option: just a black background)
|
|
40
|
+
- "custom_error_layout" to embed http error page inside "/views/layouts/custom_error_layout.html.erb"
|
|
41
|
+
|
|
42
|
+
### Extension (default: :jpg)
|
|
43
|
+
|
|
44
|
+
Extension to insert in the url. Can also be false or nil, to dont include an extension
|
|
45
|
+
|
|
46
|
+
### CSS
|
|
47
|
+
|
|
48
|
+
The img tag that contains the image has the following class: "http_error_image"
|
|
49
|
+
You can customize its style using that
|
|
50
|
+
|
|
51
|
+
## Summary
|
|
52
|
+
|
|
53
|
+
url_to_visit:
|
|
54
|
+
```ruby
|
|
55
|
+
"#{base_url}#{code_of_this_error}#{".#{extension}" if extension}"
|
|
56
|
+
```
|
|
6
57
|
|
|
7
58
|
## Installation
|
|
59
|
+
|
|
8
60
|
Add this line to your application's Gemfile:
|
|
9
61
|
|
|
10
62
|
```ruby
|
|
@@ -12,17 +64,62 @@ gem "http-cat-rails"
|
|
|
12
64
|
```
|
|
13
65
|
|
|
14
66
|
And then execute:
|
|
67
|
+
|
|
15
68
|
```bash
|
|
16
69
|
$ bundle
|
|
17
70
|
```
|
|
18
71
|
|
|
19
72
|
Or install it yourself as:
|
|
73
|
+
|
|
20
74
|
```bash
|
|
21
75
|
$ gem install http-cat-rails
|
|
22
76
|
```
|
|
23
77
|
|
|
78
|
+
Then, you'll need to add this line to your application.rb:
|
|
79
|
+
|
|
80
|
+
```ruby
|
|
81
|
+
config.exceptions_app = self.routes
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Troubleshooting
|
|
85
|
+
|
|
86
|
+
This gem won't work if your application.rb or <your_environment>.rb contains the following line
|
|
87
|
+
|
|
88
|
+
```ruby
|
|
89
|
+
config.consider_all_requests_local = true
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## Static error pages
|
|
93
|
+
|
|
94
|
+
Some errors (e.g. `500`) shouldn't depend on Rails to be displayed: if Rails itself is broken, rendering the error page
|
|
95
|
+
can raise another 500 and loop. For these, generate a self-contained HTML file (CSS inline, image loaded from your
|
|
96
|
+
`base_url`) into your app's `public/` folder:
|
|
97
|
+
|
|
98
|
+
```bash
|
|
99
|
+
$ bin/rails "http_cat:generate[500,502,503]"
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
- Writes `public/<code>.html` for each code, **overwriting** any existing file with the same name
|
|
103
|
+
- Codes must be keys of `status_codes`, otherwise the task aborts and writes nothing
|
|
104
|
+
- Uses `base_url`, `extension` and `status_codes` from your configuration; `layout` is ignored (the page has its own
|
|
105
|
+
black background, and the image keeps the `http_error_image` class)
|
|
106
|
+
- Re-run it after changing the configuration
|
|
107
|
+
|
|
108
|
+
The generated files are not used automatically while `exceptions_app` is set to your routes: serve them from your web
|
|
109
|
+
server instead, e.g. nginx: `error_page 500 502 503 /500.html;` (one `error_page` per code, with its own file).
|
|
110
|
+
|
|
111
|
+
## Development
|
|
112
|
+
|
|
113
|
+
Tests run with RSpec, against a minimal Rails app, on each supported Rails version:
|
|
114
|
+
|
|
115
|
+
```bash
|
|
116
|
+
$ BUNDLE_GEMFILE=gemfiles/rails_8.1.2.gemfile bundle install
|
|
117
|
+
$ BUNDLE_GEMFILE=gemfiles/rails_8.1.2.gemfile bundle exec rspec
|
|
118
|
+
```
|
|
119
|
+
|
|
24
120
|
## Contributing
|
|
25
|
-
|
|
121
|
+
|
|
122
|
+
It's a small project, but if you want to contribute feel free to send a Pull Request, or open an Issue
|
|
26
123
|
|
|
27
124
|
## License
|
|
28
125
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
@@ -2,7 +2,7 @@ class ErrorsController < ApplicationController
|
|
|
2
2
|
Http::Cat::Rails.status_codes.each do |code, desc|
|
|
3
3
|
define_method(desc.downcase.gsub(' ', '_')) do
|
|
4
4
|
@code = code
|
|
5
|
-
render
|
|
5
|
+
render "error", layout: Http::Cat::Rails.layout, status: code.to_s.to_i
|
|
6
6
|
end
|
|
7
7
|
end
|
|
8
8
|
end
|
|
@@ -1,5 +1 @@
|
|
|
1
|
-
|
|
2
|
-
<div style="width: 100vw; height: 100vh; background: black; display: flex; align-items: center; justify-content: center">
|
|
3
|
-
<%= image_tag "#{Http::Cat::Rails.base_url}#{@code}.jpg", width: "50%" %>
|
|
4
|
-
</div>
|
|
5
|
-
</body>
|
|
1
|
+
<%= image_tag "#{Http::Cat::Rails.base_url}#{@code}#{".#{Http::Cat::Rails.extension}" if Http::Cat::Rails.extension}", width: "50%", class: "http_error_image" %>
|
|
@@ -28,10 +28,12 @@ module Http
|
|
|
28
28
|
'509': 'Bandwidth Limit Exceeded', '510': 'Not Extended', '511': 'Network Authentication Required',
|
|
29
29
|
'521': 'Web Server Is Down', '522': 'Connection Timed Out', '523': 'Origin Is Unreachable',
|
|
30
30
|
'525': 'SSL Handshake Failed', '530': 'Site Frozen', '599': 'Network Connect Timeout Error' },
|
|
31
|
-
base_url: "https://http.cat/"
|
|
31
|
+
base_url: "https://http.cat/",
|
|
32
|
+
extension: :jpg,
|
|
33
|
+
layout: "layout_default"
|
|
32
34
|
}.freeze
|
|
33
35
|
|
|
34
|
-
attr_writer :status_codes, :base_url
|
|
36
|
+
attr_writer :status_codes, :base_url, :extension, :layout
|
|
35
37
|
|
|
36
38
|
def configure
|
|
37
39
|
yield self
|
|
@@ -45,6 +47,13 @@ module Http
|
|
|
45
47
|
@base_url || DEFAULTS[:base_url]
|
|
46
48
|
end
|
|
47
49
|
|
|
50
|
+
def extension
|
|
51
|
+
@extension || DEFAULTS[:extension]
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def layout
|
|
55
|
+
@layout || DEFAULTS[:layout]
|
|
56
|
+
end
|
|
48
57
|
end
|
|
49
58
|
end
|
|
50
59
|
end
|
data/lib/http/cat/rails.rb
CHANGED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
require "erb"
|
|
2
|
+
|
|
3
|
+
module Http
|
|
4
|
+
module Cat
|
|
5
|
+
module Rails
|
|
6
|
+
# Self-contained HTML (inline CSS, no Rails/asset pipeline needed to serve it)
|
|
7
|
+
TEMPLATE = ERB.new(<<~HTML, trim_mode: "-")
|
|
8
|
+
<!DOCTYPE html>
|
|
9
|
+
<html>
|
|
10
|
+
<head>
|
|
11
|
+
<meta charset="utf-8">
|
|
12
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
13
|
+
<title><%= code %> <%= title %></title>
|
|
14
|
+
<style>
|
|
15
|
+
body { margin: 0; }
|
|
16
|
+
.http_error_container { width: 100vw; height: 100vh; background: black; display: flex; align-items: center; justify-content: center; }
|
|
17
|
+
.http_error_image { width: 50%; }
|
|
18
|
+
</style>
|
|
19
|
+
</head>
|
|
20
|
+
<body>
|
|
21
|
+
<div class="http_error_container">
|
|
22
|
+
<img class="http_error_image" src="<%= src %>" alt="<%= code %> <%= title %>">
|
|
23
|
+
</div>
|
|
24
|
+
</body>
|
|
25
|
+
</html>
|
|
26
|
+
HTML
|
|
27
|
+
|
|
28
|
+
def self.static_html(code)
|
|
29
|
+
code = code.to_s
|
|
30
|
+
title = status_codes.fetch(code.to_sym) { status_codes.fetch(code) { raise ArgumentError, "unknown status code: #{code}" } }
|
|
31
|
+
ext = extension ? ".#{extension}" : ""
|
|
32
|
+
h = ERB::Util.method(:html_escape)
|
|
33
|
+
TEMPLATE.result_with_hash(code: h.(code), title: h.(title), src: h.("#{base_url}#{code}#{ext}"))
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -1,4 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
namespace :http_cat do
|
|
2
|
+
desc "Generate static public/<code>.html error pages (overwrites existing). Usage: rake \"http_cat:generate[500,502]\""
|
|
3
|
+
task :generate, [:code] => :environment do |_, args|
|
|
4
|
+
codes = [args[:code], *args.extras].compact.map(&:to_s)
|
|
5
|
+
abort "No status code given. Usage: rake \"http_cat:generate[500,502]\"" if codes.empty?
|
|
6
|
+
|
|
7
|
+
unknown = codes.reject { |c| Http::Cat::Rails.status_codes.key?(c.to_sym) || Http::Cat::Rails.status_codes.key?(c) }
|
|
8
|
+
abort "Unknown status code(s): #{unknown.join(', ')}" if unknown.any?
|
|
9
|
+
|
|
10
|
+
codes.each do |code|
|
|
11
|
+
path = Rails.public_path.join("#{code}.html")
|
|
12
|
+
File.write(path, Http::Cat::Rails.static_html(code))
|
|
13
|
+
puts "Written #{path}"
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
metadata
CHANGED
|
@@ -1,29 +1,43 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: http-cat-rails
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 2.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Gamberi Elia
|
|
8
|
-
autorequire:
|
|
8
|
+
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2026-12-01 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: rspec
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '3.13'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '3.13'
|
|
13
27
|
- !ruby/object:Gem::Dependency
|
|
14
28
|
name: rails
|
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
|
16
30
|
requirements:
|
|
17
31
|
- - ">="
|
|
18
32
|
- !ruby/object:Gem::Version
|
|
19
|
-
version:
|
|
33
|
+
version: '6.1'
|
|
20
34
|
type: :runtime
|
|
21
35
|
prerelease: false
|
|
22
36
|
version_requirements: !ruby/object:Gem::Requirement
|
|
23
37
|
requirements:
|
|
24
38
|
- - ">="
|
|
25
39
|
- !ruby/object:Gem::Version
|
|
26
|
-
version:
|
|
40
|
+
version: '6.1'
|
|
27
41
|
description: Configurable Ruby gem that replaces error pages with http.cat image
|
|
28
42
|
email:
|
|
29
43
|
- me@gimbaro.dev
|
|
@@ -36,20 +50,22 @@ files:
|
|
|
36
50
|
- Rakefile
|
|
37
51
|
- app/controllers/errors_controller.rb
|
|
38
52
|
- app/views/errors/error.html.erb
|
|
53
|
+
- app/views/layouts/layout_default.html.erb
|
|
39
54
|
- config/routes.rb
|
|
40
55
|
- lib/http/cat/configuration.rb
|
|
41
56
|
- lib/http/cat/rails.rb
|
|
42
57
|
- lib/http/cat/rails/engine.rb
|
|
43
58
|
- lib/http/cat/rails/version.rb
|
|
59
|
+
- lib/http/cat/static_page.rb
|
|
44
60
|
- lib/tasks/http/cat/rails_tasks.rake
|
|
45
|
-
homepage:
|
|
61
|
+
homepage:
|
|
46
62
|
licenses:
|
|
47
63
|
- MIT
|
|
48
64
|
metadata:
|
|
49
65
|
allowed_push_host: https://rubygems.org
|
|
50
66
|
homepage_uri: https://github.com/Gimbardo/http-cat-rails
|
|
51
67
|
source_code_uri: https://github.com/Gimbardo/http-cat-rails
|
|
52
|
-
post_install_message:
|
|
68
|
+
post_install_message:
|
|
53
69
|
rdoc_options: []
|
|
54
70
|
require_paths:
|
|
55
71
|
- lib
|
|
@@ -57,15 +73,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
57
73
|
requirements:
|
|
58
74
|
- - ">="
|
|
59
75
|
- !ruby/object:Gem::Version
|
|
60
|
-
version: '
|
|
76
|
+
version: '2.7'
|
|
61
77
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
62
78
|
requirements:
|
|
63
79
|
- - ">="
|
|
64
80
|
- !ruby/object:Gem::Version
|
|
65
81
|
version: '0'
|
|
66
82
|
requirements: []
|
|
67
|
-
rubygems_version: 3.
|
|
68
|
-
signing_key:
|
|
83
|
+
rubygems_version: 3.4.19
|
|
84
|
+
signing_key:
|
|
69
85
|
specification_version: 4
|
|
70
86
|
summary: Configurable Ruby gem that replaces error pages with http.cat image
|
|
71
87
|
test_files: []
|