Class: Calvatar::Avatar
- Inherits:
-
Object
- Object
- Calvatar::Avatar
- Defined in:
- lib/calvatar.rb
Constant Summary collapse
- DEFAULT_COLORS =
[ '#FF5733', '#33FF57', '#3357FF', '#F1C40F', '#9B59B6', '#E67E22', '#1ABC9C', '#2ECC71', '#3498DB', '#34495E' ].freeze
- DEFAULT_SIZE =
50
Class Method Summary collapse
Instance Method Summary collapse
- #generate ⇒ Object
-
#initialize(name, colors: DEFAULT_COLORS, size: DEFAULT_SIZE) ⇒ Avatar
constructor
A new instance of Avatar.
Constructor Details
#initialize(name, colors: DEFAULT_COLORS, size: DEFAULT_SIZE) ⇒ Avatar
Returns a new instance of Avatar.
15 16 17 18 19 |
# File 'lib/calvatar.rb', line 15 def initialize(name, colors: DEFAULT_COLORS, size: DEFAULT_SIZE) @initials = extract_initials(name) @color = colors.sample @size = size end |
Class Method Details
.generate_avatar(name, colors: DEFAULT_COLORS, size: DEFAULT_SIZE) ⇒ Object
31 32 33 |
# File 'lib/calvatar.rb', line 31 def self.generate_avatar(name, colors: DEFAULT_COLORS, size: DEFAULT_SIZE) new(name, colors: colors, size: size).generate end |
Instance Method Details
#generate ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/calvatar.rb', line 21 def generate svg = " <svg xmlns='http://www.w3.org/2000/svg' width='\#{@size}' height='\#{@size}'>\n <rect width='100%' height='100%' fill='\#{@color}'/>\n <text x='50%' y='50%' font-size='\#{@size * 0.8}' text-anchor='middle' fill='white' dy='.3em'>\#{@initials}</text>\n </svg>\n SVG\n svg.html_safe\nend\n" |