class RogerSprockets::Middleware
Public Class Methods
new(app, options = {})
click to toggle source
# File lib/roger_sprockets/middleware.rb, line 3 def initialize(app, options = {}) @app = app defaults = { sprockets_environment: ::Sprockets::Environment.new, load_paths: ["html/javascripts", "bower_components"] } # Use the resolver to translate urls # to file paths @resolver = Roger::Resolver.new(app.project.html_path) @options = defaults.update(options) @sprockets = @options[:sprockets_environment] # Add load_paths to sprocket env @options[:load_paths].each do |load_path| @sprockets.append_path(app.project.path + load_path) end end
Public Instance Methods
call(env)
click to toggle source
# File lib/roger_sprockets/middleware.rb, line 23 def call(env) url = ::Rack::Utils.unescape(env["PATH_INFO"].to_s).sub(/^\//, "") # Convert the url to an absolute path, # which is used to retrieve the asset from sprockets asset_path = resolve_url(url) if url.length > 1 && file = @sprockets.find_asset(asset_path) respond(file) else @app.call(env) end end
Private Instance Methods
resolve_url(url)
click to toggle source
# File lib/roger_sprockets/middleware.rb, line 39 def resolve_url(url) @resolver.url_to_path url, exact_match: true end
respond(file)
click to toggle source
# File lib/roger_sprockets/middleware.rb, line 43 def respond(file) resp = ::Rack::Response.new do |res| res.status = 200 res.headers["Content-Type"] = file.content_type res.write file.to_s end resp.finish end