Sha256: ff6d3c16b17d4b889bbb94580920709d4f40ef34919e6d1f8a835f3085884df3

Contents?: true

Size: 896 Bytes

Versions: 8

Compression:

Stored size: 896 Bytes

Contents

module Rack
  class Assets
    include AssetHelpers

    def initialize(app)
      @app = app
    end

    def call(env)
      status, headers, response = @app.call(env)
      return [status, headers, response] unless check_html?(headers, response) && status == 200

      if response.respond_to?(:body)
        response_body = response.body
      else
        response_body = response.first
      end

      # Inject the html, css and js code to the view
      response_body.gsub!('</head>', "#{head_code}</head>")

      headers['Content-Length'] = response_body.bytesize.to_s

      [status, headers, [response_body]]
    end

    private
    def check_html?(headers, response)
      body = response.respond_to?(:body) ? response.body : response.first
      headers['Content-Type'] and
      headers['Content-Type'].include? 'text/html' and
      body =~ %r{<html.*</html>}m
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
iam-0.3.3 lib/rack/assets.rb
iam-0.3.2 lib/rack/assets.rb
iam-0.3.1 lib/rack/assets.rb
iam-0.3.0 lib/rack/assets.rb
iam-0.2.2 lib/rack/assets.rb
iam-0.2.1 lib/rack/assets.rb
iam-0.1.12 lib/rack/assets.rb
iam-0.1.11 lib/rack/assets.rb