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.
66 67 68 |
# File 'lib/mistri/content.rb', line 66 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
52 53 54 |
# File 'lib/mistri/content.rb', line 52 def data @data end |
#mime_type ⇒ Object (readonly)
Returns the value of attribute mime_type
52 53 54 |
# File 'lib/mistri/content.rb', line 52 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.
55 |
# File 'lib/mistri/content.rb', line 55 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.
59 60 61 62 63 64 |
# File 'lib/mistri/content.rb', line 59 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
72 |
# File 'lib/mistri/content.rb', line 72 def bytes = data.unpack1("m0") |
#to_h ⇒ Object
74 |
# File 'lib/mistri/content.rb', line 74 def to_h = { type: :image, data:, mime_type: } |
#type ⇒ Object
70 |
# File 'lib/mistri/content.rb', line 70 def type = :image |