Skip to content

Commit

Permalink
Added .rack3? method
Browse files Browse the repository at this point in the history
  • Loading branch information
schinery committed Oct 24, 2023
1 parent 2d87076 commit 0a88e94
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 28 deletions.
4 changes: 4 additions & 0 deletions lib/grape.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ def self.deprecator
@deprecator ||= ActiveSupport::Deprecation.new('2.0', 'Grape')
end

def self.rack3?
Gem::Version.new(::Rack.release) >= Gem::Version.new('3')
end

eager_autoload do
autoload :API
autoload :Endpoint
Expand Down
18 changes: 9 additions & 9 deletions lib/grape/http/headers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ module Headers
REQUEST_METHOD = 'REQUEST_METHOD'
QUERY_STRING = 'QUERY_STRING'

if Gem::Version.new(Rack.release) < Gem::Version.new('3')
ALLOW = 'Allow'
CACHE_CONTROL = 'Cache-Control'
CONTENT_LENGTH = 'Content-Length'
CONTENT_TYPE = 'Content-Type'
LOCATION = 'Location'
TRANSFER_ENCODING = 'Transfer-Encoding'
X_CASCADE = 'X-Cascade'
else
if Grape.rack3?
ALLOW = 'allow'
CACHE_CONTROL = 'cache-control'
CONTENT_LENGTH = 'content-length'
CONTENT_TYPE = 'content-type'
LOCATION = 'location'
TRANSFER_ENCODING = 'transfer-encoding'
X_CASCADE = 'x-cascade'
else
ALLOW = 'Allow'
CACHE_CONTROL = 'Cache-Control'
CONTENT_LENGTH = 'Content-Length'
CONTENT_TYPE = 'Content-Type'
LOCATION = 'Location'
TRANSFER_ENCODING = 'Transfer-Encoding'
X_CASCADE = 'X-Cascade'
end

GET = 'GET'
Expand Down
10 changes: 6 additions & 4 deletions lib/grape/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ def build_headers
end
end

def transform_header(header)
if Gem::Version.new(Rack.release) < Gem::Version.new('3')
-header[5..].split('_').map(&:capitalize).join('-')
else
if Grape.rack3?
def transform_header(header)
-header[5..].tr('_', '-').downcase
end
else
def transform_header(header)
-header[5..].split('_').map(&:capitalize).join('-')
end
end
end
end
31 changes: 16 additions & 15 deletions spec/support/headers_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,7 @@ module Spec
module Support
module Helpers
def rack_versioned_headers
if Gem::Version.new(Rack.release) < Gem::Version.new('3')
{
cache_control: 'Cache-Control',
content_length: 'Content-Length',
content_type: 'Content-Type',
grape_likes_symbolic: 'Grape-Likes-Symbolic',
location: 'Location',
symbol_header: 'Symbol-Header',
transfer_encoding: 'Transfer-Encoding',
x_access_token: 'X-Access-Token',
x_cascade: 'X-Cascade',
x_grape_client: 'X-Grape-Client',
x_grape_is_cool: 'X-Grape-Is-Cool'
}
else
if Grape.rack3?
{
cache_control: 'cache-control',
content_length: 'content-length',
Expand All @@ -32,6 +18,21 @@ def rack_versioned_headers
x_grape_client: 'x-grape-client',
x_grape_is_cool: 'x-grape-is-cool'
}
else
{
cache_control: 'Cache-Control',
content_length: 'Content-Length',
content_type: 'Content-Type',
grape_likes_symbolic: 'Grape-Likes-Symbolic',
location: 'Location',
symbol_header: 'Symbol-Header',
transfer_encoding: 'Transfer-Encoding',
x_access_token: 'X-Access-Token',
x_cascade: 'X-Cascade',
x_grape_client: 'X-Grape-Client',
x_grape_is_cool: 'X-Grape-Is-Cool'
}

end
end
end
Expand Down

0 comments on commit 0a88e94

Please sign in to comment.