Class: TransferBuilder
- Inherits:
-
Object
- Object
- TransferBuilder
- Defined in:
- lib/we_transfer_client/transfer_builder.rb
Defined Under Namespace
Classes: TransferIOError
Instance Attribute Summary collapse
-
#items ⇒ Object
readonly
Returns the value of attribute items.
Instance Method Summary collapse
- #add_file(name:, io:) ⇒ Object
- #add_file_at(path:) ⇒ Object
- #add_web_url(url:, title: nil) ⇒ Object
- #ensure_io_compliant!(io) ⇒ Object
-
#initialize ⇒ TransferBuilder
constructor
A new instance of TransferBuilder.
Constructor Details
#initialize ⇒ TransferBuilder
Returns a new instance of TransferBuilder.
5 6 7 |
# File 'lib/we_transfer_client/transfer_builder.rb', line 5 def initialize @items = [] end |
Instance Attribute Details
#items ⇒ Object (readonly)
Returns the value of attribute items.
2 3 4 |
# File 'lib/we_transfer_client/transfer_builder.rb', line 2 def items @items end |
Instance Method Details
#add_file(name:, io:) ⇒ Object
9 10 11 12 13 |
# File 'lib/we_transfer_client/transfer_builder.rb', line 9 def add_file(name:, io:) ensure_io_compliant!(io) @items << FutureFileItem.new(name: name, io: io) true end |
#add_file_at(path:) ⇒ Object
15 16 17 |
# File 'lib/we_transfer_client/transfer_builder.rb', line 15 def add_file_at(path:) add_file(name: File.basename(path), io: File.open(path, 'rb')) end |
#add_web_url(url:, title: nil) ⇒ Object
19 20 21 22 23 |
# File 'lib/we_transfer_client/transfer_builder.rb', line 19 def add_web_url(url:, title: nil) title ||= url @items << FutureWebItem.new(url: url, title: title) true end |
#ensure_io_compliant!(io) ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/we_transfer_client/transfer_builder.rb', line 25 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 |