Class: Usher::Interface::Rack::Route

Inherits:
Route
  • Object
show all
Defined in:
lib/usher/interface/rack/route.rb

Overview

Route specific for Rack with redirection support built in.

Instance Attribute Summary

Attributes inherited from Route

#conditions, #default_values, #destination, #generate_with, #grapher, #match_partially, #named, #original_path, #parent_route, #paths, #priority, #recognizable, #requirements, #router, #when_proc

Instance Method Summary collapse

Methods inherited from Route

#==, #destination_keys, #dup, #find_matching_path, #initialize, #inspect, #match_partially!, #name, #recognizable!, #recognizable?, #to, #to_s, #unrecognizable!, #when

Constructor Details

This class inherits a constructor from Usher::Route

Instance Method Details

#redirect(path, status = 302) ⇒ Object

Redirect route to some other path.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/usher/interface/rack/route.rb', line 8

def redirect(path, status = 302)
  unless (300..399).include?(status)
    raise ArgumentError, "Status has to be an integer between 300 and 399"
  end
  to { |env|
    params = env[Usher::Interface::Rack::ENV_KEY_PARAMS]
    response = ::Rack::Response.new
    response.redirect(eval(%|"#{path}"|), status)
    response.finish
  }
  self
end

#serves_static_from(root) ⇒ Object

Serves either files from a directory, or a single static file.



22
23
24
25
26
27
28
29
# File 'lib/usher/interface/rack/route.rb', line 22

def serves_static_from(root)
  if File.directory?(root)
    match_partially!
    @destination = ::Rack::File.new(root)
  else
    @destination = proc{|env| env['PATH_INFO'] = File.basename(root); ::Rack::File.new(File.dirname(root)).call(env)}
  end
end