Class: Cifrado::StreamingUploader

Inherits:
Object
  • Object
show all
Defined in:
lib/cifrado/streaming_uploader.rb

Class Method Summary collapse

Class Method Details

.put(to_url, params = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cifrado/streaming_uploader.rb', line 11

def put(to_url, params = {})
  
  file = params[:file]
  headers = params[:headers] || {}
  chunker = nil
  rate_limit = nil
  rate_limit = Cifrado::RateLimit.new(params[:bwlimit]) if params[:bwlimit]

  if file
    headers.merge!({ 'Content-Length' => File.size(file.path).to_s })

    chunker = lambda do
      chunk = file.read(4096).to_s

      rate_limit.limit(chunk.size) if rate_limit

      if block_given? and chunk.size > 0
        yield chunk.size
      end
      chunk
    end
  end

  Excon.put to_url,
            :headers => headers, 
            :request_block => chunker
end