Class: SourceMonitor::Favicons::SvgConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/source_monitor/favicons/svg_converter.rb

Constant Summary collapse

PNG_CONTENT_TYPE =
"image/png"
DEFAULT_SIZE =
64

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(svg_body, filename:, size:) ⇒ SvgConverter

Returns a new instance of SvgConverter.



17
18
19
20
21
# File 'lib/source_monitor/favicons/svg_converter.rb', line 17

def initialize(svg_body, filename:, size:)
  @svg_body = svg_body
  @filename = filename.sub(/\.svg\z/i, ".png")
  @size = size
end

Class Method Details

.call(svg_body, filename: "favicon.png", size: DEFAULT_SIZE) ⇒ Object

Converts an SVG string to PNG bytes using MiniMagick. Returns a Hash with :io, :content_type, :filename or nil on failure.



11
12
13
14
15
# File 'lib/source_monitor/favicons/svg_converter.rb', line 11

def self.call(svg_body, filename: "favicon.png", size: DEFAULT_SIZE)
  return nil unless defined?(MiniMagick)

  new(svg_body, filename: filename, size: size).call
end

Instance Method Details

#callObject



23
24
25
26
27
28
# File 'lib/source_monitor/favicons/svg_converter.rb', line 23

def call
  convert_svg_to_png
rescue StandardError => e
  log_conversion_failure(e)
  nil
end