initials 0.2.2 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/initials/svg.rb +45 -8
- data/lib/initials/version.rb +1 -1
- metadata +2 -14
- data/.github/workflows/ruby.yml +0 -39
- data/.gitignore +0 -14
- data/.rspec +0 -3
- data/.travis.yml +0 -7
- data/Gemfile +0 -8
- data/LICENSE.txt +0 -21
- data/README.md +0 -67
- data/Rakefile +0 -6
- data/bin/console +0 -14
- data/bin/setup +0 -8
- data/demo.png +0 -0
- data/initials.gemspec +0 -29
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e9e93b9af7fecf572fbcf123770f1c94d35cfd7206de6498ba0b9820186f91ad
|
4
|
+
data.tar.gz: f2435541f81450b5e7fd458527579fb9c8fcf36b6bd3571c802531412daf1393
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: acf34c95dc2200b59f83835dbe885b45a694fbe8cd22dfe6fea46b5776839f5b236a7e0537c6177a6e1e76206ca73d32fefefeb4e5a6845bff6aeafb6bb7c0ed
|
7
|
+
data.tar.gz: 0a0aafb85bb929a5dcb785064693d4329eb5f127c8b8668f466d332c5424dae7f58ddac2bd8674656e79a1683ca7a71348ebc05ce71f813907bd9a77f3d520fe
|
data/lib/initials/svg.rb
CHANGED
@@ -1,18 +1,32 @@
|
|
1
1
|
module Initials
|
2
2
|
class SVG
|
3
|
-
|
3
|
+
HUE_WHEEL = 360
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
attr_reader :name, :colors, :limit, :shape, :size
|
6
|
+
|
7
|
+
def initialize(name, colors: 12, limit: 3, shape: :circle, size: 32)
|
8
|
+
@name = name.to_s.strip
|
9
|
+
@colors = colors
|
10
|
+
@limit = limit
|
11
|
+
@shape = shape
|
8
12
|
@size = size
|
13
|
+
|
14
|
+
raise Initials::Error.new("Colors must be a divider of 360 e.g. 24 but not 16.") unless valid_colors?
|
15
|
+
raise Initials::Error.new("Size is not a positive integer.") unless valid_size?
|
16
|
+
end
|
17
|
+
|
18
|
+
def name
|
19
|
+
@name.empty? ? "?" : @name
|
9
20
|
end
|
10
21
|
|
11
22
|
def to_s
|
12
23
|
svg = [
|
13
24
|
"<svg width='#{size}' height='#{size}'>",
|
14
|
-
|
15
|
-
|
25
|
+
shape == :rect ?
|
26
|
+
"<rect width='#{size}' height='#{size}' rx='#{size / 32}' ry='#{size / 32}' fill='#{fill}' />"
|
27
|
+
:
|
28
|
+
"<circle cx='#{size / 2}' cy='#{size / 2}' r='#{size / 2}' fill='#{fill}' />",
|
29
|
+
"<text x='50%' y='50%' fill='white' fill-opacity='0.75' dominant-baseline='central' text-anchor='middle' style='font-size: #{font_size}px; font-family: -apple-system, BlinkMacSystemFont, \"Segoe UI\", Roboto, Oxygen-Sans, Ubuntu, Cantarell, \"Helvetica Neue\", sans-serif; user-select: none;'>",
|
16
30
|
"#{initials}",
|
17
31
|
"</text>",
|
18
32
|
"</svg>"
|
@@ -22,7 +36,17 @@ module Initials
|
|
22
36
|
end
|
23
37
|
|
24
38
|
def fill
|
25
|
-
|
39
|
+
return "hsl(0, 0%, 67%)" if @name.empty?
|
40
|
+
|
41
|
+
hue_step = HUE_WHEEL / colors
|
42
|
+
char_sum = name.split("").sum do |c|
|
43
|
+
# Multiplication makes sure neighboring characters (like A and B) are one hue step apart.
|
44
|
+
c.ord * hue_step
|
45
|
+
end
|
46
|
+
|
47
|
+
# Spin the wheel!
|
48
|
+
hue = char_sum % HUE_WHEEL
|
49
|
+
|
26
50
|
"hsl(#{hue}, 40%, 40%)"
|
27
51
|
end
|
28
52
|
|
@@ -31,7 +55,20 @@ module Initials
|
|
31
55
|
end
|
32
56
|
|
33
57
|
def initials
|
34
|
-
name.split(' ')[0,
|
58
|
+
name.split(' ')[0, limit].map { |s| s[0].capitalize }.join
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
def valid_colors?
|
64
|
+
return false unless colors.respond_to?(:to_i)
|
65
|
+
return false unless colors > 0
|
66
|
+
HUE_WHEEL % colors == 0
|
67
|
+
end
|
68
|
+
|
69
|
+
def valid_size?
|
70
|
+
return false unless size.respond_to?(:to_i)
|
71
|
+
size.to_i > 0
|
35
72
|
end
|
36
73
|
end
|
37
74
|
end
|
data/lib/initials/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: initials
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Thomas Hutterer
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-01-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -59,18 +59,6 @@ executables: []
|
|
59
59
|
extensions: []
|
60
60
|
extra_rdoc_files: []
|
61
61
|
files:
|
62
|
-
- ".github/workflows/ruby.yml"
|
63
|
-
- ".gitignore"
|
64
|
-
- ".rspec"
|
65
|
-
- ".travis.yml"
|
66
|
-
- Gemfile
|
67
|
-
- LICENSE.txt
|
68
|
-
- README.md
|
69
|
-
- Rakefile
|
70
|
-
- bin/console
|
71
|
-
- bin/setup
|
72
|
-
- demo.png
|
73
|
-
- initials.gemspec
|
74
62
|
- lib/initials.rb
|
75
63
|
- lib/initials/svg.rb
|
76
64
|
- lib/initials/version.rb
|
data/.github/workflows/ruby.yml
DELETED
@@ -1,39 +0,0 @@
|
|
1
|
-
# This workflow uses actions that are not certified by GitHub.
|
2
|
-
# They are provided by a third-party and are governed by
|
3
|
-
# separate terms of service, privacy policy, and support
|
4
|
-
# documentation.
|
5
|
-
# This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
|
6
|
-
# For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
|
7
|
-
|
8
|
-
name: Ruby
|
9
|
-
|
10
|
-
on:
|
11
|
-
push:
|
12
|
-
branches: [ main ]
|
13
|
-
pull_request:
|
14
|
-
branches: [ main ]
|
15
|
-
|
16
|
-
jobs:
|
17
|
-
test:
|
18
|
-
|
19
|
-
runs-on: ubuntu-latest
|
20
|
-
strategy:
|
21
|
-
matrix:
|
22
|
-
ruby-version: ['2.6', '2.7', '3.0']
|
23
|
-
|
24
|
-
steps:
|
25
|
-
- uses: actions/checkout@v2
|
26
|
-
- name: Set up Ruby
|
27
|
-
# To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
|
28
|
-
# change this to (see https://github.com/ruby/setup-ruby#versioning):
|
29
|
-
# uses: ruby/setup-ruby@v1
|
30
|
-
uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
|
31
|
-
with:
|
32
|
-
ruby-version: ${{ matrix.ruby-version }}
|
33
|
-
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
|
34
|
-
- name: Run tests and publish code coverage
|
35
|
-
uses: paambaati/codeclimate-action@v2.7.5
|
36
|
-
env:
|
37
|
-
CC_TEST_REPORTER_ID: 9c690ab71ab481ac45785c4010ff30f93a45774e5ad6f03ec448b6e2b5bc39b4
|
38
|
-
with:
|
39
|
-
coverageCommand: bundle exec rake
|
data/.gitignore
DELETED
data/.rspec
DELETED
data/.travis.yml
DELETED
data/Gemfile
DELETED
data/LICENSE.txt
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
The MIT License (MIT)
|
2
|
-
|
3
|
-
Copyright (c) 2021 Thomas Hutterer
|
4
|
-
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
7
|
-
in the Software without restriction, including without limitation the rights
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
10
|
-
furnished to do so, subject to the following conditions:
|
11
|
-
|
12
|
-
The above copyright notice and this permission notice shall be included in
|
13
|
-
all copies or substantial portions of the Software.
|
14
|
-
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
-
THE SOFTWARE.
|
data/README.md
DELETED
@@ -1,67 +0,0 @@
|
|
1
|
-
# ![demo](demo.png) Initials [![Maintainability](https://api.codeclimate.com/v1/badges/fb865ec4adcd0671dc48/maintainability)](https://codeclimate.com/github/thutterer/initials/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/fb865ec4adcd0671dc48/test_coverage)](https://codeclimate.com/github/thutterer/initials/test_coverage)
|
2
|
-
|
3
|
-
Don't want to implement user avatar uploads but still have basic avatars to distinguish users and brigthen up your app?
|
4
|
-
|
5
|
-
Use colorful SVGs as user avatars in any Ruby and Rails application.
|
6
|
-
|
7
|
-
## Installation
|
8
|
-
|
9
|
-
Add this line to your application's Gemfile:
|
10
|
-
|
11
|
-
```ruby
|
12
|
-
gem 'initials'
|
13
|
-
```
|
14
|
-
|
15
|
-
And then execute:
|
16
|
-
|
17
|
-
$ bundle
|
18
|
-
|
19
|
-
Or install it yourself as:
|
20
|
-
|
21
|
-
$ gem install initials
|
22
|
-
|
23
|
-
## Usage
|
24
|
-
|
25
|
-
Call `Initials.svg("Morty Smith")` anywhere to get a colorful initials avatar SVG as string.
|
26
|
-
|
27
|
-
### Rails
|
28
|
-
|
29
|
-
No special configuration is required to work with Ruby on Rails, but for your convinience, you can add this to your `app/helpers/application_helper.rb`:
|
30
|
-
|
31
|
-
```ruby
|
32
|
-
def user_avatar(name, **options)
|
33
|
-
Initials.svg(name, **options)
|
34
|
-
end
|
35
|
-
```
|
36
|
-
|
37
|
-
Now you can create SVGs in all views:
|
38
|
-
|
39
|
-
```erb
|
40
|
-
<%= user_avatar(current_user.name) %>
|
41
|
-
```
|
42
|
-
|
43
|
-
Initials automatically marks its created SVG strings as `html_safe`.
|
44
|
-
|
45
|
-
### Options
|
46
|
-
|
47
|
-
You can pass the following options into `Initials.svg` or your `user_avatar` helper:
|
48
|
-
|
49
|
-
```rb
|
50
|
-
user_avatar(current_user.name, size: 96)
|
51
|
-
```
|
52
|
-
|
53
|
-
This sets `width` and `height` to `96px` in the SVG. Of course, you can also use CSS to make the SVG have different sizes in different places of your HTML.
|
54
|
-
|
55
|
-
## Development
|
56
|
-
|
57
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
58
|
-
|
59
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
60
|
-
|
61
|
-
## Contributing
|
62
|
-
|
63
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/thutterer/initials.
|
64
|
-
|
65
|
-
## License
|
66
|
-
|
67
|
-
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
DELETED
data/bin/console
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require "bundler/setup"
|
4
|
-
require "initials"
|
5
|
-
|
6
|
-
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
-
# with your gem easier. You can also use a different console, if you like.
|
8
|
-
|
9
|
-
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
-
# require "pry"
|
11
|
-
# Pry.start
|
12
|
-
|
13
|
-
require "irb"
|
14
|
-
IRB.start(__FILE__)
|
data/bin/setup
DELETED
data/demo.png
DELETED
Binary file
|
data/initials.gemspec
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
|
2
|
-
lib = File.expand_path("../lib", __FILE__)
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require "initials/version"
|
5
|
-
|
6
|
-
Gem::Specification.new do |spec|
|
7
|
-
spec.name = "initials"
|
8
|
-
spec.version = Initials::VERSION
|
9
|
-
spec.authors = ["Thomas Hutterer"]
|
10
|
-
spec.email = ["tohu@tuta.io"]
|
11
|
-
|
12
|
-
spec.summary = "Simple SVG avatars"
|
13
|
-
spec.description = "Use colorful SVGs as user avatars in any Ruby and Rails application."
|
14
|
-
spec.homepage = "https://github.com/thutterer/initials"
|
15
|
-
spec.license = "MIT"
|
16
|
-
|
17
|
-
# Specify which files should be added to the gem when it is released.
|
18
|
-
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
|
-
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
20
|
-
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
-
end
|
22
|
-
spec.bindir = "exe"
|
23
|
-
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
-
spec.require_paths = ["lib"]
|
25
|
-
|
26
|
-
spec.add_development_dependency "bundler"
|
27
|
-
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
-
spec.add_development_dependency "rspec", "~> 3.0"
|
29
|
-
end
|