Class: Rack::Bundle
- Inherits:
-
Object
- Object
- Rack::Bundle
- Defined in:
- lib/rack/bundle.rb
Defined Under Namespace
Classes: CSSBundle, DatabaseStore, FileSystemStore, JSBundle
Constant Summary collapse
- SELECTORS =
Struct.new('Selector', :js, :css).new( 'head script[src$=".js"]:not([src^="http"])', 'head link[href$=".css"]:not([href^="http"])' )
Instance Attribute Summary collapse
-
#document ⇒ Object
Returns the value of attribute document.
-
#public_dir ⇒ Object
Returns the value of attribute public_dir.
-
#storage ⇒ Object
Returns the value of attribute storage.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, options = {}) {|_self| ... } ⇒ Bundle
constructor
A new instance of Bundle.
- #parse! ⇒ Object
- #replace_javascripts! ⇒ Object
- #replace_stylesheets! ⇒ Object
Constructor Details
#initialize(app, options = {}) {|_self| ... } ⇒ Bundle
Returns a new instance of Bundle.
16 17 18 19 20 21 |
# File 'lib/rack/bundle.rb', line 16 def initialize app, = {} @app, @public_dir = app, [:public_dir] @storage = [:storage] || FileSystemStore.new(@public_dir) yield self if block_given? raise ArgumentError, ":public_dir needs to be a directory" unless ::File.directory?(@public_dir.to_s) end |
Instance Attribute Details
#document ⇒ Object
Returns the value of attribute document.
10 11 12 |
# File 'lib/rack/bundle.rb', line 10 def document @document end |
#public_dir ⇒ Object
Returns the value of attribute public_dir.
10 11 12 |
# File 'lib/rack/bundle.rb', line 10 def public_dir @public_dir end |
#storage ⇒ Object
Returns the value of attribute storage.
10 11 12 |
# File 'lib/rack/bundle.rb', line 10 def storage @storage end |
Instance Method Details
#call(env) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/rack/bundle.rb', line 23 def call env if match = %r(^/rack-bundle-(\w+)).match(env['PATH_INFO']) bundle = @storage.find_bundle_by_hash match[1] bundle ? respond_with(bundle) : not_found else status, headers, @response = @app.call(env) return [status, headers, @response] unless headers['Content-Type'] =~ /html/ parse! replace_javascripts! replace_stylesheets! body = @document.to_html headers['Content-Length'] = body.length.to_s if headers['Content-Length'] # Not sure how UTF-8 plays into this [status, headers, [body]] end end |
#parse! ⇒ Object
39 40 41 42 43 44 |
# File 'lib/rack/bundle.rb', line 39 def parse! # http://github.com/logicaltext/rack-bundle/commit/8e7d0282b05b01a0cbfa59b519242046437605f6 body = "" @response.each do |part| body << part end @document = Nokogiri::HTML(body) end |
#replace_javascripts! ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/rack/bundle.rb', line 46 def replace_javascripts! return unless @document.css(SELECTORS.js).count > 1 bundle = JSBundle.new *scripts @storage.add bundle unless @storage.has_bundle? bundle bundle_node = @document.create_element 'script', :type => 'text/javascript', :src => bundle_path(bundle), :charset => 'utf-8' @document.css(SELECTORS.js).first.before(bundle_node) @document.css(SELECTORS.js).slice(1..-1).remove @document end |
#replace_stylesheets! ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/rack/bundle.rb', line 59 def replace_stylesheets! return unless local_css_nodes.count > 1 styles = local_css_nodes.group_by { |node| node.attribute('media').value rescue nil } styles.each do |media, nodes| next unless nodes.count > 1 stylesheets = stylesheet_contents_for nodes bundle = CSSBundle.new *stylesheets @storage.add bundle unless @storage.has_bundle? bundle node = @document.create_element 'link', :rel => 'stylesheet', :type => 'text/css', :href => bundle_path(bundle), :media => media nodes.first.before(node) nodes.map { |node| node.remove } end @document end |