Class: WebResourceBundler::Filters::CdnFilter::Filter
- Inherits:
-
BaseFilter
- Object
- BaseFilter
- WebResourceBundler::Filters::CdnFilter::Filter
- Defined in:
- lib/web_resource_bundler/filters/cdn_filter.rb
Instance Attribute Summary
Attributes inherited from BaseFilter
Instance Method Summary collapse
- #apply!(block_data) ⇒ Object
-
#host_for_image(image_url) ⇒ Object
insures that image linked to one particular host.
-
#initialize(settings, file_manager) ⇒ Filter
constructor
A new instance of Filter.
- #new_filepath(path) ⇒ Object
- #rewrite_content_urls!(file_path, content) ⇒ Object
Methods inherited from BaseFilter
Constructor Details
#initialize(settings, file_manager) ⇒ Filter
Returns a new instance of Filter.
3 4 5 |
# File 'lib/web_resource_bundler/filters/cdn_filter.rb', line 3 def initialize(settings, file_manager) super(settings, file_manager) end |
Instance Method Details
#apply!(block_data) ⇒ Object
7 8 9 10 11 12 13 |
# File 'lib/web_resource_bundler/filters/cdn_filter.rb', line 7 def apply!(block_data) block_data.styles.each do |file| rewrite_content_urls!(file.path, file.content) unless file.content.empty? file.path = new_filepath(file.path) end block_data end |
#host_for_image(image_url) ⇒ Object
insures that image linked to one particular host
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/web_resource_bundler/filters/cdn_filter.rb', line 20 def host_for_image(image_url) #hosts are different depending on protocol if @settings[:protocol] == 'https' hosts = @settings[:https_hosts] else hosts = @settings[:http_hosts] end #getting host based on image url hash host_index = image_url.hash % hosts.size hosts[host_index] end |
#new_filepath(path) ⇒ Object
15 16 17 |
# File 'lib/web_resource_bundler/filters/cdn_filter.rb', line 15 def new_filepath(path) File.join(@settings[:cache_dir], 'cdn_' + File.basename(path)) end |
#rewrite_content_urls!(file_path, content) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/web_resource_bundler/filters/cdn_filter.rb', line 32 def rewrite_content_urls!(file_path, content) content.gsub!(/url\s*\(['|"]?([^\)'"]+)['|"]?\)/) do |s| matched_url = $1 #we shouldn't change url value for base64 encoded images if not (/base64/.match(s) or /mhtml/.match(s)) and matched_url.match(/\.(jpg|gif|png|jpeg|bmp)/) #using CssUrlRewriter method to get image url url = WebResourceBundler::CssUrlRewriter.rewrite_relative_path(file_path, matched_url) host = host_for_image(url) s = "url('#{File.join(host, url)}')" else s end end end |