local_cdn 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.rdoc +64 -0
  3. data/Rakefile +23 -0
  4. data/lib/config.rb +17 -0
  5. data/lib/local_cdn.rb +86 -0
  6. metadata +72 -0
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009-10 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.
@@ -0,0 +1,64 @@
1
+ = Local CDN
2
+
3
+ Local CDN simplifies the inclusion of external stylesheets and JavaScript from various content delivery networks (CDNs), and uses local alternatives for local requests in development mode so your site loads faster, and looks right even when you don't have an Internet connection (eg, on an airplane).
4
+
5
+ <b>Local CDN is compatible with Rails 2.x and Rails 3.</b>
6
+
7
+
8
+ == 1. Install
9
+
10
+ === As a Gem (on Rails 3)
11
+
12
+ Add this to your Gemfile:
13
+
14
+ gem "local_cdn"
15
+
16
+ and run this at the command prompt:
17
+
18
+ bundle install
19
+
20
+ === Or As a Plugin
21
+
22
+ At the command prompt:
23
+
24
+ rails plugin install git://github.com/alexreisner/local_cdn.git
25
+
26
+
27
+ == 2. Configure
28
+
29
+ You have to tell LocalCDN the URLs of the CDNs you're using: the remote (normal) URLs and the local (mirror) URLs. You do this in two separate config files:
30
+
31
+ config/cdn.remote.yml
32
+ config/cdn.local.yml
33
+
34
+ These files might look something like this:
35
+
36
+ # cdn.remote.yml
37
+ google: "http://ajax.googleapis.com/ajax/libs/"
38
+ yahoo: "http://yui.yahooapis.com/"
39
+
40
+ # cdn.local.yml
41
+ google: "http://localhost/cdn_mirror/google/"
42
+ yahoo: "http://localhost/cdn_mirror/yahoo/"
43
+
44
+ <b>Be sure to include the trailing slashes!</b>
45
+
46
+ Having two separate config files might seem tedious, but there is a good reason for it: developers may keep their local CDN mirrors in different places on their computers. Hence <b>you should check <tt>cdn.remote.yml</tt> into your source code repository, but not <tt>cdn.local.yml</tt></b>. Your application will run just fine without <tt>cdn.local.yml</tt>: remote URLs will be used. If a developer wishes, they can set up a local mirror and create <tt>cdn.local.yml</tt>.
47
+
48
+ To set up a local CDN mirror, see: http://github.com/alexreisner/cdn_mirror
49
+
50
+
51
+ == 3. Use
52
+
53
+ To use LocalCDN you must change your stylesheet and javascript inclusion tags slightly. Prepend +cdn_+ to the method name and add a new first argument which is the name of the desired CDN (as a symbol), as specified in <tt>cdn.remote.yml</tt>. For example, if you're currently loading jQuery from Google's CDN you probably have something like this:
54
+
55
+ <%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js" %>
56
+
57
+ To ensure that jQuery still gets served when you don't have a network connection, you would change the above tag to the following (assuming you have the above entry for <tt>google</tt> in your <tt>cdn.remote.yml</tt>:
58
+
59
+ <%= cdn_javascript_include_tag :google, "jquery/1.4.3/jquery.min.js" %>
60
+
61
+ Needless to say, you must also make sure the file is available locally at <tt>http://localhost/local_cdn/jquery/1.4.3/jquery.min.js</tt>. Again, to set up a local CDN mirror, see: http://github.com/alexreisner/cdn_mirror
62
+
63
+
64
+ Copyright (c) 2009-10 Alex Reisner, released under the MIT license
@@ -0,0 +1,23 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the local_cdn plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation for the local_cdn plugin.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'LocalCdn'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README.rdoc')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
@@ -0,0 +1,17 @@
1
+ module LocalCDN
2
+ class Config
3
+ attr_reader :urls
4
+
5
+ def initialize
6
+ @urls = {}
7
+ @urls[:remote] = YAML.load_file(file_path(:remote))
8
+ if File.exist?(f = file_path(:local))
9
+ @urls[:local] = YAML.load_file(f)
10
+ end
11
+ end
12
+
13
+ def file_path(local_or_remote)
14
+ File.join Rails.root, "config", "cdn.#{local_or_remote}.yml"
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,86 @@
1
+ require 'config'
2
+
3
+ module LocalCDN
4
+
5
+ ##
6
+ # Render a stylesheet link tag.
7
+ # Uses local copy in development mode, CDN otherwise.
8
+ #
9
+ def cdn_stylesheet_link_tag(cdn, file)
10
+ cdn_include_tag(:stylesheet, cdn, file)
11
+ end
12
+
13
+ ##
14
+ # Render a JavaScript include tag.
15
+ # Uses local copy in development mode, CDN otherwise.
16
+ #
17
+ def cdn_javascript_include_tag(cdn, file)
18
+ cdn_include_tag(:javascript, cdn, file)
19
+ end
20
+
21
+
22
+ private # -----------------------------------------------------------------
23
+
24
+ ##
25
+ # The CDN configuration object (based on user config files).
26
+ #
27
+ def cdn_config
28
+ if defined?(@config)
29
+ @config
30
+ else
31
+ @config = LocalCDN::Config.new
32
+ end
33
+ end
34
+
35
+ ##
36
+ # Get the URL of the named CDN.
37
+ # URLs are specified in the local_cdn config files.
38
+ #
39
+ def cdn_url(name)
40
+ urls = cdn_config.urls
41
+ if local_request? and locals = urls[:local] and url = locals[name.to_s]
42
+ url
43
+ else
44
+ if remotes = urls[:remote][name.to_s]
45
+ remotes
46
+ else
47
+ raise MissingCDN, name
48
+ end
49
+ end
50
+ end
51
+
52
+ ##
53
+ # Render a stylesheet or JavaScript include tag.
54
+ #
55
+ def cdn_include_tag(type, cdn, file)
56
+ method = type == :javascript ? :javascript_include_tag : :stylesheet_link_tag
57
+ send(method, cdn_url(cdn) + file)
58
+ end
59
+
60
+ ##
61
+ # Are we in the development environment and on localhost?
62
+ #
63
+ def local_request?
64
+ Rails.env == "development" and (
65
+ request.server_name =~ /local/ or request.server_name == '127.0.0.1'
66
+ )
67
+ end
68
+
69
+ ##
70
+ # Are we on Rails 3 or using the rails_xss plugin?
71
+ #
72
+ def rails_3?
73
+ String.instance_methods.include?("html_safe?")
74
+ end
75
+
76
+ ##
77
+ # Exception class, for when the specified CDN has not been defined.
78
+ #
79
+ class MissingCDN < NameError
80
+ def initialize(cdn)
81
+ super "Expected the CDN '#{cdn}' to be defined in the cdn.remote.yml config file"
82
+ end
83
+ end
84
+ end
85
+
86
+ ActionView::Base.send(:include, LocalCDN)
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: local_cdn
3
+ version: !ruby/object:Gem::Version
4
+ hash: 19
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 3
9
+ - 0
10
+ version: 0.3.0
11
+ platform: ruby
12
+ authors:
13
+ - Alex Reisner
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-11-11 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Allows you to use a CDN for static content in production and still load assets in development when lacking an Internet connection (like when you're on a plane).
23
+ email:
24
+ - alex@alexreisner.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - lib/local_cdn.rb
33
+ - lib/config.rb
34
+ - Rakefile
35
+ - README.rdoc
36
+ - MIT-LICENSE
37
+ has_rdoc: true
38
+ homepage: http://github.com/alexreisner/local_cdn
39
+ licenses: []
40
+
41
+ post_install_message:
42
+ rdoc_options: []
43
+
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ hash: 3
52
+ segments:
53
+ - 0
54
+ version: "0"
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 3
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ requirements: []
65
+
66
+ rubyforge_project:
67
+ rubygems_version: 1.3.7
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: Load your Rails app's CDN content locally when not connected to the Internet.
71
+ test_files: []
72
+