local_assets 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +36 -0
- data/Rakefile +26 -0
- data/VERSION +1 -0
- data/lib/rack/local_assets.rb +69 -0
- data/test/local_assets_test.rb +8 -0
- data/test/test_helper.rb +3 -0
- metadata +65 -0
data/.gitignore
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009-11 Alex Reisner
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
= LocalAssets
|
2
|
+
|
3
|
+
LocalAssets facilitates loading of assets normally served by various content delivery networks (CDNs) from local sources in development so your site loads faster, and works even when you don't have an Internet connection (eg, on an airplane).
|
4
|
+
|
5
|
+
To set up a local CDN mirror, see: http://github.com/alexreisner/cdn_mirror
|
6
|
+
|
7
|
+
|
8
|
+
== Rails
|
9
|
+
|
10
|
+
Add the gem to your +Gemfile+. Then add the following to your <tt>config/environments/development.rb</tt> file:
|
11
|
+
|
12
|
+
config.middleware.use "Rack::LocalAssets", {
|
13
|
+
"//ajax.googleapis.com/ajax/libs/" => "//localhost/cdn_mirror/google/"
|
14
|
+
}
|
15
|
+
|
16
|
+
The configuration hash should contain any URL fragments that should be replaced with local alternatives in the development environment. The above example is for assets normally served by Google's CDN.
|
17
|
+
|
18
|
+
|
19
|
+
== Sinatra
|
20
|
+
|
21
|
+
require 'local_assets'
|
22
|
+
|
23
|
+
use Rack::LocalAssets, {
|
24
|
+
"//ajax.googleapis.com/ajax/libs/" => "//localhost/cdn_mirror/google/"
|
25
|
+
}
|
26
|
+
|
27
|
+
|
28
|
+
== To-do List
|
29
|
+
|
30
|
+
* require replaced URLs to be in src or href attributes
|
31
|
+
* allow external config file which does not get committed to repo
|
32
|
+
* allows different local URLs on different development machines
|
33
|
+
* implement as a railtie and load middleware automatically on app_middleware config hook
|
34
|
+
|
35
|
+
|
36
|
+
Copyright (c) 2009-10 Alex Reisner, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
|
4
|
+
require 'rake/testtask'
|
5
|
+
Rake::TestTask.new(:test) do |test|
|
6
|
+
test.libs << 'lib' << 'test'
|
7
|
+
test.pattern = 'test/**/*_test.rb'
|
8
|
+
test.verbose = true
|
9
|
+
end
|
10
|
+
|
11
|
+
desc 'Default: run unit tests.'
|
12
|
+
task :default => :test
|
13
|
+
|
14
|
+
require 'rake/rdoctask'
|
15
|
+
Rake::RDocTask.new do |rdoc|
|
16
|
+
if File.exist?('VERSION')
|
17
|
+
version = File.read('VERSION')
|
18
|
+
else
|
19
|
+
version = ""
|
20
|
+
end
|
21
|
+
|
22
|
+
rdoc.rdoc_dir = 'rdoc'
|
23
|
+
rdoc.title = "local_assets #{version}"
|
24
|
+
rdoc.rdoc_files.include('*.rdoc')
|
25
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
26
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.4.0
|
@@ -0,0 +1,69 @@
|
|
1
|
+
module Rack # :nodoc:
|
2
|
+
|
3
|
+
##
|
4
|
+
# Selectively modifies the application responses by filtering out those
|
5
|
+
# URLs which you provide as keys to the translation table, with the
|
6
|
+
# values you assign to them:
|
7
|
+
#
|
8
|
+
# use LocalAssets, {
|
9
|
+
# 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js' => '/javascripts/jquery.min.js'
|
10
|
+
# }
|
11
|
+
#
|
12
|
+
# Optionally, you can define a block which should return the translation
|
13
|
+
# hash.
|
14
|
+
#
|
15
|
+
# use LocalAssets do |request|
|
16
|
+
# if request.get? && !params['external_assets']
|
17
|
+
# {'http://bad.url/' => '/new/local/path'}
|
18
|
+
# else
|
19
|
+
# {}
|
20
|
+
# end
|
21
|
+
# end
|
22
|
+
#
|
23
|
+
class LocalAssets
|
24
|
+
def initialize(app, translations = {}, &block)
|
25
|
+
@app = app
|
26
|
+
@translations = block_given? ? block : translations
|
27
|
+
end
|
28
|
+
|
29
|
+
def call(env)
|
30
|
+
filter_response(@app.call(env), Rack::Request.new(env))
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
##
|
35
|
+
# If the application response is a Success or Client Error response
|
36
|
+
# (client error being 404, 422, etc), then modify the outbound result,
|
37
|
+
# otherwise pass it through.
|
38
|
+
#
|
39
|
+
def filter_response(response, request)
|
40
|
+
if response.first.to_s =~ /^(?:2|4)\d{2}$/
|
41
|
+
new_body = filter_body(response.pop, request)
|
42
|
+
response.push([new_body])
|
43
|
+
else
|
44
|
+
response
|
45
|
+
end
|
46
|
+
end
|
47
|
+
private :filter_response
|
48
|
+
|
49
|
+
def filter_body(body, request)
|
50
|
+
body = body.body if body.respond_to?(:body)
|
51
|
+
body = body.first if body.kind_of?(Array)
|
52
|
+
body = body.to_s
|
53
|
+
|
54
|
+
translations(request).each_pair do |external, local|
|
55
|
+
body.gsub!(external, local)
|
56
|
+
end
|
57
|
+
|
58
|
+
body
|
59
|
+
end
|
60
|
+
private :filter_body
|
61
|
+
|
62
|
+
def translations(request)
|
63
|
+
@translations.respond_to?(:call) ?
|
64
|
+
@translations.call(request) : @translations
|
65
|
+
end
|
66
|
+
private :translations
|
67
|
+
|
68
|
+
end
|
69
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: local_assets
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.4.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Alex Reisner
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-04-21 00:00:00 -04:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: Allows you to use a CDN in production but seamlessly load assets from localhost in development. Great for when you don't have an Internet connection, like when you're on a plane.
|
18
|
+
email:
|
19
|
+
- alex@alexreisner.com
|
20
|
+
executables: []
|
21
|
+
|
22
|
+
extensions: []
|
23
|
+
|
24
|
+
extra_rdoc_files: []
|
25
|
+
|
26
|
+
files:
|
27
|
+
- .gitignore
|
28
|
+
- MIT-LICENSE
|
29
|
+
- README.rdoc
|
30
|
+
- Rakefile
|
31
|
+
- VERSION
|
32
|
+
- lib/rack/local_assets.rb
|
33
|
+
- test/local_assets_test.rb
|
34
|
+
- test/test_helper.rb
|
35
|
+
has_rdoc: true
|
36
|
+
homepage: http://github.com/alexreisner/local_assets
|
37
|
+
licenses: []
|
38
|
+
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
- lib/rack
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
requirements: []
|
58
|
+
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.6.2
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: Load your Rack app's assets locally when not connected to the Internet.
|
64
|
+
test_files: []
|
65
|
+
|