Class: Rack::Bundle
- Inherits:
-
Object
- Object
- Rack::Bundle
- Defined in:
- lib/rack/bundle.rb
Defined Under Namespace
Classes: Base, 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.
24 25 26 27 28 29 |
# File 'lib/rack/bundle.rb', line 24 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.
18 19 20 |
# File 'lib/rack/bundle.rb', line 18 def document @document end |
#public_dir ⇒ Object
Returns the value of attribute public_dir.
18 19 20 |
# File 'lib/rack/bundle.rb', line 18 def public_dir @public_dir end |
#storage ⇒ Object
Returns the value of attribute storage.
18 19 20 |
# File 'lib/rack/bundle.rb', line 18 def storage @storage end |
Instance Method Details
#call(env) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/rack/bundle.rb', line 31 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
47 48 49 50 51 |
# File 'lib/rack/bundle.rb', line 47 def parse! body = "" @response.each do |part| body << part end @document = Nokogiri::HTML(body) end |
#replace_javascripts! ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/rack/bundle.rb', line 53 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, :charset => 'utf-8' @document.css(SELECTORS.js).first.before(bundle_node) @document.css(SELECTORS.js).slice(1..-1).remove @document end |
#replace_stylesheets! ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/rack/bundle.rb', line 66 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 = stylesheets_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, :media => media nodes.first.before(node) nodes.map { |node| node.remove } end @document end |