dassets 0.15.0 → 0.15.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 51a6c8bbf4e5b1a6cebb52c6da1ce5a6eae5714cf8cd410d2aa06a79737fc9b5
4
- data.tar.gz: f97b79e8148698de5d6a1cd97419e34b416a1a03c4d9773e742614403fd9180d
3
+ metadata.gz: 8d2158827f5cdbb5f779c9c3647a0f358bec9bffa62f5fefbf8b2b83f654d21a
4
+ data.tar.gz: 84e0a6194cb0e506431750155dea71e3cc7cc0c62ac68a1ce8155454a5348f84
5
5
  SHA512:
6
- metadata.gz: a8310a78cfbbcb27795f0319303f1471625edb03e10a747e52c195a85f0b791adc41ed52a45aa4756525d67160bcbc50d788a45076f52f60513bd51ac5b63932
7
- data.tar.gz: f01e81b5758d052dcf8b5e602eb1798e206ff1cb5c9ab8d80dfaf90c83841ce278e83e8dd3577f976c37fd05a073b66ff7cd57e27d3c11bfff092a1fdb93a967
6
+ metadata.gz: b05667686c621b41c5cd0bd80242a58b74f58da3867180b039130e1898b7fcd8f49ee010c3007ad49e19d3cc748aa363c4f90170be6132797ddfb7c522e8a05b
7
+ data.tar.gz: 1827b4f16b9f9d786306c8aef467a63b040830931b3481affe7af2954e7df11ebe5557cabe016b4d1ed0375923e18b9aa8c7906d8e55dae859f205d9e9bb2c34
data/Gemfile CHANGED
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source "https://rubygems.org"
2
4
 
3
5
  ruby "~> 2.5"
4
6
 
5
7
  gemspec
6
8
 
7
- gem "pry", "~> 0.12.2"
9
+ gem "pry"
@@ -1,4 +1,6 @@
1
1
  # -*- encoding: utf-8 -*-
2
+ # frozen_string_literal: true
3
+
2
4
  lib = File.expand_path("../lib", __FILE__)
3
5
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
6
  require "dassets/version"
@@ -8,20 +10,22 @@ Gem::Specification.new do |gem|
8
10
  gem.version = Dassets::VERSION
9
11
  gem.authors = ["Kelly Redding", "Collin Redding"]
10
12
  gem.email = ["kelly@kellyredding.com", "collin.redding@me.com"]
11
- gem.summary = %q{Digested asset files}
12
- gem.description = %q{Digest and serve HTML asset files}
13
+ gem.summary = "Digested asset files"
14
+ gem.description = "Digest and serve HTML asset files"
13
15
  gem.homepage = "http://github.com/redding/dassets"
14
16
  gem.license = "MIT"
15
17
 
16
- gem.files = `git ls-files`.split($/)
18
+ gem.files = `git ls-files | grep "^[^.]"`.split($INPUT_RECORD_SEPARATOR)
19
+
17
20
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
21
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
22
  gem.require_paths = ["lib"]
20
23
 
21
24
  gem.required_ruby_version = "~> 2.5"
22
25
 
23
- gem.add_development_dependency("assert", ["~> 2.19.0"])
24
- gem.add_development_dependency("assert-rack-test", ["~> 1.1.0"])
26
+ gem.add_development_dependency("much-style-guide", ["~> 0.6.0"])
27
+ gem.add_development_dependency("assert", ["~> 2.19.3"])
28
+ gem.add_development_dependency("assert-rack-test", ["~> 1.1.1"])
25
29
  gem.add_development_dependency("sinatra", ["~> 2.1"])
26
30
 
27
31
  gem.add_dependency("rack", ["~> 2.1"])
@@ -13,17 +13,17 @@ module Dassets
13
13
  end
14
14
 
15
15
  def self.configure(&block)
16
- block.call(self.config)
16
+ block.call(config)
17
17
  end
18
18
 
19
19
  def self.init
20
20
  @asset_files ||= {}
21
- @source_files = SourceFiles.new(self.config.sources)
21
+ @source_files = SourceFiles.new(config.sources)
22
22
  end
23
23
 
24
24
  def self.reset
25
25
  @asset_files = {}
26
- self.config.reset
26
+ config.reset
27
27
  end
28
28
 
29
29
  def self.asset_file(digest_path)
@@ -31,7 +31,7 @@ module Dassets
31
31
  end
32
32
 
33
33
  def self.[](digest_path)
34
- self.asset_file(digest_path).tap do |af|
34
+ asset_file(digest_path).tap do |af|
35
35
  if af.fingerprint.nil?
36
36
  msg =
37
37
  +"error digesting `#{digest_path}`.\n\nMake sure Dassets has " \
@@ -47,7 +47,7 @@ module Dassets
47
47
  values = Dassets.combinations[key].sort
48
48
  msg << (
49
49
  ["#{bullet}#{values.first}"] +
50
- (values[1..-1] || []).map{ |v| "#{" "*bullet.size}#{v}" }
50
+ (values[1..-1] || []).map{ |v| "#{" " * bullet.size}#{v}" }
51
51
  ).join("\n")
52
52
  msg << "\n\n"
53
53
  end
@@ -66,7 +66,7 @@ module Dassets
66
66
  end
67
67
 
68
68
  def self.combinations
69
- self.config.combinations
69
+ config.combinations
70
70
  end
71
71
 
72
72
  module SourceFiles
@@ -5,6 +5,7 @@ require "rack/utils"
5
5
  require "rack/mime"
6
6
 
7
7
  module Dassets; end
8
+
8
9
  class Dassets::AssetFile
9
10
  attr_reader :digest_path, :dirname, :extname, :basename, :source_proxy
10
11
 
@@ -22,40 +23,40 @@ class Dassets::AssetFile
22
23
  end
23
24
 
24
25
  def digest!
25
- return if !self.exists?
26
- Dassets.config.file_store.save(self.url){ self.content }
26
+ return unless exists?
27
+ Dassets.config.file_store.save(url){ content }
27
28
  end
28
29
 
29
30
  def url
30
- path_basename = "#{@basename}-#{self.fingerprint}#{@extname}"
31
+ path_basename = "#{@basename}-#{fingerprint}#{@extname}"
31
32
  path =
32
- File.join(@dirname, path_basename).sub(/^\.\//, "").sub(/^\//, "")
33
+ File.join(@dirname, path_basename).sub(%r{^\./}, "").sub(%r{^/}, "")
33
34
  "#{dassets_base_url}/#{path}"
34
35
  end
35
36
  alias_method :href, :url
36
37
 
37
38
  def fingerprint
38
- return nil if !self.exists?
39
+ return nil unless exists?
39
40
  @source_proxy.fingerprint
40
41
  end
41
42
 
42
43
  def content
43
- return nil if !self.exists?
44
+ return nil unless exists?
44
45
  @source_proxy.content
45
46
  end
46
47
 
47
48
  def mtime
48
- return nil if !self.exists?
49
+ return nil unless exists?
49
50
  @source_proxy.mtime.httpdate
50
51
  end
51
52
 
52
53
  def size
53
- return nil if !self.exists?
54
- self.content.bytesize
54
+ return nil unless exists?
55
+ content.bytesize
55
56
  end
56
57
 
57
58
  def mime_type
58
- return nil if !self.exists?
59
+ return nil unless exists?
59
60
  Rack::Mime.mime_type(@extname)
60
61
  end
61
62
 
@@ -67,10 +68,10 @@ class Dassets::AssetFile
67
68
  @source_proxy.exists?
68
69
  end
69
70
 
70
- def ==(other_asset_file)
71
- other_asset_file.kind_of?(Dassets::AssetFile) &&
72
- self.digest_path == other_asset_file.digest_path &&
73
- self.fingerprint == other_asset_file.fingerprint
71
+ def ==(other)
72
+ other.is_a?(Dassets::AssetFile) &&
73
+ digest_path == other.digest_path &&
74
+ fingerprint == other.fingerprint
74
75
  end
75
76
 
76
77
  private
@@ -6,12 +6,13 @@ require "dassets/file_store"
6
6
  require "dassets/source"
7
7
 
8
8
  module Dassets; end
9
+
9
10
  class Dassets::Config
10
11
  attr_reader :sources, :combinations
11
12
 
12
13
  def initialize
13
14
  super
14
- self.reset
15
+ reset
15
16
 
16
17
  @content_cache = Dassets::NoCache.new
17
18
  @fingerprint_cache = Dassets::NoCache.new
@@ -20,12 +21,12 @@ class Dassets::Config
20
21
 
21
22
  def reset
22
23
  @sources = []
23
- @combinations = Hash.new { |h, k| [k] } # digest pass-thru if none defined
24
+ @combinations = Hash.new{ |_h, k| [k] } # digest pass-thru if none defined
24
25
  @file_store = Dassets::NullFileStore.new
25
26
  end
26
27
 
27
28
  def base_url(value = nil)
28
- set_base_url(value) if !value.nil?
29
+ set_base_url(value) unless value.nil?
29
30
  @base_url
30
31
  end
31
32
 
@@ -34,9 +35,9 @@ class Dassets::Config
34
35
  end
35
36
 
36
37
  def file_store(value = nil)
37
- if !value.nil?
38
+ unless value.nil?
38
39
  @file_store =
39
- if value.kind_of?(Dassets::FileStore)
40
+ if value.is_a?(Dassets::FileStore)
40
41
  value
41
42
  else
42
43
  Dassets::FileStore.new(value)
@@ -46,12 +47,12 @@ class Dassets::Config
46
47
  end
47
48
 
48
49
  def content_cache(cache = nil)
49
- @content_cache = cache if !cache.nil?
50
+ @content_cache = cache unless cache.nil?
50
51
  @content_cache
51
52
  end
52
53
 
53
54
  def fingerprint_cache(cache = nil)
54
- @fingerprint_cache = cache if !cache.nil?
55
+ @fingerprint_cache = cache unless cache.nil?
55
56
  @fingerprint_cache
56
57
  end
57
58
 
@@ -14,10 +14,10 @@ class Dassets::FileStore
14
14
 
15
15
  def save(url_path, &block)
16
16
  @save_mutex.synchronize do
17
- store_path(url_path).tap { |path|
17
+ store_path(url_path).tap do |path|
18
18
  FileUtils.mkdir_p(File.dirname(path))
19
- File.open(path, "w") { |f| f.write(block.call) }
20
- }
19
+ File.open(path, "w"){ |f| f.write(block.call) }
20
+ end
21
21
  end
22
22
  end
23
23
 
@@ -31,7 +31,7 @@ class Dassets::NullFileStore < Dassets::FileStore
31
31
  super("")
32
32
  end
33
33
 
34
- def save(url_path, &block)
34
+ def save(url_path)
35
35
  # No-op, just return the store path like the base does.
36
36
  store_path(url_path)
37
37
  end
@@ -6,6 +6,7 @@ require "dassets/server/response"
6
6
  # Rack middleware for serving Dassets asset files
7
7
 
8
8
  module Dassets; end
9
+
9
10
  class Dassets::Server
10
11
  def initialize(app)
11
12
  @app = app
@@ -4,6 +4,7 @@ require "rack"
4
4
 
5
5
  module Dassets; end
6
6
  class Dassets::Server; end
7
+
7
8
  class Dassets::Server::Request < Rack::Request
8
9
  # The HTTP request method. This is the standard implementation of this
9
10
  # method but is respecified here due to libraries that attempt to modify
@@ -46,7 +47,7 @@ class Dassets::Server::Request < Rack::Request
46
47
 
47
48
  def path_digest_match
48
49
  @path_digest_match ||= begin
49
- path_info.match(/\/(.+)-[a-f0-9]{32}(\..+|)$/i) || NullDigestMatch.new
50
+ path_info.match(%r{/(.+)-[a-f0-9]{32}(\..+|)$}i) || NullDigestMatch.new
50
51
  end
51
52
  end
52
53
 
@@ -6,6 +6,7 @@ require "rack/mime"
6
6
 
7
7
  module Dassets; end
8
8
  class Dassets::Server; end
9
+
9
10
  class Dassets::Server::Response
10
11
  attr_reader :asset_file, :status, :headers, :body
11
12
 
@@ -30,13 +31,15 @@ class Dassets::Server::Response
30
31
  body = Body.new(env, @asset_file)
31
32
  [
32
33
  body.partial? ? 206 : 200,
33
- Rack::Utils::HeaderHash.new.merge(@asset_file.response_headers).tap do |h|
34
- h["Last-Modified"] = mtime.to_s
35
- h["Content-Type"] = @asset_file.mime_type.to_s
36
- h["Content-Length"] = body.size.to_s
37
- h["Content-Range"] = body.content_range if body.partial?
38
- end,
39
- env["REQUEST_METHOD"] == "HEAD" ? [] : body
34
+ Rack::Utils::HeaderHash
35
+ .new
36
+ .merge(@asset_file.response_headers).tap do |h|
37
+ h["Last-Modified"] = mtime.to_s
38
+ h["Content-Type"] = @asset_file.mime_type.to_s
39
+ h["Content-Length"] = body.size.to_s
40
+ h["Content-Range"] = body.content_range if body.partial?
41
+ end,
42
+ env["REQUEST_METHOD"] == "HEAD" ? [] : body,
40
43
  ]
41
44
  end
42
45
  end
@@ -48,14 +51,14 @@ class Dassets::Server::Response
48
51
  # This class borrows from the body range handling in Rack::File and adapts
49
52
  # it for use with Dasset's asset files and their generic string content.
50
53
  class Body
51
- CHUNK_SIZE = (8*1024).freeze # 8k
54
+ CHUNK_SIZE = (8 * 1024) # 8k
52
55
 
53
56
  attr_reader :asset_file, :size, :content_range
54
57
 
55
58
  def initialize(env, asset_file)
56
59
  @asset_file = asset_file
57
60
  @range, @content_range = get_range_info(env, @asset_file)
58
- @size = self.range_end - self.range_begin + 1
61
+ @size = range_end - range_begin + 1
59
62
  end
60
63
 
61
64
  def partial?
@@ -73,7 +76,7 @@ class Dassets::Server::Response
73
76
  def each
74
77
  StringIO.open(@asset_file.content, "rb") do |io|
75
78
  io.seek(@range.begin)
76
- remaining_len = self.size
79
+ remaining_len = size
77
80
  while remaining_len > 0
78
81
  part = io.read([CHUNK_SIZE, remaining_len].min)
79
82
  break if part.nil?
@@ -85,16 +88,16 @@ class Dassets::Server::Response
85
88
  end
86
89
 
87
90
  def inspect
88
- "#<#{self.class}:#{"0x0%x" % (self.object_id << 1)} " \
89
- "digest_path=#{self.asset_file.digest_path} " \
90
- "range_begin=#{self.range_begin} range_end=#{self.range_end}>"
91
+ "#<#{self.class}:#{"0x0%x" % (object_id << 1)} " \
92
+ "digest_path=#{asset_file.digest_path} " \
93
+ "range_begin=#{range_begin} range_end=#{range_end}>"
91
94
  end
92
95
 
93
- def ==(other_body)
94
- if other_body.is_a?(self.class)
95
- self.asset_file == other_body.asset_file &&
96
- self.range_begin == other_body.range_begin &&
97
- self.range_end == other_body.range_end
96
+ def ==(other)
97
+ if other.is_a?(self.class)
98
+ asset_file == other.asset_file &&
99
+ range_begin == other.range_begin &&
100
+ range_end == other.range_end
98
101
  else
99
102
  super
100
103
  end
@@ -105,16 +108,22 @@ class Dassets::Server::Response
105
108
  def get_range_info(env, asset_file)
106
109
  content_size = asset_file.size
107
110
  # legacy rack version, just return full size
108
- return full_size_range_info(content_size) if !Rack::Utils.respond_to?(:byte_ranges)
111
+ unless Rack::Utils.respond_to?(:byte_ranges)
112
+ return full_size_range_info(content_size)
113
+ end
114
+
109
115
  ranges = Rack::Utils.byte_ranges(env, content_size)
110
116
  # No ranges or multiple ranges are not supported, just return full size
111
- return full_size_range_info(content_size) if ranges.nil? || ranges.empty? || ranges.length > 1
117
+ if ranges.nil? || ranges.empty? || ranges.length > 1
118
+ return full_size_range_info(content_size)
119
+ end
120
+
112
121
  # single range
113
122
  [ranges[0], "bytes #{ranges[0].begin}-#{ranges[0].end}/#{content_size}"]
114
123
  end
115
124
 
116
125
  def full_size_range_info(content_size)
117
- [(0..content_size-1), nil]
126
+ [(0..content_size - 1), nil]
118
127
  end
119
128
  end
120
129
  end
@@ -3,6 +3,7 @@
3
3
  require "dassets/engine"
4
4
 
5
5
  module Dassets; end
6
+
6
7
  class Dassets::Source
7
8
  attr_reader :path, :engines, :response_headers
8
9
 
@@ -10,7 +11,7 @@ class Dassets::Source
10
11
  @path = path.to_s
11
12
  @filter = proc{ |paths| paths }
12
13
  @engines = Hash.new{ |hash, key| hash[key] = [] }
13
- @response_headers = Hash.new
14
+ @response_headers = {}
14
15
  end
15
16
 
16
17
  def filter(&block)
@@ -6,6 +6,7 @@ require "dassets/asset_file"
6
6
  require "dassets/source_proxy"
7
7
 
8
8
  module Dassets; end
9
+
9
10
  class Dassets::SourceFile
10
11
  def self.find_by_digest_path(path, **options)
11
12
  Dassets.source_files[path] || Dassets::NullSourceFile.new(path, **options)
@@ -23,13 +24,13 @@ class Dassets::SourceFile
23
24
  # configured source) in `find_by_digest_path` above.
24
25
  def source
25
26
  @source ||=
26
- Dassets.config.sources.select { |source|
27
+ Dassets.config.sources.select{ |source|
27
28
  @file_path =~ /^#{slash_path(source.path)}/
28
29
  }.last
29
30
  end
30
31
 
31
32
  def asset_file
32
- @asset_file ||= Dassets::AssetFile.new(self.digest_path)
33
+ @asset_file ||= Dassets::AssetFile.new(digest_path)
33
34
  end
34
35
 
35
36
  def digest_path
@@ -37,26 +38,28 @@ class Dassets::SourceFile
37
38
  begin
38
39
  digest_basename =
39
40
  @ext_list
40
- .reduce([]) { |digest_ext_list, ext|
41
+ .reduce([]){ |digest_ext_list, ext|
41
42
  digest_ext_list <<
42
- self.source.engines[ext].reduce(ext) { |ext_acc, engine|
43
+ source.engines[ext].reduce(ext)do |ext_acc, engine|
43
44
  engine.ext(ext_acc)
44
- }
45
+ end
45
46
  }
46
47
  .reject(&:empty?)
47
48
  .reverse
48
49
  .join(".")
49
50
 
50
- File.join([digest_dirname(@file_path), digest_basename].reject(&:empty?))
51
+ File.join(
52
+ [digest_dirname(@file_path), digest_basename].reject(&:empty?),
53
+ )
51
54
  end
52
55
  end
53
56
 
54
57
  def compiled
55
- @ext_list.reduce(read_file(@file_path)) { |file_acc, ext|
56
- self.source.engines[ext].reduce(file_acc) { |ext_acc, engine|
58
+ @ext_list.reduce(read_file(@file_path)) do |file_acc, ext|
59
+ source.engines[ext].reduce(file_acc) do |ext_acc, engine|
57
60
  engine.compile(ext_acc)
58
- }
59
- }
61
+ end
62
+ end
60
63
  end
61
64
 
62
65
  def exists?
@@ -68,12 +71,12 @@ class Dassets::SourceFile
68
71
  end
69
72
 
70
73
  def response_headers
71
- self.source.nil? ? Hash.new : self.source.response_headers
74
+ source.nil? ? {} : source.response_headers
72
75
  end
73
76
 
74
- def ==(other_source_file)
75
- if other_source_file.is_a?(self.class)
76
- self.file_path == other_source_file.file_path
77
+ def ==(other)
78
+ if other.is_a?(self.class)
79
+ file_path == other.file_path
77
80
  else
78
81
  super
79
82
  end
@@ -83,7 +86,7 @@ class Dassets::SourceFile
83
86
 
84
87
  # remove the source path from the dirname (if it exists)
85
88
  def digest_dirname(file_path)
86
- slash_path(File.dirname(file_path)).sub(slash_path(self.source.path), "")
89
+ slash_path(File.dirname(file_path)).sub(slash_path(source.path), "")
87
90
  end
88
91
 
89
92
  def slash_path(path)
@@ -122,9 +125,9 @@ class Dassets::NullSourceFile < Dassets::SourceFile
122
125
  @source_proxy.mtime
123
126
  end
124
127
 
125
- def ==(other_source_file)
126
- if other_source_file.is_a?(self.class)
127
- self.file_path == other_source_file.file_path
128
+ def ==(other)
129
+ if other.is_a?(self.class)
130
+ file_path == other.file_path
128
131
  else
129
132
  super
130
133
  end