initials 0.2.2 → 0.4.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b2dce220e10036fd5284e737264c5ed0a4f0f4e47211d4fec4812207e28dd3cf
4
- data.tar.gz: 3eeebc342465b151e48ebd94d01e98d2f6a1bd0194851c5a2fc50d41103553af
3
+ metadata.gz: e9e93b9af7fecf572fbcf123770f1c94d35cfd7206de6498ba0b9820186f91ad
4
+ data.tar.gz: f2435541f81450b5e7fd458527579fb9c8fcf36b6bd3571c802531412daf1393
5
5
  SHA512:
6
- metadata.gz: 660d98bbbb542bf6a706614ab725c63f5d2e991236ff23ddaf443ec35ef2e0709677b51f4e3d94ffc9bea81f647d8135cf206513e07617710820cba2b71b7253
7
- data.tar.gz: ddeb25f094cae8ffd4d8d7b01dc022b71898a45df031a231051d2f18bfce8a1b5392b9ed4e3ae41dba4398087ab7856a3f2f0f21c74dabed382cb5151c93998d
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
- attr_reader :name, :size
3
+ HUE_WHEEL = 360
4
4
 
5
- def initialize(name, size: 32)
6
- raise Initials::Error.new("Name is not a string or empty") unless (name.respond_to?(:to_s) && name.to_s.length > 0)
7
- @name = name
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
- "<circle cx='#{size / 2}' cy='#{size / 2}' r='#{size / 2}' fill='#{fill}' />",
15
- "<text x='50%' y='50%' fill='white' fill-opacity='0.75' dominant-baseline='central' text-anchor='middle' style='font: bold #{font_size}px sans-serif; user-select: none;'>",
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
- hue = name.split("").sum { |c| c.ord } % 360
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, 3].map { |s| s[0].capitalize }.join
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
@@ -1,3 +1,3 @@
1
1
  module Initials
2
- VERSION = "0.2.2"
2
+ VERSION = "0.4.2"
3
3
  end
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.2.2
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: 2021-06-14 00:00:00.000000000 Z
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
@@ -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
@@ -1,14 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /_yardoc/
4
- /coverage/
5
- /doc/
6
- /pkg/
7
- /spec/reports/
8
- /tmp/
9
-
10
- # rspec failure tracking
11
- .rspec_status
12
-
13
- # SimpleCov reports
14
- coverage
data/.rspec DELETED
@@ -1,3 +0,0 @@
1
- --format documentation
2
- --color
3
- --require spec_helper
data/.travis.yml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- sudo: false
3
- language: ruby
4
- cache: bundler
5
- rvm:
6
- - 2.6.5
7
- before_install: gem install bundler -v 1.17.3
data/Gemfile DELETED
@@ -1,8 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
-
5
- # Specify your gem's dependencies in initials.gemspec
6
- gemspec
7
-
8
- gem 'simplecov', require: false, group: :test
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
@@ -1,6 +0,0 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
3
-
4
- RSpec::Core::RakeTask.new(:spec)
5
-
6
- task :default => :spec
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
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
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