Class: Mistri::Content::Image
- Inherits:
-
Data
- Object
- Data
- Mistri::Content::Image
- Defined in:
- lib/mistri/content.rb
Overview
A base64-encoded image with its MIME type.
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#mime_type ⇒ Object
readonly
Returns the value of attribute mime_type.
Class Method Summary collapse
-
.from_bytes(bytes, mime_type:) ⇒ Object
Frozen at the pack site so the initializer's ownership check skips a second copy of what can be a multi-megabyte payload.
-
.from_data_uri(uri) ⇒ Object
Browsers, canvases, and upload pipelines hand images around as data: URIs; accept them directly.
Instance Method Summary collapse
- #bytes ⇒ Object
-
#initialize(data:, mime_type:) ⇒ Image
constructor
A new instance of Image.
- #to_h ⇒ Object
- #type ⇒ Object
Constructor Details
#initialize(data:, mime_type:) ⇒ Image
Returns a new instance of Image.
62 63 64 |
# File 'lib/mistri/content.rb', line 62 def initialize(data:, mime_type:) super(data: Content.freeze_string(data), mime_type: Content.freeze_string(mime_type)) end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data
48 49 50 |
# File 'lib/mistri/content.rb', line 48 def data @data end |
#mime_type ⇒ Object (readonly)
Returns the value of attribute mime_type
48 49 50 |
# File 'lib/mistri/content.rb', line 48 def mime_type @mime_type end |
Class Method Details
.from_bytes(bytes, mime_type:) ⇒ Object
Frozen at the pack site so the initializer's ownership check skips a second copy of what can be a multi-megabyte payload.
51 |
# File 'lib/mistri/content.rb', line 51 def self.from_bytes(bytes, mime_type:) = new(data: [bytes.b].pack("m0").freeze, mime_type:) |
.from_data_uri(uri) ⇒ Object
Browsers, canvases, and upload pipelines hand images around as data: URIs; accept them directly.
55 56 57 58 59 60 |
# File 'lib/mistri/content.rb', line 55 def self.from_data_uri(uri) match = /\Adata:(?<mime>[^;,]+);base64,(?<data>.+)\z/m.match(uri.to_s) raise ArgumentError, "not a base64 data: URI" unless match new(data: match[:data].delete("\n").freeze, mime_type: match[:mime]) end |
Instance Method Details
#bytes ⇒ Object
68 |
# File 'lib/mistri/content.rb', line 68 def bytes = data.unpack1("m0") |
#to_h ⇒ Object
70 |
# File 'lib/mistri/content.rb', line 70 def to_h = { type: :image, data:, mime_type: } |
#type ⇒ Object
66 |
# File 'lib/mistri/content.rb', line 66 def type = :image |