httpstatus-rails 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +195 -0
- data/Rakefile +27 -0
- data/app/views/http/status.atom.erb +5 -0
- data/app/views/http/status.csv.erb +1 -0
- data/app/views/http/status.html.erb +2 -0
- data/app/views/http/status.ics.erb +17 -0
- data/app/views/http/status.js.erb +1 -0
- data/app/views/http/status.json.erb +1 -0
- data/app/views/http/status.rss.erb +7 -0
- data/app/views/http/status.svg.erb +5 -0
- data/app/views/http/status.txt.erb +1 -0
- data/app/views/http/status.xml.erb +1 -0
- data/app/views/http/status.yaml.erb +1 -0
- data/config/locales/en.yml +212 -0
- data/config/locales/fr.yml +212 -0
- data/lib/httpstatus/core_ext.rb +2 -0
- data/lib/httpstatus/core_ext/action_controller.rb +82 -0
- data/lib/httpstatus/core_ext/mapper.rb +5 -0
- data/lib/httpstatus/engine.rb +4 -0
- data/lib/httpstatus/rails.rb +9 -0
- data/lib/httpstatus/rails/railtie.rb +6 -0
- data/lib/httpstatus/rails/version.rb +5 -0
- data/lib/httpstatus/status.rb +74 -0
- data/lib/tasks/httpstatus/rails_tasks.rake +4 -0
- metadata +98 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 651e156cb21cf97aa967d53d2b8837afed7bd33b699111b7f1df2046b02b13db
|
4
|
+
data.tar.gz: fe69ab42c90b162ecd30f331faca64820df043fdbc3786e94664b386c9bc7ec7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3c88c51b1fa2d2e24c8c7da40f209bce89797158b2ee0b5bceb8d092fdb5a1087dba683761fee6f19f07ce5821a6d19c87fe2cfc2fdbea7ec13e385d43c3e802
|
7
|
+
data.tar.gz: b8306f03fe7b5d9474e1ec3bc6d0e83c014278bfcbfa2e32e55e0ddab717124a06aeceb91be4379a4460870a83f0043e8b70c02590b6369326e9d71a78939f0c
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2018 Julien Dargelos
|
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.md
ADDED
@@ -0,0 +1,195 @@
|
|
1
|
+
# Http Status Rails
|
2
|
+
This gem makes http status rendering cool. It provides a minimal rendering content for every status and format included in Ruby On Rails.
|
3
|
+
|
4
|
+
## Usage
|
5
|
+
|
6
|
+
### Basic
|
7
|
+
|
8
|
+
To render a status in a controller, you can just call the `render_status` method:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
def show
|
12
|
+
@user = User.find_by id: params[:id]
|
13
|
+
render_status :not_found if @user.nil?
|
14
|
+
end
|
15
|
+
```
|
16
|
+
|
17
|
+
Depending on the request format, it will respond with a basic message:
|
18
|
+
|
19
|
+
#### HTML
|
20
|
+
|
21
|
+
```html
|
22
|
+
<!-- Wrapped into layouts/application by default -->
|
23
|
+
<h1>404 Not Found</h1>
|
24
|
+
<p>The page you are looking for does not exist.</p>
|
25
|
+
```
|
26
|
+
|
27
|
+
#### JSON
|
28
|
+
|
29
|
+
```json
|
30
|
+
{
|
31
|
+
"status": 404,
|
32
|
+
"title": "Not Found",
|
33
|
+
"message": "The page you are looking for does not exist."
|
34
|
+
}
|
35
|
+
```
|
36
|
+
|
37
|
+
#### Formats supported by default
|
38
|
+
- text
|
39
|
+
- json
|
40
|
+
- yaml
|
41
|
+
- xml
|
42
|
+
- svg
|
43
|
+
- js
|
44
|
+
- html
|
45
|
+
- rss
|
46
|
+
- atom
|
47
|
+
- ics
|
48
|
+
- csv
|
49
|
+
|
50
|
+
If the request format isn't in this list and you haven't created any template to handle it, the server will respond with a **html** format (The *Restrict render format* section explain how to customize this behavior).
|
51
|
+
|
52
|
+
You can also explicitly set the rendering format or the layout:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
render_status :bad_request, format: :js, layout: 'layouts/status'
|
56
|
+
# Set layout to false if you doesn't want any layout
|
57
|
+
# If the layout isn't found for the given format, it's assumed to be false
|
58
|
+
```
|
59
|
+
|
60
|
+
The response could be:
|
61
|
+
|
62
|
+
```javascript
|
63
|
+
// Asuming that layouts/status.js.erb wraps JavaScript into a jQuery callback
|
64
|
+
$(function() {
|
65
|
+
window.http = { "code": 400, "title": "Bad Request", "message": "The server cannot or will not process the request due to something that is perceived to be a client error." }
|
66
|
+
});
|
67
|
+
```
|
68
|
+
|
69
|
+
### Templating
|
70
|
+
|
71
|
+
You can override these responses or create new ones by adding `status` named templates in the `app/views/http` folder. (eg: `app/views/http/status.html.erb`, `app/views/http/status.csv.erb`)
|
72
|
+
|
73
|
+
You'll have access to a `@status` variable which contains the following methods:
|
74
|
+
- `key`: The symbol you provided to the `render_status` method. (If this symbol isn't registered by Rails within valid http status, it'll be replaced by `:internal_error`)
|
75
|
+
- `code`: The code corresponding to the status. (eg: for `:not_found` it'll be `404`)
|
76
|
+
- `title`: The title of the status (eg: for `:not_found` it'll be «*Not Found*»)
|
77
|
+
- `message`: The message of the status (eg: for `:not_found` it'll be «*The page you are looking for does not exist.*»)
|
78
|
+
- `error?`: Indicates if the status is considered as an error (4xx and 5xx codes).
|
79
|
+
|
80
|
+
### Layout
|
81
|
+
|
82
|
+
You can configure the layout you want to use in your controller with the `status_layout` method:
|
83
|
+
|
84
|
+
```
|
85
|
+
class ApplicationController < ActionController::Base
|
86
|
+
status_layout 'status' # By default: 'application'
|
87
|
+
# If the layout isn't found for the current format, it's assumed to be false
|
88
|
+
end
|
89
|
+
```
|
90
|
+
|
91
|
+
### Localizing
|
92
|
+
|
93
|
+
This gem uses `I18n` to provide localized contents. You can override or translate these contents by adding new yaml keys under the `config/locales` folder:
|
94
|
+
|
95
|
+
```yaml
|
96
|
+
fr:
|
97
|
+
http_status:
|
98
|
+
not_found:
|
99
|
+
code: 404
|
100
|
+
title: Page introuvable
|
101
|
+
message: La page que vous recherchez n'existe pas
|
102
|
+
```
|
103
|
+
|
104
|
+
Every key which lives directly under `http_status` exactly matches the corresponding status symbol provided by Rails.
|
105
|
+
|
106
|
+
### Helpers
|
107
|
+
|
108
|
+
#### Handle *Not Found*
|
109
|
+
|
110
|
+
You can easily handle *Not Found* status by calling the `handles_not_found_status` method in a controller **and** in the `config/routes.rb` file:
|
111
|
+
|
112
|
+
```ruby
|
113
|
+
# app/controllers/application_controller.rb
|
114
|
+
class ApplicationController < ActionController::Base
|
115
|
+
protect_from_forgery with: :exception
|
116
|
+
handles_not_found_status
|
117
|
+
end
|
118
|
+
```
|
119
|
+
|
120
|
+
```ruby
|
121
|
+
# config/routes.rb
|
122
|
+
Rails.application.routes.draw do
|
123
|
+
handles_not_found_status
|
124
|
+
end
|
125
|
+
```
|
126
|
+
|
127
|
+
This method will respond with a *Not Found* page (in html, json, ...) when a record isn't found (eg: `User.find params[:id]`), or when there is any routing problem (controller not found, action not found).
|
128
|
+
|
129
|
+
#### Handle generic errors (as error 500)
|
130
|
+
|
131
|
+
You can easily handle generic exceptions within your controller by calling the `handles_error_status` method.
|
132
|
+
|
133
|
+
```ruby
|
134
|
+
# app/controllers/application_controller.rb
|
135
|
+
class ApplicationController < ActionController::Base
|
136
|
+
protect_from_forgery with: :exception
|
137
|
+
handles_error_status
|
138
|
+
end
|
139
|
+
```
|
140
|
+
|
141
|
+
This method will respond with a *Internal Server Error* page (in html, json, ...) when any error occurs while handling the request.
|
142
|
+
|
143
|
+
#### Restrict render formats
|
144
|
+
|
145
|
+
Http status will all be rendered in json, whatever the request format is:
|
146
|
+
```ruby
|
147
|
+
# app/controllers/application_controller.rb
|
148
|
+
class ApplicationController < ActionController::Base
|
149
|
+
status_formats only: :json
|
150
|
+
end
|
151
|
+
```
|
152
|
+
|
153
|
+
Http status will all be rendered in json or html, but will fallback on html because it's the first one:
|
154
|
+
```ruby
|
155
|
+
# app/controllers/application_controller.rb
|
156
|
+
class ApplicationController < ActionController::Base
|
157
|
+
status_formats only: [:html, :json]
|
158
|
+
end
|
159
|
+
```
|
160
|
+
|
161
|
+
Http status will be rendered in html for csv, yaml and rss formats:
|
162
|
+
```ruby
|
163
|
+
# app/controllers/application_controller.rb
|
164
|
+
class ApplicationController < ActionController::Base
|
165
|
+
status_formats except: [:csv, :yaml, :rss]
|
166
|
+
end
|
167
|
+
```
|
168
|
+
|
169
|
+
Http status will be rendered in json for csv, yaml and rss formats, or when the format is unknown, or when json is the original request format. The status will be rendered in html when it's the original request format:
|
170
|
+
```ruby
|
171
|
+
# app/controllers/application_controller.rb
|
172
|
+
class ApplicationController < ActionController::Base
|
173
|
+
status_formats except: [:csv, :yaml, :rss], only: [:json, :html]
|
174
|
+
end
|
175
|
+
```
|
176
|
+
|
177
|
+
## Installation
|
178
|
+
Add this line to your application's Gemfile:
|
179
|
+
|
180
|
+
```ruby
|
181
|
+
gem 'httpstatus-rails'
|
182
|
+
```
|
183
|
+
|
184
|
+
And then execute:
|
185
|
+
```bash
|
186
|
+
$ bundle
|
187
|
+
```
|
188
|
+
|
189
|
+
Or install it yourself as:
|
190
|
+
```bash
|
191
|
+
$ gem install httpstatus-rails
|
192
|
+
```
|
193
|
+
|
194
|
+
## License
|
195
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'Httpstatus::Rails'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
require 'bundler/gem_tasks'
|
18
|
+
|
19
|
+
require 'rake/testtask'
|
20
|
+
|
21
|
+
Rake::TestTask.new(:test) do |t|
|
22
|
+
t.libs << 'test'
|
23
|
+
t.pattern = 'test/**/*_test.rb'
|
24
|
+
t.verbose = false
|
25
|
+
end
|
26
|
+
|
27
|
+
task default: :test
|
@@ -0,0 +1 @@
|
|
1
|
+
<%= @status %>
|
@@ -0,0 +1,17 @@
|
|
1
|
+
BEGIN:VCALENDAR
|
2
|
+
CALSCALE:GREGORIAN
|
3
|
+
VERSION:2.0
|
4
|
+
X-WR-CALNAME:<%= @status.code %> <%= @status.title %>
|
5
|
+
METHOD:PUBLISH
|
6
|
+
PRODID:-//Apple Inc.//Mac OS X 10.12.5//EN
|
7
|
+
BEGIN:VEVENT
|
8
|
+
CREATED:19971210T000000Z
|
9
|
+
UID:56A7A602-B272-48BB-B9CB-BB5FC227C721
|
10
|
+
DTEND;VALUE=DATE:19971210
|
11
|
+
SUMMARY:<%= @status.code %> <%= @status.title %>
|
12
|
+
DTSTART;VALUE=DATE:19971210
|
13
|
+
DTSTAMP:19971210T000000Z
|
14
|
+
SEQUENCE:0
|
15
|
+
DESCRIPTION:<%= @status.message %>
|
16
|
+
END:VEVENT
|
17
|
+
END:VCALENDAR
|
@@ -0,0 +1 @@
|
|
1
|
+
window.http = <%= @status.to_json %>;
|
@@ -0,0 +1 @@
|
|
1
|
+
<%= @status.to_json %>
|
@@ -0,0 +1 @@
|
|
1
|
+
<%= @status %>
|
@@ -0,0 +1 @@
|
|
1
|
+
<%= @status.to_xml %>
|
@@ -0,0 +1 @@
|
|
1
|
+
<%= @status.to_yaml %>
|
@@ -0,0 +1,212 @@
|
|
1
|
+
en:
|
2
|
+
http_status:
|
3
|
+
|
4
|
+
# 1xx Informational
|
5
|
+
continue:
|
6
|
+
code: 100
|
7
|
+
title: Continue
|
8
|
+
message: The initial part of a request has been received and has not yet been rejected by the server. The server intends to send a final response after the request has been fully received and acted upon.
|
9
|
+
switching_protocols:
|
10
|
+
code: 101
|
11
|
+
title: Switching Protocols
|
12
|
+
message: The server understands and is willing to comply with the client's request, via the Upgrade header field, for a change in the application protocol being used on this connection.
|
13
|
+
processing:
|
14
|
+
code: 102
|
15
|
+
title: Processing
|
16
|
+
message: An interim response used to inform the client that the server has accepted the complete request, but has not yet completed it.
|
17
|
+
|
18
|
+
# 2xx Success
|
19
|
+
ok:
|
20
|
+
code: 200
|
21
|
+
title: OK
|
22
|
+
message: The request has succeeded.
|
23
|
+
created:
|
24
|
+
code: 201
|
25
|
+
title: Created
|
26
|
+
message: The request has been fulfilled and has resulted in one or more new resources being created.
|
27
|
+
accepted:
|
28
|
+
code: 202
|
29
|
+
title: Accepted
|
30
|
+
message: The request has been accepted for processing, but the processing has not been completed. The request might or might not eventually be acted upon, as it might be disallowed when processing actually takes place.
|
31
|
+
non_authoritative_information:
|
32
|
+
code: 203
|
33
|
+
title: Non-authoritative Information
|
34
|
+
message: The request was successful but the enclosed payload has been modified from that of the origin server's 200 OK response by a transforming proxy.
|
35
|
+
no_content:
|
36
|
+
code: 204
|
37
|
+
title: No Content
|
38
|
+
message: The server has successfully fulfilled the request and that there is no additional content to send in the response payload body.
|
39
|
+
reset_content:
|
40
|
+
code: 205
|
41
|
+
title: Reset Content
|
42
|
+
message: The server has fulfilled the request and desires that the user agent reset the "document view", which caused the request to be sent, to its original state as received from the origin server.
|
43
|
+
partial_content:
|
44
|
+
code: 206
|
45
|
+
title: Partial Content
|
46
|
+
message: The server is successfully fulfilling a range request for the target resource by transferring one or more parts of the selected representation that correspond to the satisfiable ranges found in the request's Range header field.
|
47
|
+
multi_status:
|
48
|
+
code: 207
|
49
|
+
title: Multi-Status
|
50
|
+
message: A Multi-Status response conveys information about multiple resources in situations where multiple status codes might be appropriate.
|
51
|
+
im_used:
|
52
|
+
code: 226
|
53
|
+
title: IM Used
|
54
|
+
message: The server has fulfilled a GET request for the resource, and the response is a representation of the result of one or more instance-manipulations applied to the current instance.
|
55
|
+
|
56
|
+
# 3xx Redirection
|
57
|
+
multiple_choices:
|
58
|
+
code: 300
|
59
|
+
title: Multiple Choices
|
60
|
+
message: The target resource has more than one representation, each with its own more specific identifier, and information about the alternatives is being provided so that the user (or user agent) can select a preferred representation by redirecting its request to one or more of those identifiers.
|
61
|
+
moved_permanently:
|
62
|
+
code: 301
|
63
|
+
title: Moved Permanently
|
64
|
+
message: The target resource has been assigned a new permanent URI and any future references to this resource ought to use one of the enclosed URIs.
|
65
|
+
found:
|
66
|
+
code: 302
|
67
|
+
title: Found
|
68
|
+
message: The target resource resides temporarily under a different URI. Since the redirection might be altered on occasion, the client ought to continue to use the effective request URI for future requests.
|
69
|
+
see_other:
|
70
|
+
code: 303
|
71
|
+
title: See Other
|
72
|
+
message: The server is redirecting the user agent to a different resource, as indicated by a URI in the Location header field, which is intended to provide an indirect response to the original request.
|
73
|
+
not_modified:
|
74
|
+
code: 304
|
75
|
+
title: Not Modified
|
76
|
+
message: A conditional GET or HEAD request has been received and would have resulted in a 200 OK response if it were not for the fact that the condition evaluated to false.
|
77
|
+
use_proxy:
|
78
|
+
code: 305
|
79
|
+
title: Use Proxy
|
80
|
+
message: The requested resource must be accessed through the proxy given by the Location field.
|
81
|
+
temporary_redirect:
|
82
|
+
code: 307
|
83
|
+
title: Temporary Redirect
|
84
|
+
message: The target resource resides temporarily under a different URI and the user agent MUST NOT change the request method if it performs an automatic redirection to that URI.
|
85
|
+
|
86
|
+
# 4xx Client Error
|
87
|
+
bad_request:
|
88
|
+
code: 400
|
89
|
+
title: Bad Request
|
90
|
+
message: The server cannot or will not process the request due to something that is perceived to be a client error.
|
91
|
+
unauthorized:
|
92
|
+
code: 401
|
93
|
+
title: Unauthorized
|
94
|
+
message: The request has not been applied because it lacks valid authentication credentials for the target resource.
|
95
|
+
payment_required:
|
96
|
+
code: 402
|
97
|
+
title: Payment Required
|
98
|
+
message: This code is reserved for future use.
|
99
|
+
forbidden:
|
100
|
+
code: 403
|
101
|
+
title: Forbidden
|
102
|
+
message: The server understood the request but refuses to authorize it.
|
103
|
+
not_found:
|
104
|
+
code: 404
|
105
|
+
title: Not Found
|
106
|
+
message: The page you are looking for does not exist.
|
107
|
+
method_not_allowed:
|
108
|
+
code: 405
|
109
|
+
title: Method Not Allowed
|
110
|
+
message: The method received in the request-line is known by the origin server but not supported by the target resource.
|
111
|
+
not_acceptable:
|
112
|
+
code: 406
|
113
|
+
title: Not Acceptable
|
114
|
+
message: The target resource does not have a current representation that would be acceptable to the user agent, according to the proactive negotiation header fields received in the request, and the server is unwilling to supply a default representation.
|
115
|
+
proxy_authentication_required:
|
116
|
+
code: 407
|
117
|
+
title: Proxy Authentication Required
|
118
|
+
message: The request has not been applied because it lacks valid authentication credentials for the target resource. The client needs to authenticate itself in order to use a proxy.
|
119
|
+
request_timeout:
|
120
|
+
code: 408
|
121
|
+
title: Request Timeout
|
122
|
+
message: The server did not receive a complete request message within the time that it was prepared to wait.
|
123
|
+
conflict:
|
124
|
+
code: 409
|
125
|
+
title: Conflict
|
126
|
+
message: The request could not be completed due to a conflict with the current state of the target resource.
|
127
|
+
gone:
|
128
|
+
code: 410
|
129
|
+
title: Gone
|
130
|
+
message: The target resource is no longer available at the origin server and that this condition is likely to be permanent.
|
131
|
+
length_required:
|
132
|
+
code: 411
|
133
|
+
title: Length Required
|
134
|
+
message: The server refuses to accept the request without a defined Content-Length.
|
135
|
+
precondition_failed:
|
136
|
+
code: 412
|
137
|
+
title: Precondition Failed
|
138
|
+
message: One or more conditions given in the request header fields evaluated to false when tested on the server.
|
139
|
+
payload_too_large:
|
140
|
+
code: 413
|
141
|
+
title: Payload Too Large
|
142
|
+
message: The server is refusing to process a request because the request payload is larger than the server is willing or able to process.
|
143
|
+
request_uri_too_long:
|
144
|
+
code: 414
|
145
|
+
title: Request-URI Too Long
|
146
|
+
message: The server is refusing to service the request because the request-target is longer than the server is willing to interpret.
|
147
|
+
unsupported_media_type:
|
148
|
+
code: 415
|
149
|
+
title: Unsupported Media Type
|
150
|
+
message: The origin server is refusing to service the request because the payload is in a format not supported by this method on the target resource.
|
151
|
+
requested_range_not_satisfiable:
|
152
|
+
code: 416
|
153
|
+
title: Requested Range Not Satisfiable
|
154
|
+
message: None of the ranges in the request's Range header field overlap the current extent of the selected resource or that the set of ranges requested has been rejected due to invalid ranges or an excessive request of small or overlapping ranges.
|
155
|
+
expectation_failed:
|
156
|
+
code: 417
|
157
|
+
title: Expectation Failed
|
158
|
+
message: The expectation given in the request's Expect header field could not be met by at least one of the inbound servers.
|
159
|
+
unprocessable_entity:
|
160
|
+
code: 422
|
161
|
+
title: Unprocessable Entity
|
162
|
+
message: The server understands the content type of the request entity, and the syntax of the request entity is correct but was unable to process the contained instructions.
|
163
|
+
locked:
|
164
|
+
code: 423
|
165
|
+
title: Locked
|
166
|
+
message: The source or destination resource of a method is locked.
|
167
|
+
failed_dependency:
|
168
|
+
code: 424
|
169
|
+
title: Failed Dependency
|
170
|
+
message: The method could not be performed on the resource because the requested action depended on another action and that action failed.
|
171
|
+
upgrade_required:
|
172
|
+
code: 426
|
173
|
+
title: Upgrade Required
|
174
|
+
message: The server refuses to perform the request using the current protocol but might be willing to do so after the client upgrades to a different protocol.
|
175
|
+
|
176
|
+
# 5xx Server Error
|
177
|
+
internal_server_error:
|
178
|
+
code: 500
|
179
|
+
title: Internal Server Error
|
180
|
+
message: We're sorry but something went wrong.
|
181
|
+
not_implemented:
|
182
|
+
code: 501
|
183
|
+
title: Not Implemented
|
184
|
+
message: The server does not support the functionality required to fulfill the request.
|
185
|
+
bad_gateway:
|
186
|
+
code: 502
|
187
|
+
title: Bad Gateway
|
188
|
+
message: The server, while acting as a gateway or proxy, received an invalid response from an inbound server it accessed while attempting to fulfill the request.
|
189
|
+
service_unavailable:
|
190
|
+
code: 503
|
191
|
+
title: Service Unavailable
|
192
|
+
message: The server is currently unable to handle the request due to a temporary overload or scheduled maintenance, which will likely be alleviated after some delay.
|
193
|
+
gateway_timeout:
|
194
|
+
code: 504
|
195
|
+
title: Gateway Timeout
|
196
|
+
message: The server, while acting as a gateway or proxy, did not receive a timely response from an upstream server it needed to access in order to complete the request.
|
197
|
+
http_version_not_supported:
|
198
|
+
code: 505
|
199
|
+
title: HTTP Version Not Supported
|
200
|
+
message: The server does not support, or refuses to support, the major version of HTTP that was used in the request message.
|
201
|
+
insufficient_storage:
|
202
|
+
code: 507
|
203
|
+
title: Insufficient Storage
|
204
|
+
message: The method could not be performed on the resource because the server is unable to store the representation needed to successfully complete the request.
|
205
|
+
loop_detected:
|
206
|
+
code: 508
|
207
|
+
title: Loop Detected
|
208
|
+
message: The server terminated an operation because it encountered an infinite loop while processing a request with infinite depth.
|
209
|
+
not_extended:
|
210
|
+
code: 510
|
211
|
+
title: Not Extended
|
212
|
+
message: The policy for accessing the resource has not been met in the request.
|
@@ -0,0 +1,212 @@
|
|
1
|
+
fr:
|
2
|
+
http_status:
|
3
|
+
|
4
|
+
# 1xx Informational
|
5
|
+
continue:
|
6
|
+
code: 100
|
7
|
+
title: Continuer
|
8
|
+
message: La partie initiale de la requête a bien été reçue et le client peut continuer avec la fin de sa requête.
|
9
|
+
switching_protocols:
|
10
|
+
code: 101
|
11
|
+
title: Changement de protocole
|
12
|
+
message: Le client a demandé au serveur d'utiliser un autre protocole que celui actuellement utilisé, et le serveur accepte cette requête.
|
13
|
+
processing:
|
14
|
+
code: 102
|
15
|
+
title: Traîtement
|
16
|
+
message: Traitement en cours.
|
17
|
+
|
18
|
+
# 2xx Success
|
19
|
+
ok:
|
20
|
+
code: 200
|
21
|
+
title: OK
|
22
|
+
message: Requête traitée avec succès.
|
23
|
+
created:
|
24
|
+
code: 201
|
25
|
+
title: Créé
|
26
|
+
message: La requête a été traitée avec succès et une ou plusieurs resources ont été créées.
|
27
|
+
accepted:
|
28
|
+
code: 202
|
29
|
+
title: Accepté
|
30
|
+
message: La requête a éré traitée, mais le serveur ne garantit pas un résultat.
|
31
|
+
non_authoritative_information:
|
32
|
+
code: 203
|
33
|
+
title: Information non certifiée
|
34
|
+
message: L'information retournée n'a pas été générée par le serveur HTTP mais par une autre source non authentifiée.
|
35
|
+
no_content:
|
36
|
+
code: 204
|
37
|
+
title: Pas de contenu
|
38
|
+
message: Le serveur HTTP a correctement traité la requête mais il n'y a pas d'information à envoyer en retour.
|
39
|
+
reset_content:
|
40
|
+
code: 205
|
41
|
+
title: Contenu réinitialisé
|
42
|
+
message: Le client doit remettre à zéro le formulaire utilisé dans cette transaction.
|
43
|
+
partial_content:
|
44
|
+
code: 206
|
45
|
+
title: Contenu partiel
|
46
|
+
message: Le serveur retourne une partie seulement de la taille demandée.
|
47
|
+
multi_status:
|
48
|
+
code: 207
|
49
|
+
title: Statuts multiples
|
50
|
+
message: Réponse multiple.
|
51
|
+
im_used:
|
52
|
+
code: 226
|
53
|
+
title: IM utilisé
|
54
|
+
message: Le serveur a accompli la requête pour la ressource, et la réponse est une représentation du résultat d'une ou plusieurs manipulations d'instances appliquées à l'instance actuelle.
|
55
|
+
|
56
|
+
# 3xx Redirection
|
57
|
+
multiple_choices:
|
58
|
+
code: 300
|
59
|
+
title: Choix multiples
|
60
|
+
message: L'url demandée concerne plus d'une ressource.
|
61
|
+
moved_permanently:
|
62
|
+
code: 301
|
63
|
+
title: Changement d'adresse définitif
|
64
|
+
message: La ressource demandée possède une nouvelle adresse.
|
65
|
+
found:
|
66
|
+
code: 302
|
67
|
+
title: Changement d'adresse temporaire
|
68
|
+
message: La ressource demandée réside temporairement à une adresse différente.
|
69
|
+
see_other:
|
70
|
+
code: 303
|
71
|
+
title: Voir ailleurs
|
72
|
+
message: L'url spécifiée est disponible à une autre adresse.
|
73
|
+
not_modified:
|
74
|
+
code: 304
|
75
|
+
title: Non modifié
|
76
|
+
message: Le navigateur web a effectué une requête GET conditionnelle et l'accès est autorisé, mais le document n'a pas été modifié.
|
77
|
+
use_proxy:
|
78
|
+
code: 305
|
79
|
+
title: Utiliser le proxy
|
80
|
+
message: L'adresse spécifiée ne peut être atteinte qu'à travers le proxy.
|
81
|
+
temporary_redirect:
|
82
|
+
code: 307
|
83
|
+
title: Redirection temporaire
|
84
|
+
message: La resource spécifiée réside temporairement à une autre adresse.
|
85
|
+
|
86
|
+
# 4xx Client Error
|
87
|
+
bad_request:
|
88
|
+
code: 400
|
89
|
+
title: Mauvaise requête
|
90
|
+
message: La requête HTTP n'a pas pu être comprise par le serveur en raison d'une syntaxe erronée.
|
91
|
+
unauthorized:
|
92
|
+
code: 401
|
93
|
+
title: Non autorisé
|
94
|
+
message: La requête nécessite une identification de l'utilisateur.
|
95
|
+
payment_required:
|
96
|
+
code: 402
|
97
|
+
title: Paiement exigé
|
98
|
+
message: Ce code est réservé pour un usage futur.
|
99
|
+
forbidden:
|
100
|
+
code: 403
|
101
|
+
title: Interdit
|
102
|
+
message: Le serveur HTTP a compris la requête, mais refuse de la traiter.
|
103
|
+
not_found:
|
104
|
+
code: 404
|
105
|
+
title: Page introuvable
|
106
|
+
message: La page que vous recherchez n'existe pas.
|
107
|
+
method_not_allowed:
|
108
|
+
code: 405
|
109
|
+
title: Méthode non autorisée
|
110
|
+
message: La méthode utilisée par le client n'est pas supportée pour cette url.
|
111
|
+
not_acceptable:
|
112
|
+
code: 406
|
113
|
+
title: Aucun disponible
|
114
|
+
message: L'adresse spécifiée existe, mais pas dans le format préféré du client.
|
115
|
+
proxy_authentication_required:
|
116
|
+
code: 407
|
117
|
+
title: Authentification proxy exigée
|
118
|
+
message: Le serveur proxy exige une authentification du client avant de transmettre la requête.
|
119
|
+
request_timeout:
|
120
|
+
code: 408
|
121
|
+
title: La requête a expiré
|
122
|
+
message: Le client n'a pas présenté une requête complète pendant le délai maximal qui lui était imparti, et le serveur a abandonné la connexion.
|
123
|
+
conflict:
|
124
|
+
code: 409
|
125
|
+
title: Conflit
|
126
|
+
message: La requête entre en conflit avec une autre requête ou avec la configuration du serveur.
|
127
|
+
gone:
|
128
|
+
code: 410
|
129
|
+
title: Parti
|
130
|
+
message: L'adresse demandée n'existe plus et a été définitivement supprimée du serveur.
|
131
|
+
length_required:
|
132
|
+
code: 411
|
133
|
+
title: Longueur exigée
|
134
|
+
message: Le serveur a besoin de connaître la taille de cette requête pour pouvoir y répondre.
|
135
|
+
precondition_failed:
|
136
|
+
code: 412
|
137
|
+
title: Précondition échouée
|
138
|
+
message: Les conditions spécifiées dans la requête ne sont pas remplies.
|
139
|
+
payload_too_large:
|
140
|
+
code: 413
|
141
|
+
title: Corps de requête trop grand
|
142
|
+
message: Le serveur ne peut traiter la requête car la taille de son contenu est trop importante.
|
143
|
+
request_uri_too_long:
|
144
|
+
code: 414
|
145
|
+
title: Url trop longue
|
146
|
+
message: Le serveur ne peut traiter la requête car la taille de l'url est trop importante.
|
147
|
+
unsupported_media_type:
|
148
|
+
code: 415
|
149
|
+
title: Format non supporté
|
150
|
+
message: Le serveur ne peut traiter la requête car son contenu est écrit dans un format non supporté.
|
151
|
+
requested_range_not_satisfiable:
|
152
|
+
code: 416
|
153
|
+
title: Plage demandée invalide
|
154
|
+
message: Le sous-ensemble de recherche spécifié est invalide.
|
155
|
+
expectation_failed:
|
156
|
+
code: 417
|
157
|
+
title: Comportement erroné
|
158
|
+
message: Le comportement prévu pour le serveur n'est pas supporté.
|
159
|
+
unprocessable_entity:
|
160
|
+
code: 422
|
161
|
+
title: Impossiblde traîter l'entité
|
162
|
+
message: L’entité fournie avec la requête est incompréhensible ou incomplète.
|
163
|
+
locked:
|
164
|
+
code: 423
|
165
|
+
title: Verouillé
|
166
|
+
message: L’opération ne peut avoir lieu car la ressource est verrouillée.
|
167
|
+
failed_dependency:
|
168
|
+
code: 424
|
169
|
+
title: Échec de la méthode
|
170
|
+
message: Une méthode de la transaction a échoué.
|
171
|
+
upgrade_required:
|
172
|
+
code: 426
|
173
|
+
title: Mise à niveau requise
|
174
|
+
message: Le client devrait changer de protocole.
|
175
|
+
|
176
|
+
# 5xx Server Error
|
177
|
+
internal_server_error:
|
178
|
+
code: 500
|
179
|
+
title: Erreur interne du serveur
|
180
|
+
message: Nous sommes désolé mais quelque chose n'a pas fonctionné...
|
181
|
+
not_implemented:
|
182
|
+
code: 501
|
183
|
+
title: Non implémenté
|
184
|
+
message: Fonctionnalité réclamée non supportée par le serveur.
|
185
|
+
bad_gateway:
|
186
|
+
code: 502
|
187
|
+
title: Mauvaise passerelle
|
188
|
+
message: La passerelle a fourni une réponse invalide.
|
189
|
+
service_unavailable:
|
190
|
+
code: 503
|
191
|
+
title: Service indisponible
|
192
|
+
message: Le serveur HTTP est actuellement incapable de traiter la requête en raison d'une surcharge temporaire ou d'une opération de maintenance.
|
193
|
+
gateway_timeout:
|
194
|
+
code: 504
|
195
|
+
title: Passerelle hors-délai
|
196
|
+
message: La passerrelle n'a pas présenté une réponse complète pendant le délai maximal qui lui était imparti, et le serveur a abandonné la connexion.
|
197
|
+
http_version_not_supported:
|
198
|
+
code: 505
|
199
|
+
title: Version de HTTP non supportée
|
200
|
+
message: La version du protocole HTTP utilisée dans cette requête n'est pas supportée par le serveur.
|
201
|
+
insufficient_storage:
|
202
|
+
code: 507
|
203
|
+
title: Espace insuffisant
|
204
|
+
message: Espace insuffisant pour modifier les propriétés ou construire la collection.
|
205
|
+
loop_detected:
|
206
|
+
code: 508
|
207
|
+
title: Boucle détectée
|
208
|
+
message: Boucle dans une mise en relation de ressources.
|
209
|
+
not_extended:
|
210
|
+
code: 510
|
211
|
+
title: Non étendu
|
212
|
+
message: La requête ne respecte pas la politique d'accès aux ressources HTTP étendues.
|
@@ -0,0 +1,82 @@
|
|
1
|
+
ActionController::Base.class_eval do
|
2
|
+
DEFAULT_STATUS_LAYOUT = 'application'
|
3
|
+
|
4
|
+
class << self
|
5
|
+
def status_format?(format)
|
6
|
+
(status_formats[:only].include?(format) || status_formats[:only].empty?) && !status_formats[:except].include?(format)
|
7
|
+
end
|
8
|
+
|
9
|
+
def status_formats(only: nil, except: nil)
|
10
|
+
@status_formats = { only: [], except: [] } unless @status_formats.is_a? Hash
|
11
|
+
|
12
|
+
only = only.to_sym if only.is_a? String
|
13
|
+
only = [only] if only.is_a? Symbol
|
14
|
+
|
15
|
+
except = only.to_sym if only.is_a? String
|
16
|
+
except = [only] if only.is_a? Symbol
|
17
|
+
|
18
|
+
if only.is_a? Array
|
19
|
+
@status_formats[:only] = (@status_formats[:only] + only).uniq
|
20
|
+
end
|
21
|
+
|
22
|
+
if except.is_a? Array
|
23
|
+
@status_formats[:except] = (@status_formats[:except] + except).uniq
|
24
|
+
@status_formats[:only] -= except
|
25
|
+
end
|
26
|
+
|
27
|
+
@status_formats
|
28
|
+
end
|
29
|
+
alias_method :status_format, :status_formats
|
30
|
+
|
31
|
+
def status_layout(*name)
|
32
|
+
@status_layout = name.first if name.any?
|
33
|
+
@status_layout == false ? nil : (@status_layout || DEFAULT_STATUS_LAYOUT)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
protected
|
38
|
+
|
39
|
+
def status_format?(format)
|
40
|
+
lookup_context.exists?('http/status', [], false, [], formats: [format]) && self.class.status_format?(format)
|
41
|
+
end
|
42
|
+
|
43
|
+
def render_status(status, format: nil, layout: nil)
|
44
|
+
format = (format.present? ? format : request.format).to_sym
|
45
|
+
format = self.class.status_formats[:only].first || :html unless status_format? format
|
46
|
+
layout = layout.nil? ? self.class.status_layout : layout
|
47
|
+
layout = false unless lookup_context.exists? layout, [], false, formats: [format]
|
48
|
+
layout = "layouts/#{layout}" unless layout == false
|
49
|
+
|
50
|
+
@status = Httpstatus::Status.new status
|
51
|
+
render 'http/status', formats: [format], layout: layout, status: @status.key
|
52
|
+
end
|
53
|
+
|
54
|
+
class << self
|
55
|
+
def status_action(*status)
|
56
|
+
status.each do |_status|
|
57
|
+
unless instance_methods.include? _status
|
58
|
+
define_method _status do |*_, format: nil, layout: nil|
|
59
|
+
render_status _status, format: format, layout: layout
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def handles_error_status
|
66
|
+
status_action :internal_server_error
|
67
|
+
skip_before_action :verify_authenticity_token, only: :internal_server_error, if: -> { request.format.js? }
|
68
|
+
rescue_from Exception, with: :internal_server_error
|
69
|
+
end
|
70
|
+
|
71
|
+
def handles_not_found_status
|
72
|
+
status_action :not_found
|
73
|
+
skip_before_action :verify_authenticity_token, only: :not_found, if: -> { request.format.js? }
|
74
|
+
rescue_from *[
|
75
|
+
ActiveRecord::RecordNotFound,
|
76
|
+
ActionController::RoutingError,
|
77
|
+
ActionController::UnknownFormat,
|
78
|
+
::AbstractController::ActionNotFound
|
79
|
+
], with: :not_found
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
module Httpstatus
|
2
|
+
class Status
|
3
|
+
DEFAULT = :internal_server_error
|
4
|
+
|
5
|
+
def initialize key = nil
|
6
|
+
self.key = key
|
7
|
+
end
|
8
|
+
|
9
|
+
def key
|
10
|
+
@key || DEFAULT
|
11
|
+
end
|
12
|
+
|
13
|
+
def key= v
|
14
|
+
@key = v.is_a?(Symbol) ? v : (v.is_a?(String) ? v.to_sym : nil)
|
15
|
+
default! unless valid?
|
16
|
+
end
|
17
|
+
|
18
|
+
def code
|
19
|
+
i18n :code
|
20
|
+
end
|
21
|
+
|
22
|
+
def title
|
23
|
+
i18n :title
|
24
|
+
end
|
25
|
+
|
26
|
+
def message
|
27
|
+
i18n :message
|
28
|
+
end
|
29
|
+
|
30
|
+
def error?
|
31
|
+
(400..599) === code
|
32
|
+
end
|
33
|
+
|
34
|
+
def default!
|
35
|
+
@key = nil
|
36
|
+
end
|
37
|
+
|
38
|
+
def default?
|
39
|
+
status.key == DEFAULT
|
40
|
+
end
|
41
|
+
|
42
|
+
def to_hash *options
|
43
|
+
{ status: code, title: title, message: message }
|
44
|
+
end
|
45
|
+
alias_method :to_h, :to_hash
|
46
|
+
alias_method :as_json, :to_hash
|
47
|
+
|
48
|
+
def to_s
|
49
|
+
"#{code} #{title}\n#{message}"
|
50
|
+
end
|
51
|
+
|
52
|
+
def to_json(*options)
|
53
|
+
to_h.to_json(*options).html_safe
|
54
|
+
end
|
55
|
+
|
56
|
+
def to_yaml(*options)
|
57
|
+
to_h.to_yaml(*options).html_safe
|
58
|
+
end
|
59
|
+
|
60
|
+
def to_xml(*options)
|
61
|
+
to_h.to_xml(*options).html_safe
|
62
|
+
end
|
63
|
+
|
64
|
+
protected
|
65
|
+
|
66
|
+
def valid?
|
67
|
+
code.is_a?(Fixnum) && title.is_a?(String) && message.is_a?(String)
|
68
|
+
end
|
69
|
+
|
70
|
+
def i18n(property)
|
71
|
+
I18n.t "http_status.#{key}.#{property}"
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: httpstatus-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Julien Dargelos
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-04-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 5.2.0.rc2
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 5.2.0.rc2
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sqlite3
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: This gem provides a minimal and customizable rendering content for every
|
42
|
+
status and format included in Rails.
|
43
|
+
email:
|
44
|
+
- contact@juliendargelos.com
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- MIT-LICENSE
|
50
|
+
- README.md
|
51
|
+
- Rakefile
|
52
|
+
- app/views/http/status.atom.erb
|
53
|
+
- app/views/http/status.csv.erb
|
54
|
+
- app/views/http/status.html.erb
|
55
|
+
- app/views/http/status.ics.erb
|
56
|
+
- app/views/http/status.js.erb
|
57
|
+
- app/views/http/status.json.erb
|
58
|
+
- app/views/http/status.rss.erb
|
59
|
+
- app/views/http/status.svg.erb
|
60
|
+
- app/views/http/status.txt.erb
|
61
|
+
- app/views/http/status.xml.erb
|
62
|
+
- app/views/http/status.yaml.erb
|
63
|
+
- config/locales/en.yml
|
64
|
+
- config/locales/fr.yml
|
65
|
+
- lib/httpstatus/core_ext.rb
|
66
|
+
- lib/httpstatus/core_ext/action_controller.rb
|
67
|
+
- lib/httpstatus/core_ext/mapper.rb
|
68
|
+
- lib/httpstatus/engine.rb
|
69
|
+
- lib/httpstatus/rails.rb
|
70
|
+
- lib/httpstatus/rails/railtie.rb
|
71
|
+
- lib/httpstatus/rails/version.rb
|
72
|
+
- lib/httpstatus/status.rb
|
73
|
+
- lib/tasks/httpstatus/rails_tasks.rake
|
74
|
+
homepage: https://www.github.com/juliendargelos/httpstatus
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 2.7.3
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: Makes http status rendering cool.
|
98
|
+
test_files: []
|