Module: CloudAssets::Engine::ControllerMethods
- Defined in:
- lib/cloud_assets.rb
Instance Method Summary collapse
- #apply_remote_layout ⇒ Object
- #cloud_asset(path) ⇒ Object
- #correct_uri(src) ⇒ Object
- #inject_into_remote_layout(hash) ⇒ Object
- #optimize_uri(src) ⇒ Object
- #optimized_html_for(asset_response) ⇒ Object
- #override_remote_layout(hash) ⇒ Object
- #remove_remote_layout(selector) ⇒ Object
- #replace_remote_layout(hash) ⇒ Object
- #set_default_remote_layout(layout) ⇒ Object
- #set_remote_layout(layout) ⇒ Object
Instance Method Details
#apply_remote_layout ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/cloud_assets.rb', line 195 def apply_remote_layout begin if @remote_layout.nil? raise Exception.new( <<-ERR No remote layout is defined. Use set_remote_layout or set_default_remote_layout in your views prior to calling apply_remote_layout. ERR ) end if @remote_layout.kind_of? String doc = optimized_html_for cloud_asset "#{@remote_layout}" else doc = optimized_html_for @remote_layout end unless @replacements.nil? @replacements.each do |key, value| value = value.encode("UTF-8") begin doc.css(key).each do |node| node.replace(value) end rescue Rails.logger.warn "Failed to replace template element: #{key}" end end end unless @overrides.nil? @overrides.each do |key, value| except = {} if value.kind_of? Hash # interpret as an option set except = value[:except] value = value[:value] end value = value.encode("UTF-8") begin doc.css(key).each do |node| except.each do |e| node.css(e).each do |sn| value << sn.to_s end end node.inner_html = value end rescue StandardError => e Rails.logger.warn "Failed to override template element: #{key}" Rails.logger.warn e end end end unless @injections.nil? @injections.each do |key, value| value = value.encode("UTF-8") begin doc.css(key).each do |node| node.add_child(value) end rescue Rails.logger.warn "Failed to inject data into template element: #{key}" end end end s = doc.serialize(:encoding => 'UTF-8') # We don't know what in-doc references might be to, so we have to make # them local and can't optimize them to the CDN -- at least not without # some serious guessing which we are not ready to do CloudAssets::fixup_html(s.gsub CloudAssets::origin, '') rescue => e Rails.logger.error e raise e end end |
#cloud_asset(path) ⇒ Object
71 72 73 74 75 76 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 |
# File 'lib/cloud_assets.rb', line 71 def cloud_asset(path) if path =~ /^http.?:/ raise ActionController::RoutingError.new("URL instead of URI for remote asset: #{path}") end p = CloudAssets::fixup_url("#{CloudAssets::origin}#{path}") hydra = Typhoeus::Hydra.hydra unless $dalli_cache.nil? hydra.cache_getter do |request| $dalli_cache.get(request.cache_key) rescue nil end hydra.cache_setter do |request| begin $dalli_cache.set(request.cache_key, request.response, request.cache_timeout) rescue Rails.logger.info "Attempt to save to memcached thru Dalli failed." end end end = { :follow_location => true, :max_redirects => 3, :cache_timeout => CloudAssets::cache_timeout_seconds } unless CloudAssets::user.nil? [:username] = CloudAssets::user [:password] = CloudAssets::password [:auth_method] = :basic end if CloudAssets.verbose Rails.logger.debug "Retrieving remote asset #{p}" end request = Typhoeus::Request.new p, asset_response = nil request.on_complete do |hydra_response| asset_response = hydra_response end hydra.queue request hydra.run if asset_response.code == 404 raise ActionController::RoutingError.new("Remote asset not found: #{path}") elsif asset_response.code > 399 raise Exception.new("Error #{asset_response.code} on remote asset server fetching #{path}") end asset_response end |
#correct_uri(src) ⇒ Object
125 126 127 |
# File 'lib/cloud_assets.rb', line 125 def correct_uri(src) src.gsub(CloudAssets::origin,'') end |
#inject_into_remote_layout(hash) ⇒ Object
160 161 162 163 164 165 |
# File 'lib/cloud_assets.rb', line 160 def inject_into_remote_layout(hash) if @injections.nil? @injections = {} end @injections.merge! hash end |
#optimize_uri(src) ⇒ Object
117 118 119 120 121 122 123 |
# File 'lib/cloud_assets.rb', line 117 def optimize_uri(src) return nil if src.nil? o = CloudAssets::cdn || '' src.gsub!(CloudAssets::origin,'') return src if src =~ /^http:/ "#{o}#{src}" end |
#optimized_html_for(asset_response) ⇒ Object
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 |
# File 'lib/cloud_assets.rb', line 129 def optimized_html_for(asset_response) doc = Nokogiri::HTML(asset_response.body,nil,'UTF-8') { 'img' => 'src', 'link' => 'href' }.each do |tag,attribute| doc.css(tag).each do |e| if tag == 'link' and e['rel'] != 'stylesheet' next end unless e[attribute].nil? e[attribute] = optimize_uri(e[attribute]) end end end { 'a' => 'href', 'script' => 'src', 'link' => 'href' }.each do |tag,attribute| doc.css(tag).each do |e| if tag == 'link' and e['rel'] == 'stylesheet' next end unless e[attribute].nil? e[attribute] = correct_uri(e[attribute]) end end end doc end |
#override_remote_layout(hash) ⇒ Object
167 168 169 170 171 172 |
# File 'lib/cloud_assets.rb', line 167 def override_remote_layout(hash) if @overrides.nil? @overrides = {} end @overrides.merge! hash end |
#remove_remote_layout(selector) ⇒ Object
181 182 183 |
# File 'lib/cloud_assets.rb', line 181 def remove_remote_layout(selector) replace_remote_layout(selector => '') end |
#replace_remote_layout(hash) ⇒ Object
174 175 176 177 178 179 |
# File 'lib/cloud_assets.rb', line 174 def replace_remote_layout(hash) if @replacements.nil? @replacements = {} end @replacements.merge! hash end |
#set_default_remote_layout(layout) ⇒ Object
189 190 191 192 193 |
# File 'lib/cloud_assets.rb', line 189 def set_default_remote_layout(layout) if @remote_layout.nil? @remote_layout = layout end end |
#set_remote_layout(layout) ⇒ Object
185 186 187 |
# File 'lib/cloud_assets.rb', line 185 def set_remote_layout(layout) @remote_layout = layout end |