Class: Vectory::Datauri

Inherits:
Image
  • Object
show all
Defined in:
lib/vectory/datauri.rb

Constant Summary collapse

REGEX =
%r{^
  data:
  (?<mimetype>[^;]+);
  (?:charset=[^;]+;)?
  base64,
  (?<data>.+)
$}x.freeze

Instance Attribute Summary

Attributes inherited from Image

#content

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Image

from_content, from_path, #initialize

Constructor Details

This class inherits a constructor from Vectory::Image

Class Method Details

.from_vector(vector) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/vectory/datauri.rb', line 13

def self.from_vector(vector)
  mimetype = vector.class.mimetype
  content = vector.content.gsub("\r\n", "\n")
  data = Base64.strict_encode64(content)

  new("data:#{mimetype};base64,#{data}")
end

Instance Method Details

#heightObject



26
27
28
# File 'lib/vectory/datauri.rb', line 26

def height
  to_vector.height
end

#mimeObject



21
22
23
24
# File 'lib/vectory/datauri.rb', line 21

def mime
  match = parse_datauri(@content)
  match[:mimetype]
end

#to_vectorObject



34
35
36
37
38
39
40
# File 'lib/vectory/datauri.rb', line 34

def to_vector
  match = parse_datauri(@content)
  content = Base64.strict_decode64(match[:data])
  image_class = detect_image_class(match[:mimetype])

  image_class.from_content(content)
end

#widthObject



30
31
32
# File 'lib/vectory/datauri.rb', line 30

def width
  to_vector.width
end