Skip to content

Commit

Permalink
Simplify RodaResponseHeaders constant setting code
Browse files Browse the repository at this point in the history
This is only called once at load time, no need to optimize
it to avoid a conditional inside the small loop.
jeremyevans committed Oct 18, 2023
1 parent 554143f commit f9e5a3f
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions lib/roda/response.rb
Original file line number Diff line number Diff line change
@@ -10,19 +10,15 @@ class Roda
# headers used internally by Roda can be lower case on Rack 3, so that it is
# possible to use a plain hash of response headers instead of using Rack::Headers.
module RodaResponseHeaders
headers = %w'Allow Cache-Control Content-Disposition Content-Encoding Content-Length
Content-Security-Policy Content-Security-Policy-Report-Only Content-Type
ETag Expires Last-Modified Link Location Set-Cookie Transfer-Encoding Vary'.freeze.each(&:freeze)
downcase = defined?(Rack::Headers) && Rack::Headers.is_a?(Class)

if defined?(Rack::Headers) && Rack::Headers.is_a?(Class)
headers.each do |mixed_case|
const_set(mixed_case.gsub('-', '_').upcase!.to_sym, mixed_case.downcase.freeze)
end
else
headers.each do |mixed_case|
const_set(mixed_case.gsub('-', '_').upcase!.to_sym, mixed_case.freeze)
%w'Allow Cache-Control Content-Disposition Content-Encoding Content-Length
Content-Security-Policy Content-Security-Policy-Report-Only Content-Type
ETag Expires Last-Modified Link Location Set-Cookie Transfer-Encoding Vary'.
each do |value|
value = value.downcase if downcase
const_set(value.gsub('-', '_').upcase!.to_sym, value.freeze)
end
end
end

# Base class used for Roda responses. The instance methods for this

0 comments on commit f9e5a3f

Please sign in to comment.