Class: SvgOptimizer
- Inherits:
-
Object
- Object
- SvgOptimizer
- Defined in:
- lib/svgo.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize(options = SvgoOptions.new) {|options| ... } ⇒ SvgOptimizer
constructor
A new instance of SvgOptimizer.
- #optimize(svg_data) ⇒ Object
- #optimize_file(svg_file) ⇒ Object
Constructor Details
#initialize(options = SvgoOptions.new) {|options| ... } ⇒ SvgOptimizer
Returns a new instance of SvgOptimizer.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/svgo.rb', line 115 def initialize(=SvgoOptions.new) yield if block_given? if .is_a? SvgoOptions = . else = end if not [:plugins] [:plugins] = PLUGINS_DEFAULT end if not [:plugins].is_a? Array raise StandardError.new("`options.plugins` should be an Array.") end [:plugins] = [:plugins].map { | p | p.is_a?(Hash) ? p : {p => true} } svgo_js = File.("../../svgo-js/svgo-built.js", __FILE__) svgo_module = File.open(svgo_js, "r:utf-8", &:read) @context = MiniRacer::Context.new @context.eval svgo_module @context.call("svgo", {setup: }) end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
114 115 116 |
# File 'lib/svgo.rb', line 114 def end |
Instance Method Details
#optimize(svg_data) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/svgo.rb', line 138 def optimize(svg_data) result = @context.call("svgo", {optimize: svg_data.to_s}) if not result raise StandardError.new("Bad response from JavaScript runtime.") end if result['errors'].length > 0 if result['errors'].length > 1 err = %Q(Errors occurred: \n - #{result['errors'].join("\n - s")}) else err = "An error occurred: #{result['errors'][0]}" end raise StandardError.new(err) end result end |
#optimize_file(svg_file) ⇒ Object
154 155 156 |
# File 'lib/svgo.rb', line 154 def optimize_file(svg_file) optimize(File.read(svg_file)) end |