heroku_rails_deflate 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,8 +1,8 @@
1
1
  source "http://rubygems.org"
2
2
 
3
3
  gem 'rack', '~> 1.4.5'
4
- gem 'actionpack', '~> 3.2.13'
5
- gem 'activesupport', '~> 3.2.13'
4
+ gem 'actionpack', '>= 3.2.13'
5
+ gem 'activesupport', '>= 3.2.13'
6
6
 
7
7
  group :development do
8
8
  gem "rspec"
@@ -1,9 +1,9 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
- actionpack (3.2.13)
5
- activemodel (= 3.2.13)
6
- activesupport (= 3.2.13)
4
+ actionpack (3.2.16)
5
+ activemodel (= 3.2.16)
6
+ activesupport (= 3.2.16)
7
7
  builder (~> 3.0.0)
8
8
  erubis (~> 2.7.0)
9
9
  journey (~> 1.0.4)
@@ -11,19 +11,19 @@ GEM
11
11
  rack-cache (~> 1.2)
12
12
  rack-test (~> 0.6.1)
13
13
  sprockets (~> 2.2.1)
14
- activemodel (3.2.13)
15
- activesupport (= 3.2.13)
14
+ activemodel (3.2.16)
15
+ activesupport (= 3.2.16)
16
16
  builder (~> 3.0.0)
17
- activesupport (3.2.13)
18
- i18n (= 0.6.1)
17
+ activesupport (3.2.16)
18
+ i18n (~> 0.6, >= 0.6.4)
19
19
  multi_json (~> 1.0)
20
20
  addressable (2.3.5)
21
21
  builder (3.0.4)
22
- diff-lcs (1.2.2)
22
+ diff-lcs (1.2.5)
23
23
  erubis (2.7.0)
24
24
  faraday (0.8.8)
25
25
  multipart-post (~> 1.2.0)
26
- git (1.2.5)
26
+ git (1.2.6)
27
27
  github_api (0.10.1)
28
28
  addressable
29
29
  faraday (~> 0.8.1)
@@ -32,11 +32,11 @@ GEM
32
32
  nokogiri (~> 1.5.2)
33
33
  oauth2
34
34
  hashie (2.0.5)
35
- highline (1.6.19)
36
- hike (1.2.1)
35
+ highline (1.6.20)
36
+ hike (1.2.3)
37
37
  httpauth (0.2.0)
38
- i18n (0.6.1)
39
- jeweler (1.8.6)
38
+ i18n (0.6.9)
39
+ jeweler (1.8.8)
40
40
  builder
41
41
  bundler (~> 1.0)
42
42
  git (>= 1.2.5)
@@ -46,11 +46,11 @@ GEM
46
46
  rake
47
47
  rdoc
48
48
  journey (1.0.4)
49
- json (1.8.0)
49
+ json (1.8.1)
50
50
  jwt (0.1.8)
51
51
  multi_json (>= 1.5)
52
- multi_json (1.7.2)
53
- multi_xml (0.5.4)
52
+ multi_json (1.8.2)
53
+ multi_xml (0.5.5)
54
54
  multipart-post (1.2.0)
55
55
  nokogiri (1.5.10)
56
56
  oauth2 (0.9.2)
@@ -68,27 +68,27 @@ GEM
68
68
  rake (10.1.0)
69
69
  rdoc (4.0.1)
70
70
  json (~> 1.4)
71
- rspec (2.13.0)
72
- rspec-core (~> 2.13.0)
73
- rspec-expectations (~> 2.13.0)
74
- rspec-mocks (~> 2.13.0)
75
- rspec-core (2.13.1)
76
- rspec-expectations (2.13.0)
71
+ rspec (2.14.1)
72
+ rspec-core (~> 2.14.0)
73
+ rspec-expectations (~> 2.14.0)
74
+ rspec-mocks (~> 2.14.0)
75
+ rspec-core (2.14.7)
76
+ rspec-expectations (2.14.4)
77
77
  diff-lcs (>= 1.1.3, < 2.0)
78
- rspec-mocks (2.13.0)
78
+ rspec-mocks (2.14.4)
79
79
  sprockets (2.2.2)
80
80
  hike (~> 1.2)
81
81
  multi_json (~> 1.0)
82
82
  rack (~> 1.0)
83
83
  tilt (~> 1.1, != 1.3.0)
84
- tilt (1.3.6)
84
+ tilt (1.4.1)
85
85
 
86
86
  PLATFORMS
87
87
  ruby
88
88
 
89
89
  DEPENDENCIES
90
- actionpack (~> 3.2.13)
91
- activesupport (~> 3.2.13)
90
+ actionpack (>= 3.2.13)
91
+ activesupport (>= 3.2.13)
92
92
  bundler
93
93
  jeweler
94
94
  rack (~> 1.4.5)
data/README.md CHANGED
@@ -55,7 +55,8 @@ part. After you get done with this, you could take it to the next level by addin
55
55
 
56
56
  ## Thanks
57
57
 
58
- This gem expands on code originally published in a [gist](https://gist.github.com/guyboltonking/2152663) by [guyboltonking](https://github.com/guyboltonking).
58
+ This gem uses a combination of techniques from this [gist](https://gist.github.com/guyboltonking/2152663) by [guyboltonking](https://github.com/guyboltonking) and
59
+ [romanbsd](https://github.com/romanbsd)'s gem [heroku-deflater](https://github.com/romanbsd/heroku-deflater).
59
60
 
60
61
  ## Copyright
61
62
 
data/Rakefile CHANGED
@@ -36,3 +36,7 @@ Rake::RDocTask.new do |rdoc|
36
36
  rdoc.rdoc_files.include('README*')
37
37
  rdoc.rdoc_files.include('lib/**/*.rb')
38
38
  end
39
+
40
+ require 'rspec/core/rake_task'
41
+ RSpec::Core::RakeTask.new(:spec)
42
+ task default: [:spec]
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "heroku_rails_deflate"
8
- s.version = "1.0.1"
8
+ s.version = "1.0.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Matt Olson"]
12
- s.date = "2013-08-02"
12
+ s.date = "2014-02-13"
13
13
  s.description = "This gem is designed for use by Rails applications running on Heroku. For others, the better approach is to use a frontend server such as nginx or Apache. However, the Heroku Cedar stack is no longer fronted by a file server, and there is no automatic provision for gzipping responses. This gem activates Rack::Deflate for all requests. In addition, we serve up the gzipped versions of our precompiled assets, taking advantage of the higher compression ratio used during precompilation, and reducing CPU load at request time."
14
14
  s.email = "matt@mattolson.com"
15
15
  s.extra_rdoc_files = [
@@ -45,23 +45,23 @@ Gem::Specification.new do |s|
45
45
 
46
46
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
47
47
  s.add_runtime_dependency(%q<rack>, ["~> 1.4.5"])
48
- s.add_runtime_dependency(%q<actionpack>, ["~> 3.2.13"])
49
- s.add_runtime_dependency(%q<activesupport>, ["~> 3.2.13"])
48
+ s.add_runtime_dependency(%q<actionpack>, [">= 3.2.13"])
49
+ s.add_runtime_dependency(%q<activesupport>, [">= 3.2.13"])
50
50
  s.add_development_dependency(%q<rspec>, [">= 0"])
51
51
  s.add_development_dependency(%q<bundler>, [">= 0"])
52
52
  s.add_development_dependency(%q<jeweler>, [">= 0"])
53
53
  else
54
54
  s.add_dependency(%q<rack>, ["~> 1.4.5"])
55
- s.add_dependency(%q<actionpack>, ["~> 3.2.13"])
56
- s.add_dependency(%q<activesupport>, ["~> 3.2.13"])
55
+ s.add_dependency(%q<actionpack>, [">= 3.2.13"])
56
+ s.add_dependency(%q<activesupport>, [">= 3.2.13"])
57
57
  s.add_dependency(%q<rspec>, [">= 0"])
58
58
  s.add_dependency(%q<bundler>, [">= 0"])
59
59
  s.add_dependency(%q<jeweler>, [">= 0"])
60
60
  end
61
61
  else
62
62
  s.add_dependency(%q<rack>, ["~> 1.4.5"])
63
- s.add_dependency(%q<actionpack>, ["~> 3.2.13"])
64
- s.add_dependency(%q<activesupport>, ["~> 3.2.13"])
63
+ s.add_dependency(%q<actionpack>, [">= 3.2.13"])
64
+ s.add_dependency(%q<activesupport>, [">= 3.2.13"])
65
65
  s.add_dependency(%q<rspec>, [">= 0"])
66
66
  s.add_dependency(%q<bundler>, [">= 0"])
67
67
  s.add_dependency(%q<jeweler>, [">= 0"])
@@ -7,13 +7,20 @@ module HerokuRailsDeflate
7
7
  initializer "heroku_rails_deflate.middleware_initialization", :after => :load_config_initializers do |app|
8
8
  # Put Rack::Deflater in the right place
9
9
  if app.config.action_controller.perform_caching && app.config.action_dispatch.rack_cache
10
+ # If Rack::Cache is enabled, make sure we are caching compressed files
10
11
  app.config.middleware.insert_after 'Rack::Cache', 'Rack::Deflater'
11
12
  else
13
+ # Make sure we compress after retrieving static files
12
14
  app.config.middleware.insert_before 'ActionDispatch::Static', 'Rack::Deflater'
13
15
  end
14
16
 
15
- # Insert our custom middleware for serving gzipped static assets
16
- app.config.middleware.insert_after 'Rack::Deflater', 'HerokuRailsDeflate::ServeZippedAssets', app.paths["public"].first, app.config.assets.prefix, app.config.static_cache_control
17
+ # Insert our middleware for serving gzipped static assets. If we serve a compressed
18
+ # version, we tell deflater to skip it.
19
+ app.config.middleware.insert_after 'Rack::Deflater',
20
+ 'HerokuRailsDeflate::ServeZippedAssets',
21
+ app.paths["public"].first,
22
+ app.config.assets.prefix,
23
+ app.config.static_cache_control
17
24
  end
18
25
 
19
26
  # Set default Cache-Control headers to 365 days. Override in config/application.rb.
@@ -2,31 +2,37 @@ require 'action_controller'
2
2
  require 'active_support/core_ext/uri'
3
3
  require 'action_dispatch/middleware/static'
4
4
 
5
+ # Middleware to serve the gzipped version of static assets if they exist
5
6
  # Adapted from https://gist.github.com/guyboltonking/2152663
6
7
  module HerokuRailsDeflate
7
8
  class ServeZippedAssets
8
- def initialize(app, root, assets_path, cache_control=nil)
9
+ # Params:
10
+ # root: the public directory
11
+ # asset_prefix: config.assets.prefix
12
+ # cache_control: config.static_cache_control
13
+ def initialize(app, root, asset_prefix, cache_control=nil)
9
14
  @app = app
10
- @assets_path = assets_path.chomp('/') + '/'
15
+ @asset_prefix = asset_prefix.chomp('/') + '/'
11
16
  @file_handler = ActionDispatch::FileHandler.new(root, cache_control)
12
17
  end
13
18
 
14
19
  def call(env)
20
+ # Only process get requests
15
21
  if env['REQUEST_METHOD'] == 'GET'
16
22
  request = Rack::Request.new(env)
17
- encoding = Rack::Utils.select_best_encoding(%w(gzip identity), request.accept_encoding)
18
23
 
19
- if encoding == 'gzip'
20
- # See if gzipped version exists in assets directory
24
+ # See if client accepts gzip encoding
25
+ if Rack::Utils.select_best_encoding(%w(gzip identity), request.accept_encoding) == 'gzip'
26
+ # Check if compressed version exists in assets directory
21
27
  compressed_path = env['PATH_INFO'] + '.gz'
22
- if compressed_path.start_with?(@assets_path) && (match = @file_handler.match?(compressed_path))
23
- # Get the FileHandler to serve up the gzipped file, then strip the .gz suffix
24
- env["PATH_INFO"] = match
28
+ if compressed_path.start_with?(@asset_prefix) && (match = @file_handler.match?(compressed_path))
29
+ # Use FileHandler to serve up the gzipped file, then strip the .gz suffix
30
+ path = env["PATH_INFO"] = match
25
31
  status, headers, body = @file_handler.call(env)
26
32
  path = env["PATH_INFO"] = env["PATH_INFO"].chomp('.gz')
27
33
 
28
34
  # Set the Vary HTTP header.
29
- vary = headers["Vary"].to_s.split(",").map { |v| v.strip }
35
+ vary = headers["Vary"].to_s.split(",").map(&:strip)
30
36
  unless vary.include?("*") || vary.include?("Accept-Encoding")
31
37
  headers["Vary"] = vary.push("Accept-Encoding").join(",")
32
38
  end
@@ -34,7 +40,6 @@ module HerokuRailsDeflate
34
40
  # Add encoding and type
35
41
  headers['Content-Encoding'] = 'gzip'
36
42
  headers['Content-Type'] = Rack::Mime.mime_type(File.extname(path), 'text/plain')
37
- headers.delete('Content-Length')
38
43
 
39
44
  # Update cache-control to add directive telling Rack::Deflate to leave it alone.
40
45
  cache_control = headers['Cache-Control'].try(:to_s).try(:downcase)
@@ -44,14 +49,13 @@ module HerokuRailsDeflate
44
49
  headers['Cache-Control'] += ', no-transform'
45
50
  end
46
51
 
52
+ body.close if body.respond_to?(:close)
47
53
  return [status, headers, body]
48
54
  end
49
55
  end
50
56
  end
51
57
 
52
- status, headers, body = @app.call(env)
53
- body.close if body.respond_to?(:close)
54
- [status, headers, body]
58
+ @app.call(env)
55
59
  end
56
60
  end
57
61
  end
@@ -2,7 +2,7 @@ module HerokuRailsDeflate
2
2
  class Version
3
3
  MAJOR = 1
4
4
  MINOR = 0
5
- PATCH = 1
5
+ PATCH = 2
6
6
  BUILD = nil
7
7
 
8
8
  STRING = [MAJOR, MINOR, PATCH, BUILD].compact.join('.')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: heroku_rails_deflate
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-02 00:00:00.000000000 Z
12
+ date: 2014-02-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
@@ -32,7 +32,7 @@ dependencies:
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  none: false
34
34
  requirements:
35
- - - ~>
35
+ - - ! '>='
36
36
  - !ruby/object:Gem::Version
37
37
  version: 3.2.13
38
38
  type: :runtime
@@ -40,7 +40,7 @@ dependencies:
40
40
  version_requirements: !ruby/object:Gem::Requirement
41
41
  none: false
42
42
  requirements:
43
- - - ~>
43
+ - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
45
  version: 3.2.13
46
46
  - !ruby/object:Gem::Dependency
@@ -48,7 +48,7 @@ dependencies:
48
48
  requirement: !ruby/object:Gem::Requirement
49
49
  none: false
50
50
  requirements:
51
- - - ~>
51
+ - - ! '>='
52
52
  - !ruby/object:Gem::Version
53
53
  version: 3.2.13
54
54
  type: :runtime
@@ -56,7 +56,7 @@ dependencies:
56
56
  version_requirements: !ruby/object:Gem::Requirement
57
57
  none: false
58
58
  requirements:
59
- - - ~>
59
+ - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: 3.2.13
62
62
  - !ruby/object:Gem::Dependency
@@ -152,7 +152,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
152
152
  version: '0'
153
153
  segments:
154
154
  - 0
155
- hash: 2875700319250447575
155
+ hash: 310604106122414860
156
156
  required_rubygems_version: !ruby/object:Gem::Requirement
157
157
  none: false
158
158
  requirements: