Class: Browser::Blob

Inherits:
Object show all
Includes:
NativeCachedWrapper
Defined in:
opal/browser/blob.rb

Direct Known Subclasses

File

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from NativeCachedWrapper

#restricted?, #set_native_reference

Instance Attribute Details

#sizeInteger (readonly)

Returns blob size in bytes.

Returns:

  • (Integer)

    blob size in bytes


13
14
15
# File 'opal/browser/blob.rb', line 13

def size
  `#@native.size`
end

#typeString (readonly)

Returns blob mime type.

Returns:


19
20
21
# File 'opal/browser/blob.rb', line 19

def type
  `#@native.type`
end

Class Method Details

.create(from, options = {}) ⇒ Object

Create a new blob from anything that Blob API supports


7
8
9
# File 'opal/browser/blob.rb', line 7

def self.create(from, options={})
  new(`new Blob(#{Native.convert(from)}, #{options.to_n})`)
end

Instance Method Details

#bufferObject

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.


43
44
45
46
47
48
49
50
51
52
# File 'opal/browser/blob.rb', line 43

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.

Returns:

  • (File)

    a renamed blob


68
69
70
71
72
# File 'opal/browser/blob.rb', line 68

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


55
56
57
# File 'opal/browser/blob.rb', line 55

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.


28
29
30
31
32
33
34
35
36
# File 'opal/browser/blob.rb', line 28

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


61
62
63
# File 'opal/browser/blob.rb', line 61

def to_url(window=$window)
  `#{window.to_n}.URL.createObjectURL(#@native)`
end