hanko 0.1.0

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.
@@ -0,0 +1,15 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 2.1
5
+ - 2.2
6
+ - ruby-head
7
+ before_install: gem install bundler -v 1.10.6
8
+ gemfile:
9
+ - gemfiles/rails3.gemfile
10
+ - gemfiles/rails40.gemfile
11
+ - gemfiles/rails41.gemfile
12
+ - gemfiles/rails42.gemfile
13
+ notifications:
14
+ emails:
15
+ - tsukasa.oishi@gmail.com
@@ -0,0 +1,20 @@
1
+ appraise "rails3" do
2
+ gem "actionpack", "~> 3.2.0"
3
+ gem "railties", "~> 3.2.0"
4
+ gem "test-unit", "~> 3.0"
5
+ end
6
+
7
+ appraise "rails40" do
8
+ gem "rails", "~> 4.0.0"
9
+ gem "railties", "~> 4.0.0"
10
+ end
11
+
12
+ appraise "rails41" do
13
+ gem "rails", "~> 4.1.0"
14
+ gem "railties", "~> 4.1.0"
15
+ end
16
+
17
+ appraise "rails42" do
18
+ gem "rails", "~> 4.2.0"
19
+ gem "railties", "~> 4.2.0"
20
+ end
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hanko.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 tsukasaoishi
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,49 @@
1
+ # Hanko
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/hanko.svg)](http://badge.fury.io/rb/hanko) [![Build Status](https://travis-ci.org/tsukasaoishi/hanko.svg?branch=master)](https://travis-ci.org/tsukasaoishi/hanko) [![Code Climate](https://codeclimate.com/github/tsukasaoishi/hanko/badges/gpa.svg)](https://codeclimate.com/github/tsukasaoishi/hanko)
4
+
5
+ Hanko add a asset fingerprint in query string of Rails asset url(image, javascript, stylesheet, etc).
6
+ Like this, ```/images/kaeruspoon.jpg?1af6d6f2f682f76f80e606aeaaee1680```
7
+
8
+ Hanko would use when your rails app skipped Sprocket.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'hanko'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install hanko
25
+
26
+ ## Usage
27
+
28
+ The asset fingerprint of Hanko is Digest::SHA256 digest of asset file content.
29
+ You could use Digest::Base class the asset fingerprint.
30
+ For example ```config/application.rb```
31
+ ```ruby
32
+ config.hanko.digest_class = Digest::MD5
33
+ ```
34
+
35
+ ## Development
36
+
37
+ 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.
38
+
39
+ To install this gem onto your local machine, run `bundle exec appraisal 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).
40
+
41
+ ## Contributing
42
+
43
+ Bug reports and pull requests are welcome on GitHub at https://github.com/tsukasaoishi/hanko. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
44
+
45
+
46
+ ## License
47
+
48
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
49
+
@@ -0,0 +1,10 @@
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
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "hanko"
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
@@ -0,0 +1,8 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install --path .bundle
6
+ bundle exec appraisal install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,9 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "actionpack", "~> 3.2.0"
6
+ gem "railties", "~> 3.2.0"
7
+ gem "test-unit", "~> 3.0"
8
+
9
+ gemspec :path => "../"
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~> 4.0.0"
6
+ gem "railties", "~> 4.0.0"
7
+
8
+ gemspec :path => "../"
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~> 4.1.0"
6
+ gem "railties", "~> 4.1.0"
7
+
8
+ gemspec :path => "../"
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~> 4.2.0"
6
+ gem "railties", "~> 4.2.0"
7
+
8
+ gemspec :path => "../"
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hanko/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hanko"
8
+ spec.version = Hanko::VERSION
9
+ spec.authors = ["tsukasaoishi"]
10
+ spec.email = ["tsukasa.oishi@gmail.com"]
11
+
12
+ spec.summary = %q{Add digest fingerprint (SHA256, MD5 and so on) in query string of Rails asset url.}
13
+ spec.description = %q{Hanko add a asset fingerprint in query string of Rails asset url(image, javascript, stylesheet, etc).}
14
+ spec.homepage = "https://github.com/tsukasaoishi/hanko"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "actionpack", ">= 3.2", "< 4.3"
23
+
24
+ spec.add_development_dependency "railties", ">= 3.2", "< 4.3"
25
+ spec.add_development_dependency "bundler", '>= 1.3.0', '< 2.0'
26
+ spec.add_development_dependency "rake", '>= 0.8.7'
27
+ spec.add_development_dependency "minitest"
28
+ spec.add_development_dependency "minitest-reporters"
29
+ spec.add_development_dependency 'appraisal'
30
+ end
@@ -0,0 +1,4 @@
1
+ require "hanko/version"
2
+ require "hanko/fingerprint"
3
+ require 'hanko/init'
4
+ require "hanko/railtie" if defined?(Rails)
@@ -0,0 +1,9 @@
1
+ module Hanko
2
+ module AssetPaths
3
+ private
4
+
5
+ def rails_asset_id(source)
6
+ Hanko::Fingerprint.pon(config.assets_dir, source)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,48 @@
1
+ module Hanko
2
+ module AssetUrlHelper
3
+ URI_REGEXP = ::ActionView::Helpers::AssetUrlHelper::URI_REGEXP
4
+
5
+ def asset_path(source, options = {})
6
+ source = source.to_s
7
+ return "" unless source.present?
8
+ return source if source =~ URI_REGEXP
9
+
10
+ tail, source = source[/([\?#].+)$/], source.sub(/([\?#].+)$/, '')
11
+
12
+ if extname = compute_asset_extname(source, options)
13
+ source = "#{source}#{extname}"
14
+ end
15
+
16
+ if source[0] != ?/
17
+ # The patch is only here
18
+ source = compute_asset_path(source, options.merge(fingerprint: tail.nil?))
19
+ end
20
+
21
+ relative_url_root = defined?(config.relative_url_root) && config.relative_url_root
22
+ if relative_url_root
23
+ source = File.join(relative_url_root, source) unless source.starts_with?("#{relative_url_root}/")
24
+ end
25
+
26
+ if host = compute_asset_host(source, options)
27
+ source = File.join(host, source)
28
+ end
29
+
30
+ "#{source}#{tail}"
31
+ end
32
+ alias_method :path_to_asset, :asset_path
33
+
34
+ def compute_asset_path(source, options = {})
35
+ fingerprint = options.delete(:fingerprint)
36
+
37
+ source = super
38
+ return source unless fingerprint
39
+
40
+ asset_id = Hanko::Fingerprint.pon(config.assets_dir, source)
41
+ if asset_id.empty?
42
+ source
43
+ else
44
+ "#{source}?#{asset_id}"
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,17 @@
1
+ require 'digest/sha2'
2
+
3
+ module Hanko
4
+ class Digest
5
+ class << self
6
+ attr_writer :digest_class
7
+
8
+ def file_digest(path)
9
+ digest_class.file(path).hexdigest
10
+ end
11
+
12
+ def digest_class
13
+ @digest_class || ::Digest::SHA256
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,38 @@
1
+ require 'digest/sha2'
2
+
3
+ module Hanko
4
+ class Fingerprint
5
+ class << self
6
+ attr_writer :digest_class
7
+
8
+ def pon(assets_dir, source)
9
+ cached_pon[source] ||= generate_pon(assets_dir, source)
10
+ end
11
+
12
+ def digest_class=(klass)
13
+ @cached_pon = {}
14
+ @digest_class = klass
15
+ end
16
+
17
+ private
18
+
19
+ def cached_pon
20
+ @cached_pon ||= {}
21
+ end
22
+
23
+ def generate_pon(assets_dir, source)
24
+ path = File.join(assets_dir, source)
25
+ File.exist?(path) ? file_digest(path) : ''
26
+ end
27
+
28
+ def file_digest(path)
29
+ digest_class.file(path).hexdigest
30
+ end
31
+
32
+ def digest_class
33
+ @digest_class || ::Digest::SHA256
34
+ end
35
+ end
36
+ end
37
+ end
38
+
@@ -0,0 +1,35 @@
1
+ module Hanko
2
+ class Init
3
+ def initialize(digest_class = nil)
4
+ @digest_class = digest_class
5
+ end
6
+
7
+ def ready
8
+ Hanko::Fingerprint.digest_class = @digest_class if @digest_class
9
+
10
+ if exist_asset_paths?
11
+ prepend_asset_paths
12
+ else
13
+ prepend_asset_url_helper
14
+ end
15
+ end
16
+
17
+ def prepend_asset_url_helper
18
+ require "hanko/asset_url_helper"
19
+ ActionView::Base.__send__(
20
+ :prepend, Hanko::AssetUrlHelper
21
+ )
22
+ end
23
+
24
+ def prepend_asset_paths
25
+ require "hanko/asset_paths"
26
+ ActionView::Helpers::AssetTagHelper::AssetPaths.__send__(
27
+ :prepend, Hanko::AssetPaths
28
+ )
29
+ end
30
+
31
+ def exist_asset_paths?
32
+ defined?(ActionView::Helpers::AssetTagHelper::AssetPaths)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,14 @@
1
+ require "rails"
2
+
3
+ module Hanko
4
+ class Railtie < ::Rails::Railtie
5
+ config.hanko = ActiveSupport::OrderedOptions.new
6
+
7
+ initializer "hanko.load_action_view" do |app|
8
+ ActiveSupport.on_load(:action_view) do
9
+ Hanko::Init.new(app.config.hanko.digest_class).ready
10
+ end
11
+ end
12
+ end
13
+ end
14
+
@@ -0,0 +1,3 @@
1
+ module Hanko
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,187 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hanko
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - tsukasaoishi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-11-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: actionpack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '4.3'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '3.2'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '4.3'
33
+ - !ruby/object:Gem::Dependency
34
+ name: railties
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '3.2'
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: '4.3'
43
+ type: :development
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '3.2'
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: '4.3'
53
+ - !ruby/object:Gem::Dependency
54
+ name: bundler
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: 1.3.0
60
+ - - "<"
61
+ - !ruby/object:Gem::Version
62
+ version: '2.0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: 1.3.0
70
+ - - "<"
71
+ - !ruby/object:Gem::Version
72
+ version: '2.0'
73
+ - !ruby/object:Gem::Dependency
74
+ name: rake
75
+ requirement: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: 0.8.7
80
+ type: :development
81
+ prerelease: false
82
+ version_requirements: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: 0.8.7
87
+ - !ruby/object:Gem::Dependency
88
+ name: minitest
89
+ requirement: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ type: :development
95
+ prerelease: false
96
+ version_requirements: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ - !ruby/object:Gem::Dependency
102
+ name: minitest-reporters
103
+ requirement: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ type: :development
109
+ prerelease: false
110
+ version_requirements: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ - !ruby/object:Gem::Dependency
116
+ name: appraisal
117
+ requirement: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ type: :development
123
+ prerelease: false
124
+ version_requirements: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ description: Hanko add a asset fingerprint in query string of Rails asset url(image,
130
+ javascript, stylesheet, etc).
131
+ email:
132
+ - tsukasa.oishi@gmail.com
133
+ executables: []
134
+ extensions: []
135
+ extra_rdoc_files: []
136
+ files:
137
+ - ".codeclimate.yml"
138
+ - ".gitignore"
139
+ - ".rubocop.yml"
140
+ - ".travis.yml"
141
+ - Appraisals
142
+ - CODE_OF_CONDUCT.md
143
+ - Gemfile
144
+ - LICENSE.txt
145
+ - README.md
146
+ - Rakefile
147
+ - bin/console
148
+ - bin/setup
149
+ - gemfiles/rails3.gemfile
150
+ - gemfiles/rails40.gemfile
151
+ - gemfiles/rails41.gemfile
152
+ - gemfiles/rails42.gemfile
153
+ - hanko.gemspec
154
+ - lib/hanko.rb
155
+ - lib/hanko/asset_paths.rb
156
+ - lib/hanko/asset_url_helper.rb
157
+ - lib/hanko/digest.rb
158
+ - lib/hanko/fingerprint.rb
159
+ - lib/hanko/init.rb
160
+ - lib/hanko/railtie.rb
161
+ - lib/hanko/version.rb
162
+ homepage: https://github.com/tsukasaoishi/hanko
163
+ licenses:
164
+ - MIT
165
+ metadata: {}
166
+ post_install_message:
167
+ rdoc_options: []
168
+ require_paths:
169
+ - lib
170
+ required_ruby_version: !ruby/object:Gem::Requirement
171
+ requirements:
172
+ - - ">="
173
+ - !ruby/object:Gem::Version
174
+ version: '0'
175
+ required_rubygems_version: !ruby/object:Gem::Requirement
176
+ requirements:
177
+ - - ">="
178
+ - !ruby/object:Gem::Version
179
+ version: '0'
180
+ requirements: []
181
+ rubyforge_project:
182
+ rubygems_version: 2.4.5.1
183
+ signing_key:
184
+ specification_version: 4
185
+ summary: Add digest fingerprint (SHA256, MD5 and so on) in query string of Rails asset
186
+ url.
187
+ test_files: []