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.



8
9
10
11
# File 'lib/initial_avatar/avatar.rb', line 8

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.



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

def opts
  @opts
end

#textObject (readonly)

Returns the value of attribute text.



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

def text
  @text
end

Instance Method Details

#data_uriObject



21
22
23
# File 'lib/initial_avatar/avatar.rb', line 21

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

#svg_tagObject



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

def svg_tag
  "    <svg xmlns=\"http://www.w3.org/2000/svg\" pointer-events=\"none\" width=\"\#{opts[:size]}\" height=\"\#{opts[:size]}\" style=\"background-color: \#{color};\">\n    <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>\n    </svg>\n  SVG\nend\n"