Method: Down::Http#open

Defined in:
lib/down/http.rb

#open(url, rewindable: true, **options, &block) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/down/http.rb', line 61

def open(url, rewindable: true, **options, &block)
  begin
    response = get(url, **options, &block)
  rescue => exception
    request_error!(exception)
  end

  response_error!(response) unless response.status.success?

  body_chunks = Enumerator.new do |yielder|
    begin
      response.body.each { |chunk| yielder << chunk }
    rescue => exception
      request_error!(exception)
    end
  end

  Down::ChunkedIO.new(
    chunks:     body_chunks,
    size:       response.content_length,
    encoding:   response.content_type.charset,
    rewindable: rewindable,
    on_close:   (-> { response.connection.close } unless @client.persistent?),
    data:       { status: response.code, headers: response.headers.to_h, response: response },
  )
end