Class: SvgSprite::SVG

Inherits:
Object
  • Object
show all
Defined in:
lib/svg_sprite/svg.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#pathObject (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

#base64Object

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_uriObject

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

#contentsObject

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_uriObject

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

#encodedObject

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

#encodingObject

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.

Returns:

  • (Boolean)


15
16
17
# File 'lib/svg_sprite/svg.rb', line 15

def has_dimensions?
  width && height
end

#heightObject

Return the <svg>‘s height.



30
31
32
# File 'lib/svg_sprite/svg.rb', line 30

def height
  svg["height"]
end

#nameObject

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

#optimizedObject

Return the optimized content.



40
41
42
# File 'lib/svg_sprite/svg.rb', line 40

def optimized
  @optimized ||= SvgOptimizer.optimize(contents)
end

#svgObject

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_uriObject

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

#widthObject

Return the <svg>‘s width.



25
26
27
# File 'lib/svg_sprite/svg.rb', line 25

def width
  svg["width"]
end