Class: Dommy::Blob
- Inherits:
-
Object
- Object
- Dommy::Blob
- Defined in:
- lib/dommy/blob.rb
Overview
‘Blob` — opaque binary chunk with a MIME type, mirroring the File API’s ‘Blob` interface. Used by File, FormData, and any code that needs to round-trip bytes through the DOM (e.g. a `<input type=“file”>` test scenario).
Direct Known Subclasses
Instance Attribute Summary collapse
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#__bytes__ ⇒ Object
Raw binary bytes (Ruby ASCII-8BIT string).
- #__js_call__(method, args) ⇒ Object
- #__js_get__(key) ⇒ Object
-
#array_buffer ⇒ Object
Read the bytes as an Array<Integer>.
-
#initialize(parts = [], options = {}) ⇒ Blob
constructor
Construct a Blob from a list of parts.
-
#slice(start = 0, last = @size, content_type = "") ⇒ Object
Return a new Blob over a byte range of this one.
-
#text ⇒ Object
Read the bytes as UTF-8 text.
Constructor Details
#initialize(parts = [], options = {}) ⇒ Blob
Construct a Blob from a list of parts. Each part can be:
- String (treated as binary bytes)
- Blob / File (their bytes are concatenated)
- Array<Integer> (byte values, like ArrayBuffer)
- anything else: coerced via to_s
‘options` sets the MIME type (lowercased per spec).
20 21 22 23 24 25 26 |
# File 'lib/dommy/blob.rb', line 20 def initialize(parts = [], = {}) parts = [parts] unless parts.is_a?(Array) @data = collect_bytes(parts) @size = @data.bytesize raw_type = ["type"] || [:type] || "" @type = raw_type.to_s.downcase end |
Instance Attribute Details
#size ⇒ Object (readonly)
Returns the value of attribute size.
11 12 13 |
# File 'lib/dommy/blob.rb', line 11 def size @size end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
11 12 13 |
# File 'lib/dommy/blob.rb', line 11 def type @type end |
Instance Method Details
#__bytes__ ⇒ Object
Raw binary bytes (Ruby ASCII-8BIT string). Used by FormData / fetch when serializing multipart bodies.
51 52 53 |
# File 'lib/dommy/blob.rb', line 51 def __bytes__ @data end |
#__js_call__(method, args) ⇒ Object
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/dommy/blob.rb', line 64 def __js_call__(method, args) case method when "slice" slice(args[0] || 0, args[1] || @size, args[2] || "") when "text" text when "arrayBuffer" array_buffer end end |
#__js_get__(key) ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/dommy/blob.rb', line 55 def __js_get__(key) case key when "size" @size when "type" @type end end |
#array_buffer ⇒ Object
Read the bytes as an Array<Integer>. The DOM spec returns a Promise<ArrayBuffer>; Dommy is synchronous.
45 46 47 |
# File 'lib/dommy/blob.rb', line 45 def array_buffer @data.bytes end |
#slice(start = 0, last = @size, content_type = "") ⇒ Object
Return a new Blob over a byte range of this one. Negative indices are treated as offsets from the end (per spec).
30 31 32 33 34 35 |
# File 'lib/dommy/blob.rb', line 30 def slice(start = 0, last = @size, content_type = "") s = clamp_index(start.to_i, @size) e = clamp_index(last.to_i, @size) e = s if e < s Blob.new([@data.byteslice(s, e - s) || ""], "type" => content_type.to_s) end |
#text ⇒ Object
Read the bytes as UTF-8 text. The DOM spec returns a Promise, but Dommy is synchronous, so callers can use the result directly.
39 40 41 |
# File 'lib/dommy/blob.rb', line 39 def text @data.dup.force_encoding(Encoding::UTF_8) end |