Ruby Uploader

This library provides a simple HTTP uploader based on chunks transfer. It supports handlers, useful to make custom request configuration and to read the progression of the file transfer.

Quickstart

First, install ruby-uploader

gem install ruby-uploader

ruby-uploader set the necessary headers and configure the request to enable chunk transfer. Below, the most basic example do not require any configuration

require 'ruby-uploader/uploader'

uploader = Uploader::Upload.new(URI('https://server/path/to/upload'), 'myfile.bin')

uploader.execute

Optionally, headers can be set using a Hash parameter

uploader = Uploader::Upload.new(URI('https://server/path/to/upload'), 'myfile.bin', { 'custom-header' => 'value' })

uploader.execute

For more configuration capabilities, see the Handlers section

Handlers

ruby-uploader support handlers at four different steps of the upload request process

  • Before the request is fired
  • Before each chunk is sent
  • After each chunk has been sent
  • After the HTTP response is received

Set a handler is done usine the method add_handler on the Upload object. several handlers can be add on the uploader, even several of the same type.

Add a :before handler

This handler is triggered before the request is fired. It give access to the request object which is an instance of type Net::HTTPRequest from the net/http standard API.

class BeforeRequest
  def execute(request)
    ...
  end
end

uploader.add_handler :before, BeforeRequest.new

Add a :before_chunk handler

This handler is triggered for each chunk, before it is sent to the server. It give access to the following information :

  • chunk - the chunk of data
  • count - the number of the chunk (first chunk is 0)
  • total_count - total number of chunks to transfer
  • content_length - the value of the corresponding HTTP header
class BeforeChunk
  def execute(chunk, count, total_count, content_length)
    ...
  end
end

uploader.add_handler :before_chunk, BeforeChunk.new

Add a :after_chunk handler

This handler is triggered for each chunk, after it has been sent to the server. Its structure is identical to the one of the before_chunk handler

class AfterChunk
  def execute(chunk, count, total_count, content_length)
    ...
  end
end

uploader.add_handler :after_chunk, AfterChunk.new

Add a :after handler

This handler is triggered after the response is received for the server. It give access to the response object which is an instance of type Net::HTTPResponse from the net/http standard API.

class AfterRequest
  def execute(response)
    ...
  end
end

uploader.add_handler :after, AfterRequest.new