Skip to content

Commit 1a75b28

Browse files
authored
Remove Grape::Http::Headers (#2554)
* Move HTTP_HEADERS in Grape::Request and renew known headers Use plain HTTP_ or real header name instead of referencing to Http::Headers:: Remove Grape::Util::Lazy::Object * Add few headers * Add other headers * Add other known headers and sort * CHANGELOG.md entry * Fix CHANGELOG.md Add UPGRADING notes Add X-Access-Token in list of headers
1 parent c42b66f commit 1a75b28

28 files changed

+307
-261
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* [#2540](https://github.com/ruby-grape/grape/pull/2540): Introduce Params builder with symbolized short name - [@ericproulx](https://github.com/ericproulx).
1010
* [#2550](https://github.com/ruby-grape/grape/pull/2550): Drop ActiveSupport 6.0 - [@ericproulx](https://github.com/ericproulx).
1111
* [#2549](https://github.com/ruby-grape/grape/pull/2549): Delegate cookies management to `Grape::Request` - [@ericproulx](https://github.com/ericproulx).
12+
* [#2554](https://github.com/ruby-grape/grape/pull/2554): Remove `Grape::Http::Headers` and `Grape::Util::Lazy::Object` - [@ericproulx](https://github.com/ericproulx).
1213
* Your contribution here.
1314

1415
#### Fixes

UPGRADING.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
Upgrading Grape
22
===============
33

4+
### Upgrading to >= 2.4.0
5+
6+
#### Grape::Http::Headers, Grape::Util::Lazy::Object
7+
8+
Both have been removed. See [2554](https://github.com/ruby-grape/grape/pull/2554).
9+
Here are the notable changes:
10+
11+
- Constants like `HTTP_ACCEPT` have been replaced by their literal value.
12+
- `SUPPORTED_METHODS` has been moved to `Grape` module.
13+
- `HTTP_HEADERS` has been moved to `Grape::Request` and renamed `KNOWN_HEADERS`. The last has been refreshed with new headers, and it's not lazy anymore.
14+
- `SUPPORTED_METHODS_WITHOUT_OPTIONS` and `find_supported_method` have been removed.
15+
416
### Grape::Middleware::Base
517

618
- Constant `TEXT_HTML` has been removed in favor of using literal string 'text/html'.
@@ -30,8 +42,6 @@ For the non provided case, 1.0 was automatically assigned and in a case of multi
3042
Excluding the [header versioning strategy](https://github.com/ruby-grape/grape?tab=readme-ov-file#header), whenever a media type with the [vendor tree](https://datatracker.ietf.org/doc/html/rfc6838#section-3.2) leading facet `vnd.` like `application/vnd.api+json` was provided, Grape would also consider its closest generic when negotiating. In that case, `application/json` was added to the negotiation. Now, it will just consider the provided media types without considering any closest generics, and you'll need to [register](https://github.com/ruby-grape/grape?tab=readme-ov-file#api-formats) it.
3143
You can find the official vendor tree registrations on [IANA](https://www.iana.org/assignments/media-types/media-types.xhtml)
3244

33-
### Upgrading to >= 2.4.0
34-
3545
#### Custom Validators
3646

3747
If you now receive an error of `'Grape::Validations.require_validator': unknown validator: your_custom_validation (Grape::Exceptions::UnknownValidator)` after upgrading to 2.4.0 then you will need to ensure that you require the `your_custom_validation` file before your Grape API code is loaded.

lib/grape.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@
5959
module Grape
6060
include ActiveSupport::Configurable
6161

62+
HTTP_SUPPORTED_METHODS = [
63+
Rack::GET,
64+
Rack::POST,
65+
Rack::PUT,
66+
Rack::PATCH,
67+
Rack::DELETE,
68+
Rack::HEAD,
69+
Rack::OPTIONS
70+
].freeze
71+
6272
def self.deprecator
6373
@deprecator ||= ActiveSupport::Deprecation.new('2.0', 'Grape')
6474
end

lib/grape/api/instance.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def call(env)
164164
status, headers, response = @router.call(env)
165165
unless cascade?
166166
headers = Grape::Util::Header.new.merge(headers)
167-
headers.delete(Grape::Http::Headers::X_CASCADE)
167+
headers.delete('X-Cascade')
168168
end
169169

170170
[status, headers, response]

lib/grape/dsl/headers.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module Headers
1010
# 4. Delete a specifc header key-value pair
1111
def header(key = nil, val = nil)
1212
if key
13-
val ? header[key.to_s] = val : header.delete(key.to_s)
13+
val ? header[key] = val : header.delete(key)
1414
else
1515
@header ||= Grape::Util::Header.new
1616
end

lib/grape/dsl/inside_route.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def redirect(url, permanent: false, body: nil)
213213
status 302
214214
body_message ||= "This resource has been moved temporarily to #{url}."
215215
end
216-
header Grape::Http::Headers::LOCATION, url
216+
header 'Location', url
217217
content_type 'text/plain'
218218
body body_message
219219
end
@@ -330,7 +330,7 @@ def stream(value = nil)
330330
return if value.nil? && @stream.nil?
331331

332332
header Rack::CONTENT_LENGTH, nil
333-
header Grape::Http::Headers::TRANSFER_ENCODING, nil
333+
header 'Transfer-Encoding', nil
334334
header Rack::CACHE_CONTROL, 'no-cache' # Skips ETag generation (reading the response up front)
335335
if value.is_a?(String)
336336
file_body = Grape::ServeStream::FileBody.new(value)
@@ -439,7 +439,7 @@ def entity_representation_for(entity_class, object, options)
439439
end
440440

441441
def http_version
442-
env.fetch(Grape::Http::Headers::HTTP_VERSION) { env[Rack::SERVER_PROTOCOL] }
442+
env.fetch('HTTP_VERSION') { env[Rack::SERVER_PROTOCOL] }
443443
end
444444

445445
def api_format(format)

lib/grape/dsl/routing.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def route(methods, paths = ['/'], route_options = {}, &block)
158158
reset_validations!
159159
end
160160

161-
Grape::Http::Headers::SUPPORTED_METHODS.each do |supported_method|
161+
Grape::HTTP_SUPPORTED_METHODS.each do |supported_method|
162162
define_method supported_method.downcase do |*args, &block|
163163
options = args.extract_options!
164164
paths = args.first || ['/']

lib/grape/endpoint.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ def run
257257
header['Allow'] = env[Grape::Env::GRAPE_ALLOWED_METHODS].join(', ')
258258
raise Grape::Exceptions::MethodNotAllowed.new(header) unless options?
259259

260-
header Grape::Http::Headers::ALLOW, header['Allow']
260+
header 'Allow', header['Allow']
261261
response_object = ''
262262
status 204
263263
else

lib/grape/http/headers.rb

Lines changed: 0 additions & 56 deletions
This file was deleted.

lib/grape/middleware/formatter.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def read_body_input?
119119
(rack_request.post? || rack_request.put? || rack_request.patch? || rack_request.delete?) &&
120120
!(rack_request.form_data? && rack_request.content_type) &&
121121
!rack_request.parseable_data? &&
122-
(rack_request.content_length.to_i.positive? || rack_request.env[Grape::Http::Headers::HTTP_TRANSFER_ENCODING] == 'chunked')
122+
(rack_request.content_length.to_i.positive? || rack_request.env['HTTP_TRANSFER_ENCODING'] == 'chunked')
123123
end
124124

125125
def negotiate_content_type
@@ -141,7 +141,7 @@ def format_from_extension
141141
end
142142

143143
def format_from_header
144-
accept_header = env[Grape::Http::Headers::HTTP_ACCEPT].try(:scrub)
144+
accept_header = env['HTTP_ACCEPT'].try(:scrub)
145145
return if accept_header.blank?
146146

147147
media_type = Rack::Utils.best_q_match(accept_header, mime_types.keys)

0 commit comments

Comments
 (0)