Class: SvgoWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/svgo_wrapper.rb,
lib/svgo_wrapper/error.rb,
lib/svgo_wrapper/version.rb,
lib/svgo_wrapper/constants.rb,
lib/svgo_wrapper/svgo_check.rb,
lib/svgo_wrapper/parser_error.rb

Defined Under Namespace

Classes: Error, ParserError

Constant Summary collapse

VERSION =
"0.0.4".freeze
DEFAULT_TIMEOUT =

seconds

60
VALID_PLUGINS =
Set[:cleanupAttrs, :cleanupEnableBackground, :cleanupIDs, :cleanupNumericValues, :collapseGroups,
:convertColors, :convertPathData, :convertShapeToPath, :convertStyleToAttrs, :convertTransform,
:mergePaths, :moveElemsAttrsToGroup, :moveGroupAttrsToElems, :removeComments, :removeDesc,
:removeDoctype, :removeEditorsNSData, :removeEmptyAttrs, :removeEmptyContainers, :removeEmptyText,
:removeHiddenElems, :removeMetadata, :removeNonInheritableGroupAttrs, :removeRasterImages,
:removeTitle, :removeUnknownsAndDefaults, :removeUnusedNS, :removeUselessStrokeAndFill,
:removeViewBox, :removeXMLProcInst, :sortAttrs, :transformsWithOnePath].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enable: nil, disable: nil, timeout: DEFAULT_TIMEOUT) ⇒ SvgoWrapper

Returns a new instance of SvgoWrapper.



14
15
16
17
18
19
# File 'lib/svgo_wrapper.rb', line 14

def initialize(enable: nil, disable: nil, timeout: DEFAULT_TIMEOUT)
  self.enabled_plugins = enable
  self.disabled_plugins = disable
  self.plugin_args = generate_plugin_args(enabled: enabled_plugins, disabled: disabled_plugins)
  self.timeout = timeout
end

Instance Attribute Details

#disabled_pluginsObject

Returns the value of attribute disabled_plugins.



12
13
14
# File 'lib/svgo_wrapper.rb', line 12

def disabled_plugins
  @disabled_plugins
end

#enabled_pluginsObject

Returns the value of attribute enabled_plugins.



12
13
14
# File 'lib/svgo_wrapper.rb', line 12

def enabled_plugins
  @enabled_plugins
end

#timeoutObject

Returns the value of attribute timeout.



12
13
14
# File 'lib/svgo_wrapper.rb', line 12

def timeout
  @timeout
end

Class Method Details

.svgo_present?Boolean

Returns:

  • (Boolean)


6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/svgo_wrapper/svgo_check.rb', line 6

def svgo_present?
  begin
    Open4.spawn "svgo -i - -o -",
                stdin: " <svg/>",
                stdout: output = "",
                stdout_timeout: 5
  rescue
    return false
  end

  output.start_with?("<svg")
end

Instance Method Details

#optimize_images_data(data) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/svgo_wrapper.rb', line 21

def optimize_images_data(data)
  begin
    Open4.spawn ["svgo", *plugin_args, "-i", "-", "-o", "-"],
                stdin: data,
                stdout: output = "",
                stdout_timeout: timeout
  rescue Open4::SpawnError => e
    raise Error, "Unexpected error (#{e.exitstatus})\n"
  end

  verify_output(output)
  output
end