Sha256: 030af25fb2445f357c869fe207d8ba072f7608eb0049b7ee92699e206998bb1a

Contents?: true

Size: 987 Bytes

Versions: 3

Compression:

Stored size: 987 Bytes

Contents

class TransferBuilder
  attr_reader :items
  class TransferIOError < StandardError; end

  def initialize
    @items = []
  end

  def add_file(name:, io:)
    ensure_io_compliant!(io)
    @items << FutureFileItem.new(name: name, io: io)
    true
  end

  def add_file_at(path:)
    add_file(name: File.basename(path), io: File.open(path, 'rb'))
  end

  def add_web_url(url:, title: nil)
    title ||= url
    @items << FutureWebItem.new(url: url, title: title)
    true
  end

  def ensure_io_compliant!(io)
    io.seek(0)
    io.read(1) # Will cause things like Errno::EACCESS to happen early, before the upload begins
    io.seek(0) # Also rewinds the IO for later uploading action
    size = io.size # Will cause a NoMethodError
    raise TransferIOError, 'The IO object given to add_file has a size of 0' if size <= 0
  rescue NoMethodError
    raise TransferIOError, "The IO object given to add_file must respond to seek(), read() and size(), but #{io.inspect} did not"
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
wetransfer-0.4.4 lib/we_transfer_client/transfer_builder.rb
wetransfer-0.4.3 lib/we_transfer_client/transfer_builder.rb
wetransfer-0.4.2 lib/we_transfer_client/transfer_builder.rb