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



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

def size
  `#@native.size`
end

#typeString (readonly)

Returns blob mime type.

Returns:



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, 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.



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.

Returns:

  • (File)

    a renamed blob



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