Class: Browser::Blob
- Includes:
- NativeCachedWrapper
- Defined in:
- opal/browser/blob.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#size ⇒ Integer
readonly
Blob size in bytes.
-
#type ⇒ String
readonly
Blob mime type.
Class Method Summary collapse
-
.create(from, options = {}) ⇒ Object
Create a new blob from anything that Blob API supports.
Instance Method Summary collapse
-
#buffer ⇒ Object
Buffer view into the blob.
-
#rename(new_filename) ⇒ File
Rename a blob and return a File with a new name.
-
#slice(start, finish = nil) ⇒ Object
Create a new blob by slicing this blob.
-
#text(&block) ⇒ Object
Convert a blob to a UTF-8 encoded string.
-
#to_url(window = $window) ⇒ Object
Convert a blob to an URL that can be used to reference this blob in DOM eg.
Methods included from NativeCachedWrapper
#restricted?, #set_native_reference
Instance Attribute Details
#size ⇒ Integer (readonly)
Returns blob size in bytes.
14 15 16 |
# File 'opal/browser/blob.rb', line 14 def size `#@native.size` end |
#type ⇒ String (readonly)
Returns blob mime type.
20 21 22 |
# File 'opal/browser/blob.rb', line 20 def type `#@native.type` end |
Class Method Details
.create(from, options = {}) ⇒ Object
Create a new blob from anything that Blob API supports
8 9 10 |
# File 'opal/browser/blob.rb', line 8 def self.create(from, ={}) new(`new Blob(#{Native.convert(from)}, #{.to_n})`) end |
Instance Method Details
#buffer ⇒ Object
Buffer view into the blob
If block is given it will be called with a parameter once we receive the buffer. Otherwise return a Promise which will resolve once we receive it.
44 45 46 47 48 49 50 51 52 53 |
# File 'opal/browser/blob.rb', line 44 def buffer promise = nil unless block_given? promise = Promise.new block = proc { |i| promise.resolve(i) } end resblock = proc { |i| block.call(Buffer.new(i)) } `#@native.arrayBuffer().then(#{resblock.to_n})` promise end |
#rename(new_filename) ⇒ File
Rename a blob and return a File with a new name.
69 70 71 72 73 |
# File 'opal/browser/blob.rb', line 69 def rename(new_filename) File.create([self], new_filename, type: type, lastModified: respond_to?(:last_modified) ? last_modified : Time.now) end |
#slice(start, finish = nil) ⇒ Object
Create a new blob by slicing this blob
56 57 58 |
# File 'opal/browser/blob.rb', line 56 def slice(start, finish=nil) Blob.new(`#@native.slice(#{start}, #{finish})`) end |
#text(&block) ⇒ Object
Convert a blob to a UTF-8 encoded string.
If block is given it will be called with a parameter once we receive the text. Otherwise return a Promise which will resolve once we receive it.
29 30 31 32 33 34 35 36 37 |
# File 'opal/browser/blob.rb', line 29 def text(&block) promise = nil unless block_given? promise = Promise.new block = proc { |i| promise.resolve(i) } end `#@native.text().then(#{block.to_n})` promise end |
#to_url(window = $window) ⇒ Object
Convert a blob to an URL that can be used to reference this blob in DOM eg. display some multimedia
62 63 64 |
# File 'opal/browser/blob.rb', line 62 def to_url(window=$window) `#{window.to_n}.URL.createObjectURL(#@native)` end |