supertramp 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6e782e06f94b9db00d129ed79674a626cba15d6dd4aba1f9412ff487600e9e77
4
+ data.tar.gz: 46f4119919f82b072578a62009d628e69215745d4d3f33f3bc1c2c624969ae9a
5
+ SHA512:
6
+ metadata.gz: 66b40bd408dbad5300c9659b0fd2cb3e7f38df1d0d3f705bc9f192ebb8c826dec8d648bd66df517897a8787432a44d16d98be6ba93a76e399e0448228854967c
7
+ data.tar.gz: 19ef9e61382f857bf1a0e18fddfa970bff2695abd60fbe72d5de6b725b357f971f46600d5e84c50a52dda43a221563558ad65a143edcf0effad29eb0f6879755
data/lib/supertramp.rb ADDED
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'supertramp/config'
4
+ require_relative 'supertramp/avatar'
5
+
6
+ class Supertramp
7
+ @@config = Config.new
8
+
9
+ def initialize(initials: nil, name: nil, background: nil, shape: nil)
10
+ @initials = initials
11
+ @name = name
12
+ @background = background
13
+ @shape = shape
14
+
15
+ validate_arguments
16
+ end
17
+
18
+ def to_s
19
+ Avatar.new(initials: initials, background: background, shape: shape).to_s
20
+ end
21
+
22
+ def self.configure
23
+ yield @@config
24
+ end
25
+
26
+ private
27
+
28
+ attr_accessor :name
29
+
30
+ def validate_arguments
31
+ raise ArgumentError, 'either `initials:` or `name:` must be specified' unless @name || @initials
32
+ raise ArgumentError, "`shape:` must be one of #{Avatar::SHAPES}" unless Avatar::SHAPES.include?(shape)
33
+ end
34
+
35
+ def config
36
+ @@config
37
+ end
38
+
39
+ def initials
40
+ @initials ||= initials_from_name
41
+
42
+ return @initials.upcase if config.uppercase
43
+
44
+ @initials
45
+ end
46
+
47
+ def initials_from_name
48
+ name.split.map { |n| n[0] }.join
49
+ end
50
+
51
+ def background
52
+ @background ||= background_for_initials
53
+ end
54
+
55
+ def background_for_initials
56
+ Random.srand(initials_seed)
57
+ index = (rand * config.colours.length).floor
58
+ config.colours[index]
59
+ end
60
+
61
+ def initials_seed
62
+ initials.downcase.chars.map(&:ord).sum
63
+ end
64
+
65
+ def shape
66
+ @shape ||= config.shape
67
+ end
68
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'erb'
4
+
5
+ class Supertramp
6
+ class Avatar
7
+ SHAPES = [
8
+ SQUARE = 'square',
9
+ CIRCLE = 'circle',
10
+ ROUNDED = 'rounded'
11
+ ].freeze
12
+
13
+ def initialize(initials:, background:, shape:)
14
+ @initials = initials
15
+ @background = background
16
+ @shape = shape
17
+ end
18
+
19
+ def to_s
20
+ bind_template('avatar')
21
+ end
22
+
23
+ def shape
24
+ bind_template("_#{@shape}")
25
+ end
26
+
27
+ private
28
+
29
+ def bind_template(file)
30
+ ERB.new(template(file)).result(binding)
31
+ end
32
+
33
+ def template(file)
34
+ File.read(template_path(file))
35
+ end
36
+
37
+ def template_path(file)
38
+ File.expand_path("../templates/#{file}.svg.erb", File.dirname(__FILE__))
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Supertramp
4
+ class Config
5
+ attr_accessor :colours, :uppercase, :shape
6
+ alias colors= colours=
7
+
8
+ def initialize
9
+ @colours = %w[#B91C1C #B45309 #047857 #1D4ED8 #6D28D9]
10
+ @uppercase = true
11
+ @shape = Avatar::SQUARE
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Supertramp
4
+ VERSION = '0.6.1'
5
+ end
@@ -0,0 +1 @@
1
+ <circle fill="<%= @background %>" r="50%" cx="25" cy="25"></circle>
@@ -0,0 +1 @@
1
+ <rect width="100%" height="100%" fill="<%= @background %>" rx="7" ry="7"></rect>
@@ -0,0 +1 @@
1
+ <rect width="100%" height="100%" fill="<%= @background %>"/>
@@ -0,0 +1,7 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="50" height="50" viewBox="0 0 50 50">
3
+ <%= shape %>
4
+ <text fill="#fff" font-family="Helvetica,Arial,sans-serif" font-size="26" font-weight="500" x="50%" y="55%" dominant-baseline="middle" text-anchor="middle">
5
+ <%= @initials %>
6
+ </text>
7
+ </svg>
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: supertramp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.6.1
5
+ platform: ruby
6
+ authors:
7
+ - Matt Bearman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-07-31 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - matt@mattbearman.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/supertramp.rb
21
+ - lib/supertramp/avatar.rb
22
+ - lib/supertramp/config.rb
23
+ - lib/supertramp/version.rb
24
+ - lib/templates/_circle.svg.erb
25
+ - lib/templates/_rounded.svg.erb
26
+ - lib/templates/_square.svg.erb
27
+ - lib/templates/avatar.svg.erb
28
+ homepage: https://github.com/mattbearman/supertramp
29
+ licenses:
30
+ - MIT
31
+ metadata:
32
+ homepage_uri: https://github.com/mattbearman/supertramp
33
+ source_code_uri: https://github.com/mattbearman/supertramp
34
+ changelog_uri: https://github.com/mattbearman/supertramp/CHANGELOG.md
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 2.3.0
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubygems_version: 3.2.25
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: Creates SVG letter avatars on the fly with consistent colours.
54
+ test_files: []