Class: InitialAvatar::Avatar

Inherits:
Object
  • Object
show all
Defined in:
lib/initial_avatar/avatar.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Avatar.



6
7
8
9
# File 'lib/initial_avatar/avatar.rb', line 6

def initialize(text, options = {})
  @text = text.upcase
  @opts = InitialAvatar.configuration.default_options.merge(options)
end

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



4
5
6
# File 'lib/initial_avatar/avatar.rb', line 4

def opts
  @opts
end

#textObject (readonly)

Returns the value of attribute text.



3
4
5
# File 'lib/initial_avatar/avatar.rb', line 3

def text
  @text
end

Instance Method Details

#data_uriObject



19
20
21
# File 'lib/initial_avatar/avatar.rb', line 19

def data_uri
  "data:image/svg+xml;base64,#{Base64.strict_encode64(svg_tag)}"
end

#svg_tagObject



11
12
13
14
15
16
17
# File 'lib/initial_avatar/avatar.rb', line 11

def svg_tag
  <<~SVG
    <svg xmlns="http://www.w3.org/2000/svg" pointer-events="none" width="#{opts[:size]}" height="#{opts[:size]}" style="background-color: #{color};">
    <text text-anchor="middle" y="50%" x="50%" dy="0.35em" pointer-events="auto" fill="#{opts[:text_color]}" font-family="#{opts[:font_family]}" style="font-weight: #{opts[:font_weight]}; font-size: #{font_size}px;">#{text}</text>
    </svg>
  SVG
end