Class: SvgSprite::SVG
- Inherits:
-
Object
- Object
- SvgSprite::SVG
- Defined in:
- lib/svg_sprite/svg.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#base64 ⇒ Object
Return the Base64-encoded version of the content.
-
#base64_data_uri ⇒ Object
Return the Base64 version of the data URI.
-
#contents ⇒ Object
Return the raw content.
-
#data_uri ⇒ Object
Return the smaller data URI.
-
#encoded ⇒ Object
Return the URL-encoded version of the content.
-
#encoding ⇒ Object
The output encoding based on the global configuration.
-
#has_dimensions? ⇒ Boolean
Detect if SVG has all dimensions defined.
-
#height ⇒ Object
Return the <svg>‘s height.
-
#initialize(path) ⇒ SVG
constructor
A new instance of SVG.
-
#name ⇒ Object
Return the image file name without the extension.
-
#optimized ⇒ Object
Return the optimized content.
-
#svg ⇒ Object
Return a Nokogiri representation of the <svg> element.
-
#urlencoded_data_uri ⇒ Object
Return the URL-encoded version of the data URI.
-
#width ⇒ Object
Return the <svg>‘s width.
Constructor Details
#initialize(path) ⇒ SVG
Returns a new instance of SVG.
5 6 7 |
# File 'lib/svg_sprite/svg.rb', line 5 def initialize(path) @path = path end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
3 4 5 |
# File 'lib/svg_sprite/svg.rb', line 3 def path @path end |
Instance Method Details
#base64 ⇒ Object
Return the Base64-encoded version of the content.
50 51 52 |
# File 'lib/svg_sprite/svg.rb', line 50 def base64 Base64.strict_encode64(optimized) end |
#base64_data_uri ⇒ Object
Return the Base64 version of the data URI.
60 61 62 |
# File 'lib/svg_sprite/svg.rb', line 60 def base64_data_uri %[data:image/svg+xml;base64,#{base64}] end |
#contents ⇒ Object
Return the raw content. This content is not optimized by svg_optimizer.
35 36 37 |
# File 'lib/svg_sprite/svg.rb', line 35 def contents @contents ||= File.read(path) end |
#data_uri ⇒ Object
Return the smaller data URI.
55 56 57 |
# File 'lib/svg_sprite/svg.rb', line 55 def data_uri [base64_data_uri, urlencoded_data_uri].sort_by(&:bytesize).first end |
#encoded ⇒ Object
Return the URL-encoded version of the content.
45 46 47 |
# File 'lib/svg_sprite/svg.rb', line 45 def encoded CGI.escape(contents) end |
#encoding ⇒ Object
The output encoding based on the global configuration.
70 71 72 |
# File 'lib/svg_sprite/svg.rb', line 70 def encoding Encoding.default_external.name end |
#has_dimensions? ⇒ Boolean
Detect if SVG has all dimensions defined.
15 16 17 |
# File 'lib/svg_sprite/svg.rb', line 15 def has_dimensions? width && height end |
#height ⇒ Object
Return the <svg>‘s height.
30 31 32 |
# File 'lib/svg_sprite/svg.rb', line 30 def height svg["height"] end |
#name ⇒ Object
Return the image file name without the extension.
10 11 12 |
# File 'lib/svg_sprite/svg.rb', line 10 def name File.basename(path, ".*") end |
#optimized ⇒ Object
Return the optimized content.
40 41 42 |
# File 'lib/svg_sprite/svg.rb', line 40 def optimized @optimized ||= SvgOptimizer.optimize(contents) end |
#svg ⇒ Object
Return a Nokogiri representation of the <svg> element.
20 21 22 |
# File 'lib/svg_sprite/svg.rb', line 20 def svg @svg ||= Nokogiri::XML(contents).css("svg").first end |
#urlencoded_data_uri ⇒ Object
Return the URL-encoded version of the data URI.
65 66 67 |
# File 'lib/svg_sprite/svg.rb', line 65 def urlencoded_data_uri %[data:image/svg+xml;charset=#{encoding},#{encoded}] end |
#width ⇒ Object
Return the <svg>‘s width.
25 26 27 |
# File 'lib/svg_sprite/svg.rb', line 25 def width svg["width"] end |