rack-identicon 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ Gemfile.lock
@@ -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
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rack-identicon.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "minitest", "~> 5.0"
@@ -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.
@@ -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).
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ t.warning = false
9
+ end
10
+
11
+ task :default => :test
@@ -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__)
@@ -0,0 +1,8 @@
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
@@ -0,0 +1,4 @@
1
+ require "rack/identicon"
2
+
3
+ map("/__identicon__") { run Rack::Identicon.new }
4
+ run lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['OK']] }
@@ -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,9 @@
1
+ module Rack
2
+ module Identicon
3
+ class Railtie < Rails::Railtie
4
+ initializer "rack_identicon.caching" do |app|
5
+ Rack::Identicon.cache_store = Rails.cache
6
+ end
7
+ end
8
+ end
9
+ 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,5 @@
1
+ module Rack
2
+ module Identicon
3
+ VERSION = "1.0.0"
4
+ end
5
+ 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: []