Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions lib/down/httpx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def initialize(**options, &block)
end


# Downlods the remote file to disk. Accepts HTTPX options via a hash or a
# Downloads the remote file to disk. Accepts HTTPX options via a hash or a
# block, and some additional options as well.
def download(url, max_size: nil, progress_proc: nil, content_length_proc: nil, destination: nil, extension: nil, tempfile_name: nil, **options, &block)
client = @client
Expand Down Expand Up @@ -82,7 +82,7 @@ def download(url, max_size: nil, progress_proc: nil, content_length_proc: nil, d
end

# Starts retrieving the remote file and returns an IO-like object which
# downloads the response body on-demand. Accepts HTTP.rb options via a hash
# downloads the response body on-demand. Accepts HTTPX options via a hash
# or a block.
def open(url, rewindable: true, **options, &block)
response = request(@client, url, stream: true, **options, &block)
Expand All @@ -105,15 +105,23 @@ def open(url, rewindable: true, **options, &block)

# Yields chunks of the response body to the block.
def stream_body(response, &block)
response.each(&block)
response.each do |chunk|
next if (300..399).include?(response.status)

block.call(chunk)
end

if (300..399).include?(response.status)
raise Down::TooManyRedirects, "too many redirects"
end
rescue => exception
request_error!(exception)
end

def request(client, url, method: @method, **options, &block)
response = send_request(client, method, url, **options, &block)
response.raise_for_status
response_error!(response) unless (200..299).include?(response.status)
response_error!(response) unless (200..399).include?(response.status)
response
rescue HTTPX::HTTPError
response_error!(response)
Expand All @@ -140,7 +148,6 @@ def response_error!(response)
args = [response.status.to_s, response]

case response.status
when 300..399 then raise Down::TooManyRedirects, "too many redirects"
when 404 then raise Down::NotFound.new(*args)
when 400..499 then raise Down::ClientError.new(*args)
when 500..599 then raise Down::ServerError.new(*args)
Expand Down
Loading