Class: Togo::Static

Inherits:
Object
  • Object
show all
Defined in:
lib/togo/dispatch/dispatch.rb

Overview

Dispatch

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Static.



204
205
206
207
208
209
210
# File 'lib/togo/dispatch/dispatch.rb', line 204

def initialize(app, options={})
  @app = app
  @path_prefix = options[:path_prefix] || ''
  @urls = options[:urls] || ["/favicon.ico"]
  root = options[:root] || Dir.pwd
  @file_server = Rack::File.new(root)
end

Instance Method Details

#call(env) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/togo/dispatch/dispatch.rb', line 212

def call(env)
  path = env["PATH_INFO"]
  path = path.gsub(Regexp.new("^#{@path_prefix}"), '') if @path_prefix > ''

  unless @urls.kind_of? Hash
    can_serve = @urls.any? { |url| path.index(url) == 0 }
  else
    can_serve = @urls.key? path
  end

  if can_serve
    env["PATH_INFO"] = (@urls.kind_of?(Hash) ? @urls[path] : path)
    @file_server.call(env)
  else
    @app.call(env)
  end
end