rails-assets-cdn 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +80 -0
- data/Rakefile +6 -0
- data/lib/rails-assets-cdn.rb +3 -0
- data/lib/rails-assets-cdn/rails-assets-cdn.rb +72 -0
- data/lib/rails-assets-cdn/version.rb +3 -0
- data/rails-assets-cdn.gemspec +20 -0
- data/test/rails_assets_cdn_test.rb +200 -0
- metadata +72 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Carl Mercier
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
# rails-assets-cdn
|
2
|
+
|
3
|
+
This gem allows you to add single or multiple CDN hosts for the asset pipeline.
|
4
|
+
It aims to be flexible and configurable.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'rails-assets-cdn'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Then add `assets_cdn.yml` to your `config` directory. Here's an example of the the most minimal config file possible:
|
17
|
+
|
18
|
+
development:
|
19
|
+
host: assets.myapp.dev
|
20
|
+
|
21
|
+
And here's a more complex config file supporting multiple CDN hosts for the production environment:
|
22
|
+
|
23
|
+
development:
|
24
|
+
host: assets.myapp.dev
|
25
|
+
|
26
|
+
production:
|
27
|
+
enabled: true
|
28
|
+
hosts:
|
29
|
+
- assets1.myapp.com
|
30
|
+
- assets2.myapp.com
|
31
|
+
- assets3.myapp.com
|
32
|
+
- assets4.myapp.com
|
33
|
+
protocol: browser
|
34
|
+
fallback_protocol: http
|
35
|
+
|
36
|
+
Important: don't forget to restart your application!
|
37
|
+
|
38
|
+
## Configuration options:
|
39
|
+
|
40
|
+
#### enabled
|
41
|
+
|
42
|
+
A boolean value to enable/disable the CDN.
|
43
|
+
|
44
|
+
#### host
|
45
|
+
|
46
|
+
A single CDN host.
|
47
|
+
|
48
|
+
#### hosts
|
49
|
+
|
50
|
+
An array of hosts. The path of the asset is hashed then the modulo is extracted to determine which CDN host to use.
|
51
|
+
|
52
|
+
This is a simple and effective way to evenly split the load between multiple hosts. It is recommended to have 4 hosts (even if they all CNAME to the same destination) since some browsers can't download more than 2 files per host at the same time.
|
53
|
+
|
54
|
+
#### fallback_protocol
|
55
|
+
|
56
|
+
If `request` is used for protocol, this is the protocol Rails will use when an asset URL is generated outside of a request since it won't be able to determine the protocol to use.
|
57
|
+
|
58
|
+
Default: `http`
|
59
|
+
|
60
|
+
#### protocol
|
61
|
+
|
62
|
+
The protocol to prepend to the asset URL.
|
63
|
+
|
64
|
+
Default: `browser`
|
65
|
+
|
66
|
+
Options:
|
67
|
+
|
68
|
+
- `browser` (Let the browser determine which protocol to use)
|
69
|
+
- `request` (Rails prepends the protocol of the current request)
|
70
|
+
- `http` (force http://)
|
71
|
+
- `https` (force https://)
|
72
|
+
|
73
|
+
|
74
|
+
## Contributing
|
75
|
+
|
76
|
+
1. Fork it
|
77
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
78
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
79
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
80
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
module RailsAssetsCdn
|
2
|
+
class Railtie < ::Rails::Railtie
|
3
|
+
def initialize_railtie
|
4
|
+
Rails.logger.info "Initializing rails-assets-cdn..."
|
5
|
+
$rails_assets_cdn_initialized = false
|
6
|
+
|
7
|
+
config = nil
|
8
|
+
%w(assets_cdn.yml cdn.yml).each do |filename|
|
9
|
+
config_path = Rails.root.join('config', filename).to_s
|
10
|
+
if File.exist?(config_path)
|
11
|
+
Rails.logger.info " Reading config file at #{config_path}..."
|
12
|
+
config = YAML.load_file(config_path)[Rails.env]
|
13
|
+
break
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
unless config
|
18
|
+
Rails.logger.info " Couldn't find configuration file. Skipping initialization."
|
19
|
+
return false
|
20
|
+
end
|
21
|
+
|
22
|
+
# Set defaults
|
23
|
+
config['enabled'] = true if config['enabled'].nil?
|
24
|
+
config['protocol'] = 'browser' if config['protocol'].nil?
|
25
|
+
config['fallback_protocol'] = 'http' if config['fallback_protocol'].nil?
|
26
|
+
config['protocol'].sub!('://', '')
|
27
|
+
config['fallback_protocol'].sub!('://', '')
|
28
|
+
|
29
|
+
# Validations
|
30
|
+
unless config['enabled']
|
31
|
+
Rails.logger.info " rails-assets-cdn is disabled. Bye bye!"
|
32
|
+
return false
|
33
|
+
end
|
34
|
+
|
35
|
+
raise ArgumentError.new("Protocol #{config["protocol"]} is invalid.") \
|
36
|
+
unless %w(browser request http https).include?(config['protocol'])
|
37
|
+
raise ArgumentError.new("Fallback protocol #{config["fallback_protocol"]} is invalid.") \
|
38
|
+
unless %w(http https).include?(config['fallback_protocol'])
|
39
|
+
raise ArgumentError.new("CDN hosts not specified.") \
|
40
|
+
unless (config['hosts'] || config['host']).present?
|
41
|
+
|
42
|
+
$rails_assets_cdn = {}
|
43
|
+
$rails_assets_cdn[:hosts] = Array.wrap(config['hosts'] || config['host'])
|
44
|
+
$rails_assets_cdn[:fallback_protocol] = config['fallback_protocol'] + "://"
|
45
|
+
$rails_assets_cdn[:protocol] = case config['protocol'].to_sym
|
46
|
+
when :browser; '//' # Let browser decide on protocol
|
47
|
+
when :request; nil # Same protocol as the current request, but hardcoded
|
48
|
+
when :http; 'http://' # Force http
|
49
|
+
when :https; 'https://' # Force https
|
50
|
+
end
|
51
|
+
|
52
|
+
Rails.logger.info " CDN hosts: #{$rails_assets_cdn[:hosts].to_sentence}."
|
53
|
+
Rails.logger.info " Protocol strategy: #{$rails_assets_cdn[:protocol]} (fallback: #{$rails_assets_cdn[:fallback_protocol]})."
|
54
|
+
|
55
|
+
ActionController::Base.asset_host = Proc.new do |source, request|
|
56
|
+
protocol = if $rails_assets_cdn[:protocol]
|
57
|
+
$rails_assets_cdn[:protocol]
|
58
|
+
else
|
59
|
+
request ? request.protocol : $rails_assets_cdn[:fallback_protocol]
|
60
|
+
end
|
61
|
+
|
62
|
+
$rails_assets_cdn[:hosts].map { |host| protocol.to_s + host }[source.hash % $rails_assets_cdn[:hosts].size]
|
63
|
+
end
|
64
|
+
|
65
|
+
$rails_assets_cdn[:initialized] = true
|
66
|
+
end
|
67
|
+
|
68
|
+
initializer "rails_assets_cdn.initialize" do
|
69
|
+
self.initialize_railtie
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rails-assets-cdn/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "rails-assets-cdn"
|
8
|
+
gem.version = RailsAssetsCdn::VERSION
|
9
|
+
gem.authors = ["Carl Mercier"]
|
10
|
+
gem.email = ["carl@carlmercier.com"]
|
11
|
+
gem.description = %q{Add support for single and multiple CDN hosts for the Rails asset pipeline}
|
12
|
+
gem.summary = %q{CDN support for Rails asset pipeline made easy}
|
13
|
+
gem.homepage = "http://github.com/cmer/rails-assets-cdn"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
gem.add_dependency 'rails'
|
20
|
+
end
|
@@ -0,0 +1,200 @@
|
|
1
|
+
# Oooh
|
2
|
+
#
|
3
|
+
# We're no strangers to love
|
4
|
+
# You know the rules and so do I
|
5
|
+
# A full commitment's what I'm thinking of
|
6
|
+
# You wouldn't get this from any other guy
|
7
|
+
#
|
8
|
+
# I just wanna tell you how I'm feeling
|
9
|
+
# Gotta make you understand
|
10
|
+
#
|
11
|
+
# Never gonna give you up
|
12
|
+
# Never gonna let you down
|
13
|
+
# Never gonna run around and desert you
|
14
|
+
# Never gonna make you cry
|
15
|
+
# Never gonna say goodbye
|
16
|
+
# Never gonna tell a lie and hurt you
|
17
|
+
#
|
18
|
+
# We've known each other for so long
|
19
|
+
# Your heart's been aching, but
|
20
|
+
# You're too shy to say it
|
21
|
+
# Inside, we both know what's been going on
|
22
|
+
# We know the game and we're gonna play it
|
23
|
+
#
|
24
|
+
# And if you ask me how I'm feeling
|
25
|
+
# Don't tell me you're too blind to see
|
26
|
+
#
|
27
|
+
# Never gonna give you up
|
28
|
+
# Never gonna let you down
|
29
|
+
# Never gonna run around and desert you
|
30
|
+
# Never gonna make you cry
|
31
|
+
# Never gonna say goodbye
|
32
|
+
# Never gonna tell a lie and hurt you
|
33
|
+
#
|
34
|
+
# Never gonna give you up
|
35
|
+
# Never gonna let you down
|
36
|
+
# Never gonna run around and desert you
|
37
|
+
# Never gonna make you cry
|
38
|
+
# Never gonna say goodbye
|
39
|
+
# Never gonna tell a lie and hurt you
|
40
|
+
#
|
41
|
+
# (Ooh, give you up)
|
42
|
+
# (Ooh, give you up)
|
43
|
+
# Never gonna give, never gonna give
|
44
|
+
# (Give you up)
|
45
|
+
# Never gonna give, never gonna give
|
46
|
+
# (Give you up)
|
47
|
+
#
|
48
|
+
# We've known each other for so long
|
49
|
+
# Your heart's been aching, but
|
50
|
+
# You're too shy to say it
|
51
|
+
# Inside, we both know what's been going on
|
52
|
+
# We know the game and we're gonna play it
|
53
|
+
#
|
54
|
+
# I just wanna tell you how I'm feeling
|
55
|
+
# Gotta make you understand
|
56
|
+
#
|
57
|
+
# Never gonna give you up
|
58
|
+
# Never gonna let you down
|
59
|
+
# Never gonna run around and desert you
|
60
|
+
# Never gonna make you cry
|
61
|
+
# Never gonna say goodbye
|
62
|
+
# Never gonna tell a lie and hurt you
|
63
|
+
#
|
64
|
+
# Never gonna give you up
|
65
|
+
# Never gonna let you down
|
66
|
+
# Never gonna run around and desert you
|
67
|
+
# Never gonna make you cry
|
68
|
+
# Never gonna say goodbye
|
69
|
+
# Never gonna tell a lie and hurt you
|
70
|
+
#
|
71
|
+
# Never gonna give you up
|
72
|
+
# Never gonna let you down
|
73
|
+
# Never gonna run around and desert you
|
74
|
+
# Never gonna make you cry
|
75
|
+
# Never gonna say goodbye
|
76
|
+
# Never gonna tell a lie and hurt you
|
77
|
+
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
|
200
|
+
`open http://www.youtube.com/watch?v=oHg5SJYRHA0`
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails-assets-cdn
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Carl Mercier
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-03-15 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
version_requirements: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ! '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
none: false
|
21
|
+
name: rails
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
requirement: !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - ! '>='
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: '0'
|
29
|
+
none: false
|
30
|
+
description: Add support for single and multiple CDN hosts for the Rails asset pipeline
|
31
|
+
email:
|
32
|
+
- carl@carlmercier.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- Gemfile
|
39
|
+
- LICENSE.txt
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- lib/rails-assets-cdn.rb
|
43
|
+
- lib/rails-assets-cdn/rails-assets-cdn.rb
|
44
|
+
- lib/rails-assets-cdn/version.rb
|
45
|
+
- rails-assets-cdn.gemspec
|
46
|
+
- test/rails_assets_cdn_test.rb
|
47
|
+
homepage: http://github.com/cmer/rails-assets-cdn
|
48
|
+
licenses: []
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ! '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
none: false
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ! '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
none: false
|
65
|
+
requirements: []
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 1.8.23
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: CDN support for Rails asset pipeline made easy
|
71
|
+
test_files:
|
72
|
+
- test/rails_assets_cdn_test.rb
|