Class: Creeper::SprocketsMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/creeper/web.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ SprocketsMiddleware

Returns a new instance of SprocketsMiddleware.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/creeper/web.rb', line 8

def initialize(app, options={})
  @app = app
  @root = options[:root]
  path   =  options[:path] || 'assets'
  @matcher = /^\/#{path}\/*/
  @environment = ::Sprockets::Environment.new(@root)
  @environment.append_path 'assets/javascripts'
  @environment.append_path 'assets/javascripts/vendor'
  @environment.append_path 'assets/stylesheets'
  @environment.append_path 'assets/stylesheets/vendor'
  @environment.append_path 'assets/images'
end

Instance Method Details

#call(env) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/creeper/web.rb', line 21

def call(env)
  # Solve the problem of people requesting /creeper when they need to request /creeper/ so
  # that relative links in templates resolve correctly.
  return [301, { 'Location' => "#{env['SCRIPT_NAME']}/", 'Content-Type' => 'text/html' }, ['redirecting']] if env['SCRIPT_NAME'] == env['REQUEST_PATH']

  return @app.call(env) unless @matcher =~ env["PATH_INFO"]
  env['PATH_INFO'].sub!(@matcher,'')
  @environment.call(env)
end