sprockets-redirect 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2011 Prem Sichanugrist
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,88 @@
1
+ Sprockets::Redirect [![Build Status](https://secure.travis-ci.org/thoughtbot/paperclip.png?branch=master)](http://travis-ci.org/thoughtbot/paperclip)
2
+ ===================
3
+
4
+ A Rack middleware for [Rails](https://github.com/rails/rails) >= 3.1.0 with asset pipeline and asset digest enabled. This middleware is used to redirect any request to static asset without a digest to the version with digest in its filename by reading the `manifest.yml` file generated after you run `rake assets:precompile`
5
+
6
+ For example, if a browser is requesting this URL:
7
+
8
+ http://example.org/assets/application.js
9
+
10
+ They will get redirected to:
11
+
12
+ http://example.org/assets/application-faa42cf2fd5db7e7290baa07109bc82b.js
13
+
14
+ This gem is designed to run on your staging or production environment, where you already precompile all your assets, turn on your asset digest, and turn of asset compilation. This is useful if you're having a static page or E-Mail which refers to static assets in the asset pipeline, which might be impossible and impractical for you to use an URL with a digest in it.
15
+
16
+
17
+ Requirements
18
+ ------------
19
+
20
+ * Application running on [Ruby on Rails](http://github.com/rails/rails) version >= 3.1.0.
21
+ * Enable asset digest by setting `config.assets.digest = true` in your production or staging environment configuration file.
22
+ * Running `rake assets:precompile` to precompile your static assets after deploy to your production or staging server.
23
+
24
+
25
+ Installation
26
+ ------------
27
+
28
+ Insert this line into your Gemfile:
29
+
30
+ group :production, :staging do
31
+ gem 'sprockets-redirect'
32
+ end
33
+
34
+
35
+ Usage
36
+ -----
37
+
38
+ This middleware will be enabled by default if you set `config.assets.compile = false` and `config.assets.digest = true` in your configuration file. It will automatically retrieve your asset prefix and the list of your digests automatically from Rails.
39
+
40
+ You could also config this middle ware at a class level, or at middleware initialization.
41
+
42
+
43
+ ### Configuration at class level:
44
+
45
+ You can set these configurations at your initializer file:
46
+
47
+ #### Sprockets::Redirect.enabled = (true|false)
48
+
49
+ Set this to `false` to override the auto detection and turn off this middleware. Default value is `true`.
50
+
51
+ #### Sprockets::Redirect.manifest = [path to your manifest.yml]
52
+
53
+ Set this to another YAML file which override Rails' default digests manifest. Default value is `nil`.
54
+
55
+ #### Sprockets::Redirect.redirect_status = (301|302)
56
+
57
+ Set this to override the redirection status. By default, this middleware will use `302` status to prevent any proxy server to cache your redirection, as the target might be changed later.
58
+
59
+
60
+ ### Configuration at class initialization:
61
+
62
+ These configurations are configured via an option hash:
63
+
64
+ * `:digests` - Set a hash used for file name lookup. This will be default to Rails' manifest at `Rails.configuration.assets.digests`.
65
+ * `:prefix` - Set a path prefix of your assets file. This will be default to `Rails.configuration.assets.prefix` (usually at `/assets`.)
66
+ * `:manifest` - Set a path to your own manifest file to use for lookup. This will override both `:digest` hash and `Sprockets::Redirect.manifest` setting.
67
+
68
+ You can swap out the middleware inserted automatically by the gem by using `config.middleware.swap` in your configuration file:
69
+
70
+ config.middleware.swap Sprockets::Redirect, Sprockets::Redirect, :manifest => Rails.root.join('assets', 'manifest.yml').to_s
71
+
72
+
73
+ Contributing
74
+ ------------
75
+
76
+ If you found any bug or would like to request a feature, please use Github's [issue tracker](https://github.com/sikachu/sprockets-redirect/issues) to report them. [Pull requests](https://github.com/sikachu/sprockets-redirect/pulls) are always welcomed if you also want to help me fix it. Please make sure to include a test to make sure that I don't break it in the future.
77
+
78
+
79
+ License
80
+ -------
81
+
82
+ This gem is released under MIT license. Please see [LICENSE](https://github.com/sikachu/sprockets-redirect/blob/master/LICENSE) file for more information.
83
+
84
+
85
+ Credit
86
+ ------
87
+
88
+ This gem is maintained by [Prem Sichanugrist](http://sikachu.com) ([@sikachu](http://twitter.com/sikachu)) and supported by [thoughtbot, inc](http://thoughtbot.com).
@@ -0,0 +1,12 @@
1
+ require 'sprockets/redirect'
2
+
3
+ module Sprockets
4
+ class RedirectRailtie < ::Rails::Railtie
5
+ initializer "my_railtie.configure_rails_initialization" do |app|
6
+ if ::Rails.configuration.assets.enabled && !::Rails.configuration.assets.compile && ::Rails.configuration.assets.digest
7
+ app.middleware.insert_before Rack::Lock, Sprockets::Redirect, :digests => ::Rails.configuration.assets.digests,
8
+ :prefix => ::Rails.configuration.assets.prefix
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,82 @@
1
+ require 'rack'
2
+ require 'rack/request'
3
+ require 'rack/mime'
4
+ require 'active_support/core_ext/class/attribute'
5
+ require 'yaml'
6
+
7
+ module Sprockets
8
+ # A Rack middleware for Rails >= 3.1.0 with asset pipeline and asset digest
9
+ # enabled. This middleware is used to redirect any request to static asset
10
+ # without a digest to the version with digest in its filename by reading the
11
+ # manifest.yml file generated after you run rake assets:precompile
12
+ #
13
+ # For example, if a browser is requesting this URL:
14
+ #
15
+ # http://example.org/assets/application.js
16
+ #
17
+ # They will get redirected to:
18
+ #
19
+ # http://example.org/assets/application-faa42cf2fd5db7e7290baa07109bc82b.js
20
+ #
21
+ # This middleware is designed to run on your staging or production environment,
22
+ # where you already precompile all your assets, turn on your asset digest, and
23
+ # turn of asset compilation. This is useful if you're having a static page or
24
+ # E-Mail which refers to static assets in the asset pipeline, which might be
25
+ # impossible and impractical for you to use an URL with a digest in it.
26
+ class Redirect
27
+
28
+ # Set a path to manifest file, which will be used for lookup
29
+ class_attribute :manifest
30
+ self.manifest = nil
31
+
32
+ # Set this to false to disable middleware's redirection
33
+ class_attribute :enabled
34
+ self.enabled = true
35
+
36
+ # Set the status code use for redirection
37
+ class_attribute :redirect_status
38
+ self.redirect_status = 302
39
+
40
+ def initialize(app, options = {})
41
+ @app = app
42
+ @digests = options[:digests] || []
43
+ @prefix = options[:prefix] || "/assets"
44
+ if manifest = options[:manifest] || self.class.manifest
45
+ @digests = YAML.load_file manifest
46
+ end
47
+ end
48
+
49
+ def call(env)
50
+ if self.class.enabled && !@digests.empty? && asset_match?(env)
51
+ redirect_to_digest_version(env)
52
+ else
53
+ @app.call(env)
54
+ end
55
+ end
56
+
57
+ protected
58
+
59
+ # This will returns true if a requested path is matched in the digests hash
60
+ def asset_match?(env)
61
+ @request = Rack::Request.new(env)
62
+ @request.path.match(/^#{@prefix}/) && @digests.has_key?(@request.path.sub(/^#{@prefix}\//, ""))
63
+ end
64
+
65
+ # Sends a redirect header back to browser
66
+ def redirect_to_digest_version(env)
67
+ url = URI(@request.url)
68
+ filename = @digests[@request.path.sub("#{@prefix}/", "")]
69
+ url.path = "#{@prefix}/#{filename}"
70
+ headers = { 'Location' => url.to_s,
71
+ 'Content-Type' => Rack::Mime.mime_type(::File.extname(filename)),
72
+ 'Pragma' => 'no-cache',
73
+ 'Cache-Control' => 'no-cache; max-age=0' }
74
+ [self.class.redirect_status, headers, [redirect_message(url.to_s)]]
75
+ end
76
+
77
+ # Create a default redirect message
78
+ def redirect_message(location)
79
+ %Q(Redirecting to <a href="#{location}">#{location}</a>)
80
+ end
81
+ end
82
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sprockets-redirect
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Prem Sichanugrist
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-09-15 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rack
16
+ requirement: &70163961784520 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70163961784520
25
+ - !ruby/object:Gem::Dependency
26
+ name: activesupport
27
+ requirement: &70163961783480 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 3.1.0
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70163961783480
36
+ - !ruby/object:Gem::Dependency
37
+ name: bundler
38
+ requirement: &70163961774060 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70163961774060
47
+ description: ! " Rack middleware which will look up your `public/assets/manifest.yml`
48
+ and\n redirect a request with no digest in the file name to the version with\n
49
+ \ digest in the file name.\n"
50
+ email: s@sikac.hu
51
+ executables: []
52
+ extensions: []
53
+ extra_rdoc_files:
54
+ - README.md
55
+ files:
56
+ - lib/sprockets-redirect.rb
57
+ - lib/sprockets/redirect.rb
58
+ - LICENSE
59
+ - README.md
60
+ homepage: https://github.com/sikachu/sprockets-redirect
61
+ licenses: []
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 1.8.10
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: Redirect assets with no digest request to a filename with digest version.
84
+ test_files: []