Class: Down::Http

Inherits:
Backend show all
Defined in:
lib/down/http.rb

Defined Under Namespace

Modules: DownloadedFile

Instance Method Summary collapse

Methods inherited from Backend

download, open

Constructor Details

#initialize(client_or_options = {}) ⇒ Http

Returns a new instance of Http.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/down/http.rb', line 17

def initialize(client_or_options = {})
  options = client_or_options.is_a?(HTTP::Client) ? client_or_options.default_options.to_hash : client_or_options

  @method  = options.delete(:method) || :get
  @options = {
    headers:         { "User-Agent" => "Down/#{Down::VERSION}" },
    follow:          { max_hops: 2 },
    timeout_class:   HTTP::Timeout::PerOperation,
    timeout_options: { write_timeout: 30, connect_timeout: 30, read_timeout: 30 }
  }.merge(options)
end

Instance Method Details

#download(url, max_size: nil, progress_proc: nil, content_length_proc: nil, destination: nil, **options, &block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/down/http.rb', line 29

def download(url, max_size: nil, progress_proc: nil, content_length_proc: nil, destination: nil, **options, &block)
  io = open(url, **options, rewindable: false, &block)

  content_length_proc.call(io.size) if content_length_proc && io.size

  if max_size && io.size && io.size > max_size
    raise Down::TooLarge, "file is too large (max is #{max_size/1024/1024}MB)"
  end

  extname  = File.extname(io.data[:response].uri.path)
  tempfile = Tempfile.new(["down-http", extname], binmode: true)

  until io.eof?
    chunk = io.readpartial(nil, buffer ||= String.new)

    tempfile.write(chunk)

    progress_proc.call(tempfile.size) if progress_proc

    if max_size && tempfile.size > max_size
      raise Down::TooLarge, "file is too large (max is #{max_size/1024/1024}MB)"
    end
  end

  tempfile.open # flush written content

  tempfile.extend Down::Http::DownloadedFile
  tempfile.url     = io.data[:response].uri.to_s
  tempfile.headers = io.data[:headers]

  download_result(tempfile, destination)
rescue
  tempfile.close! if tempfile
  raise
ensure
  io.close if io
end

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



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/down/http.rb', line 67

def open(url, rewindable: true, **options, &block)
  response = request(url, **options, &block)

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

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