rack-identicon 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +18 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +65 -0
- data/Rakefile +11 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/config.ru +4 -0
- data/lib/rack/identicon.rb +27 -0
- data/lib/rack/identicon/caching.rb +38 -0
- data/lib/rack/identicon/railtie.rb +9 -0
- data/lib/rack/identicon/response.rb +55 -0
- data/lib/rack/identicon/version.rb +5 -0
- data/rack-identicon.gemspec +30 -0
- metadata +104 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 91e47361453e1627c892cec13a707b1853607fb5
|
4
|
+
data.tar.gz: ac7ce868b788671e4d694831d1a5283a8b07a5a6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 654d524cf8c8b1c78b7e9a14ce01a390331c9a375d97f3ac72a872cb32b700977ba720838daad24faa7dc37c106799aad95840c066d0f291f9985657dd9e85f6
|
7
|
+
data.tar.gz: df25355f4f40c907a69e0d2293e62d4abb5c846870caf2cae939887f0a97e593bff7eee68735384f5bb4e06f177d185e60e4f245fc5ea2fce65af6f36a90dad9
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
---
|
2
|
+
language: ruby
|
3
|
+
cache: bundler
|
4
|
+
rvm:
|
5
|
+
- 2.4
|
6
|
+
- 2.5
|
7
|
+
- 2.6
|
8
|
+
- 2.7
|
9
|
+
- ruby-head
|
10
|
+
before_install: gem install bundler -v 2.1.4
|
11
|
+
deploy:
|
12
|
+
provider: rubygems
|
13
|
+
gem: rake-identicon
|
14
|
+
api_key:
|
15
|
+
secure: VpqLsVFF5QepC59kIu32n11SGUIrAgWdHdFS2ubb4+xi4lIQps9ImZFxHAuZjeakNVYJ110AETG+BH6Lblw52xNIQgd+toX10Y/SmiRHVrkMS+AJ9wuuEhWjcxzNIu+2GWrpAVKFa6+dLNxkWaoF/6zGIA/KG2pUyH9jZv3XhYgVRD7unn3f3llEgEbVVkIAv5nx6PtWaYYi0KciCM57yQSTNsckhQQGhAXiVbHOcIDzas42EBtja+PT7IOITGTz5S8dDrTTn56RNnumCD6BUwJ3SayHf9Hsc4zdnYxHzHEAhL/TPv4/b8WcHj1COAliDU1Ko2GsZWn9Wa99xeGFN07553ezA/wvytd2z6YPwOg/Iy4J1XuiKC1XWI2mPEyZ/cbZ46ysZw7uIuN2n5stLRxTgE74Jczf2nFVGLTk7DyRQNdLuSDbzjRqso/cg1NpFXSv/EzFHve84ydAuPDiOx5VTaEv4zMyV224DzbjoI3xzb3wgF2b/BcgxnUKucrZDsRv0/tyKzhWj6SkgBubDKr+CBMl5zTW8XXofdiL7wStCRLBrUKBzBX9mxCWzPTs4F4UYYyrbIOsmm+UOJny1eTcq7AtAw+zH7swVH/Lknz+EyUy001OhrAf3nIu74671gLhyS1F8nQs7Jvnd+V6jLvD23lNyt5msI47+jo6Jr8=
|
16
|
+
on:
|
17
|
+
tags: true
|
18
|
+
rvm: 2.7
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 glaszig
|
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
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
# Rack::Identicon
|
2
|
+
|
3
|
+
Rack middleware to generate identicons through the [identicon gem](https://rubygems.org/gems/identicon).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'rack-identicon'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install rack-identicon
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Use the middleware with plain rack:
|
24
|
+
|
25
|
+
```rb
|
26
|
+
# config.ru
|
27
|
+
require "rack/identicon"
|
28
|
+
map("/__identicon__") { run Rack::Identicon.new }
|
29
|
+
run lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['OK']] }
|
30
|
+
```
|
31
|
+
|
32
|
+
Use with Rails:
|
33
|
+
|
34
|
+
```rb
|
35
|
+
# config/routes.rb
|
36
|
+
Rails.application.routes.draw do
|
37
|
+
mount Rack::Identicon.new, at: "/__identicon__", as: :identicon
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
### Caching
|
42
|
+
|
43
|
+
Rack::Identicon supports caching through any object implementing the methods
|
44
|
+
`read(key)`, `write(key, data)`, `exist?(key)` and `fetch(key, &blk)`.
|
45
|
+
When used with Rails it'll use `Rails.cache` automatically. Outside of Rails
|
46
|
+
you'll need to configure your cache store like this:
|
47
|
+
|
48
|
+
```rb
|
49
|
+
Rack::Identicon.cache_store = MyStore.new
|
50
|
+
```
|
51
|
+
|
52
|
+
## Development
|
53
|
+
|
54
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
55
|
+
|
56
|
+
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).
|
57
|
+
|
58
|
+
## Contributing
|
59
|
+
|
60
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/glaszig/rack-identicon.
|
61
|
+
|
62
|
+
|
63
|
+
## License
|
64
|
+
|
65
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "rack/identicon"
|
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
ADDED
data/config.ru
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require "rack/identicon/version"
|
2
|
+
require "rack/identicon/caching"
|
3
|
+
require "rack/identicon/response"
|
4
|
+
|
5
|
+
module Rack
|
6
|
+
module Identicon
|
7
|
+
class Error < StandardError; end
|
8
|
+
|
9
|
+
include Caching
|
10
|
+
|
11
|
+
def self.new *args
|
12
|
+
Middleware.new *args
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.call env
|
16
|
+
new.call env
|
17
|
+
end
|
18
|
+
|
19
|
+
class Middleware
|
20
|
+
def call env
|
21
|
+
Response.new(env).triplet
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
require "rack/identicon/railtie" if defined? ::Rails
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Rack
|
2
|
+
module Identicon
|
3
|
+
module Caching
|
4
|
+
|
5
|
+
NAMESPACE = "rack-identicon".freeze
|
6
|
+
|
7
|
+
def self.included base
|
8
|
+
base.extend ClassMethods
|
9
|
+
base.cache_store = NullCacheStore.new
|
10
|
+
end
|
11
|
+
|
12
|
+
class NullCacheStore
|
13
|
+
def exist? key
|
14
|
+
false
|
15
|
+
end
|
16
|
+
|
17
|
+
def read key
|
18
|
+
end
|
19
|
+
|
20
|
+
def write key, data
|
21
|
+
end
|
22
|
+
|
23
|
+
def fetch key
|
24
|
+
yield
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
module ClassMethods
|
29
|
+
attr_accessor :cache_store
|
30
|
+
|
31
|
+
def cache key, &blk
|
32
|
+
cache_store.fetch NAMESPACE + "/" + key, &blk
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require "digest"
|
2
|
+
require "identicon"
|
3
|
+
|
4
|
+
module Rack
|
5
|
+
module Identicon
|
6
|
+
class Response
|
7
|
+
DEFAULT_SIZE = 128
|
8
|
+
DEFAULT_BG = "ffffff".freeze
|
9
|
+
|
10
|
+
def initialize env
|
11
|
+
@request = Rack::Request.new env
|
12
|
+
@size = @request.params["s"] || DEFAULT_SIZE
|
13
|
+
@bg = parse_color @request.params.fetch "b", DEFAULT_BG
|
14
|
+
end
|
15
|
+
|
16
|
+
def body
|
17
|
+
cache { ::Identicon.blob_for hashed_payload, @size.to_i, @bg }
|
18
|
+
end
|
19
|
+
|
20
|
+
def triplet
|
21
|
+
if valid_request?
|
22
|
+
[ 200, { "Content-Type" => "image/png" }, [ body ] ]
|
23
|
+
else
|
24
|
+
[ 404, { "Content-Type" => "text/plain" }, [ "Not Found" ] ]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
protected
|
29
|
+
|
30
|
+
def valid_request?
|
31
|
+
payload.length > 0
|
32
|
+
end
|
33
|
+
|
34
|
+
def hashed_payload
|
35
|
+
@hashed_payload ||= Digest::SHA2.hexdigest(payload)
|
36
|
+
end
|
37
|
+
|
38
|
+
def payload
|
39
|
+
@payload ||= @request.path_info[1..-1].to_s.strip
|
40
|
+
end
|
41
|
+
|
42
|
+
def parse_color hex_str
|
43
|
+
hex_str.scan(/.{2}/).map { |c| c.to_i 16 }
|
44
|
+
end
|
45
|
+
|
46
|
+
def cache &blk
|
47
|
+
Rack::Identicon.cache cache_key, &blk
|
48
|
+
end
|
49
|
+
|
50
|
+
def cache_key
|
51
|
+
hashed_payload + "?s=#{@size}&b=#{@bg}"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require_relative 'lib/rack/identicon/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "rack-identicon"
|
5
|
+
spec.version = Rack::Identicon::VERSION
|
6
|
+
spec.authors = ["glaszig"]
|
7
|
+
spec.email = ["glaszig@gmail.com"]
|
8
|
+
|
9
|
+
spec.summary = %q{Generates Identicon for arbitrary data on the fly.}
|
10
|
+
spec.description = %q{Gives you a Rack middleware to generate pictures representing data, like gravatar does.}
|
11
|
+
spec.homepage = "https://github.com/glaszig/rack-identicon"
|
12
|
+
spec.license = "MIT"
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
14
|
+
|
15
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
16
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
17
|
+
|
18
|
+
# Specify which files should be added to the gem when it is released.
|
19
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
20
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
21
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
22
|
+
end
|
23
|
+
spec.bindir = "exe"
|
24
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
25
|
+
spec.require_paths = ["lib"]
|
26
|
+
|
27
|
+
spec.add_runtime_dependency 'identicon'
|
28
|
+
spec.add_development_dependency 'rack-test'
|
29
|
+
spec.add_development_dependency 'pry'
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-identicon
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- glaszig
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-07-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: identicon
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rack-test
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Gives you a Rack middleware to generate pictures representing data, like
|
56
|
+
gravatar does.
|
57
|
+
email:
|
58
|
+
- glaszig@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- config.ru
|
72
|
+
- lib/rack/identicon.rb
|
73
|
+
- lib/rack/identicon/caching.rb
|
74
|
+
- lib/rack/identicon/railtie.rb
|
75
|
+
- lib/rack/identicon/response.rb
|
76
|
+
- lib/rack/identicon/version.rb
|
77
|
+
- rack-identicon.gemspec
|
78
|
+
homepage: https://github.com/glaszig/rack-identicon
|
79
|
+
licenses:
|
80
|
+
- MIT
|
81
|
+
metadata:
|
82
|
+
homepage_uri: https://github.com/glaszig/rack-identicon
|
83
|
+
source_code_uri: https://github.com/glaszig/rack-identicon
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: 2.3.0
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 2.5.2.3
|
101
|
+
signing_key:
|
102
|
+
specification_version: 4
|
103
|
+
summary: Generates Identicon for arbitrary data on the fly.
|
104
|
+
test_files: []
|