Sha256: 5110940dc080b1cb508eeb6c257ddd75e4c80f302f8dc50e2448c94a6ce9d31d

Contents?: true

Size: 968 Bytes

Versions: 4

Compression:

Stored size: 968 Bytes

Contents

class BoardBuilder
  attr_reader :items
  class TransferIOError < StandardError; end

  def initialize
    @items = []
  end

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

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

  def add_web_url(url:, title: url)
    @items << FutureLink.new(url: url, title: title)
  end

  private

  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, "#{File.basename(io)}, given to add_file has a size of 0" if size <= 0
  rescue NoMethodError
    raise TransferIOError, "#{File.basename(io)}, given to add_file must respond to seek(), read() and size(), but #{io.inspect} did not"
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
wetransfer-0.9.0.beta3 lib/we_transfer_client/board_builder.rb
wetransfer-0.9.0.beta2 lib/we_transfer_client/board_builder.rb
wetransfer-0.9.0.beta1 lib/we_transfer_client/board_builder.rb
wetransfer-0.9.0.beta lib/we_transfer_client/board_builder.rb