Class: Rpictogrify::Pictogram

Inherits:
Object
  • Object
show all
Defined in:
lib/rpictogrify/pictogram.rb

Constant Summary collapse

VERSION =

BUMP UP if avatar algorithm changes

1

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, options = {}) ⇒ Pictogram

Returns a new instance of Pictogram.



12
13
14
15
16
17
# File 'lib/rpictogrify/pictogram.rb', line 12

def initialize(text, options = {})
  @text    = text.to_s
  @options = options
  @theme   = Rpictogrify::Theme.find(options[:theme] || Rpictogrify.config.theme)
  @uid     = generate_uid
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/rpictogrify/pictogram.rb', line 10

def options
  @options
end

#textObject (readonly)

Returns the value of attribute text.



10
11
12
# File 'lib/rpictogrify/pictogram.rb', line 10

def text
  @text
end

#themeObject (readonly)

Returns the value of attribute theme.



10
11
12
# File 'lib/rpictogrify/pictogram.rb', line 10

def theme
  @theme
end

#uidObject (readonly)

Returns the value of attribute uid.



10
11
12
# File 'lib/rpictogrify/pictogram.rb', line 10

def uid
  @uid
end

Class Method Details

.base_pathObject



62
63
64
# File 'lib/rpictogrify/pictogram.rb', line 62

def base_path
  File.join (Rpictogrify.config.base_path || 'public/system'), 'rpictogrify', "#{VERSION}"
end

Instance Method Details

#base64Object



40
41
42
# File 'lib/rpictogrify/pictogram.rb', line 40

def base64
  "data:image/svg+xml;base64,#{Base64.encode64(svg)[0...-1]}"
end

#generateObject



19
20
21
22
# File 'lib/rpictogrify/pictogram.rb', line 19

def generate
  File.write(path, svg) unless File.exist?(path)
  path
end

#pathObject



48
49
50
51
52
53
54
55
# File 'lib/rpictogrify/pictogram.rb', line 48

def path
  @path ||= begin
    dir = File.join(self.class.base_path, theme.ident)
    FileUtils.mkdir_p(dir)

    File.join(dir, "#{text_hash}.svg")
  end
end

#sizeObject



44
45
46
# File 'lib/rpictogrify/pictogram.rb', line 44

def size
  @size ||= theme.width || 300
end

#svgObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rpictogrify/pictogram.rb', line 24

def svg
  includes = symbols.map do |shape, symbol|
    fillable = fill[shape] ? "fill='#{fill[shape]}'" : ''
    "<svg class='#{shape}' #{fillable} xmlns='http://www.w3.org/2000/svg'>#{symbol}</svg>"
  end

  <<-XML.strip
    <svg viewBox="0 0 #{size} #{size}" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
      <g>
        <rect fill="#{colors['background']}" x="0" y="0" width="#{size}" height="#{size}"></rect>
        #{includes.join("\n")}
      </g>
    </svg>
  XML
end

#text_hashObject



57
58
59
# File 'lib/rpictogrify/pictogram.rb', line 57

def text_hash
  @text_hash ||= XXhash.xxh32(text)
end