Skip to content

Commit

Permalink
Improved documentation for Protocol::HTTP::AcceptEncoding.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Nov 28, 2024
1 parent 06baa24 commit 00bf9d9
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions lib/protocol/http/accept_encoding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,37 @@

module Protocol
module HTTP
# Set a valid accept-encoding header and decode the response.
# A middleware that sets the accept-encoding header and decodes the response according to the content-encoding header.
class AcceptEncoding < Middleware
# The header used to request encodings.
ACCEPT_ENCODING = "accept-encoding".freeze

# The header used to specify encodings.
CONTENT_ENCODING = "content-encoding".freeze

# The default wrappers to use for decoding content.
DEFAULT_WRAPPERS = {
"gzip" => Body::Inflate.method(:for),

# There is no point including this:
# 'identity' => ->(body){body},
}

def initialize(app, wrappers = DEFAULT_WRAPPERS)
super(app)
# Initialize the middleware with the given delegate and wrappers.
#
# @parameter delegate [Protocol::HTTP::Middleware] The delegate middleware.
# @parameter wrappers [Hash] A hash of encoding names to wrapper functions.
def initialize(delegate, wrappers = DEFAULT_WRAPPERS)
super(delegate)

@accept_encoding = wrappers.keys.join(", ")
@wrappers = wrappers
end

# Set the accept-encoding header and decode the response body.
#
# @parameter request [Protocol::HTTP::Request] The request to modify.
# @returns [Protocol::HTTP::Response] The response.
def call(request)
request.headers[ACCEPT_ENCODING] = @accept_encoding

Expand Down

0 comments on commit 00bf9d9

Please sign in to comment.