Class: Net::HTTP::UploadProgress

Inherits:
Object
  • Object
show all
Defined in:
lib/fir/patches/progress.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(req, &block) ⇒ UploadProgress

Returns a new instance of UploadProgress.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fir/patches/progress.rb', line 8

def initialize(req, &block)
  @req = req
  @callback = block
  @upload_size = 0
  if req.body_stream
    @io = req.body_stream
    req.body_stream = self
  elsif req.instance_variable_get(:@body_data)
    raise NotImplementedError if req.chunked?
    raise NotImplementedError if /\Amultipart\/form-data\z/i !~ req.content_type
    opt = req.instance_variable_get(:@form_option).dup
    opt[:boundary] ||= SecureRandom.urlsafe_base64(40)
    req.set_content_type(req.content_type, boundary: opt[:boundary])
    file = Tempfile.new('multipart')
    file.binmode
    req.send(:encode_multipart_form_data, file, req.instance_variable_get(:@body_data), opt)
    file.rewind
    req.content_length = file.size
    @io = file
    req.body_stream = self
  else
    raise NotImplementedError
  end
end

Instance Attribute Details

#upload_sizeObject (readonly)

Returns the value of attribute upload_size.



6
7
8
# File 'lib/fir/patches/progress.rb', line 6

def upload_size
  @upload_size
end

Instance Method Details

#readpartial(maxlen, outbuf) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/fir/patches/progress.rb', line 33

def readpartial(maxlen, outbuf)
  begin
    str = @io.readpartial(maxlen, outbuf)
  ensure
    @callback.call(self) unless @upload_size.zero?
  end
  @upload_size += str.length
  str
end