Skip to content

Commit

Permalink
Cleanup HTTP::Response
Browse files Browse the repository at this point in the history
  • Loading branch information
ixti committed Dec 13, 2015
1 parent 8c7c66a commit 6de9119
Showing 1 changed file with 10 additions and 21 deletions.
31 changes: 10 additions & 21 deletions lib/http/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ def initialize(opts)
@uri = opts.include?(:uri) && HTTP::URI.parse(opts.fetch(:uri))
@status = HTTP::Response::Status.new opts.fetch(:status)
@headers = HTTP::Headers.coerce(opts.fetch(:headers, {}))
@encoding = opts.fetch(:encoding, nil)

if opts.include?(:connection)
@body = Response::Body.new(opts.fetch(:connection), resolved_encoding)
encoding = opts[:encoding] || charset || Encoding::BINARY
@body = Response::Body.new(opts.fetch(:connection), encoding)
else
@body = opts.fetch(:body)
end
Expand Down Expand Up @@ -85,19 +85,15 @@ def content_type
@content_type ||= ContentType.parse headers[Headers::CONTENT_TYPE]
end

# MIME type of response (if any)
#
# @return [String, nil]
def mime_type
@mime_type ||= content_type.mime_type
end
# @!method mime_type
# MIME type of response (if any)
# @return [String, nil]
def_delegator :content_type, :mime_type

# Charset of response (if any)
#
# @return [String, nil]
def charset
@charset ||= content_type.charset
end
# @!method charset
# Charset of response (if any)
# @return [String, nil]
def_delegator :content_type, :charset

def cookies
@cookies ||= headers.each_with_object CookieJar.new do |(k, v), jar|
Expand All @@ -119,12 +115,5 @@ def parse(as = nil)
def inspect
"#<#{self.class}/#{@version} #{code} #{reason} #{headers.to_h.inspect}>"
end

private

# Work out what encoding to assume for the body
def resolved_encoding
@encoding || charset || Encoding::BINARY
end
end
end

0 comments on commit 6de9119

Please sign in to comment.