Class: Usher::Interface::RackInterface

Inherits:
Object
  • Object
show all
Defined in:
lib/usher/interface/rack_interface.rb,
lib/usher/interface/rack_interface/route.rb

Defined Under Namespace

Modules: Route Classes: Builder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&blk) ⇒ RackInterface

Returns a new instance of RackInterface.


23
24
25
26
# File 'lib/usher/interface/rack_interface.rb', line 23

def initialize(&blk)
  @router = Usher.new(:request_methods => [:request_method, :host, :port, :scheme], :generator => Usher::Util::Generators::URL.new)
  instance_eval(&blk) if blk
end

Instance Attribute Details

#routerObject (readonly)

Returns the value of attribute router.


7
8
9
# File 'lib/usher/interface/rack_interface.rb', line 7

def router
  @router
end

Instance Method Details

#add(path, options = nil) ⇒ Object


28
29
30
# File 'lib/usher/interface/rack_interface.rb', line 28

def add(path, options = nil)
  @router.add_route(path, options)
end

#call(env) ⇒ Object


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/usher/interface/rack_interface.rb', line 44

def call(env)
  env['usher.params'] ||= {}
  response = @router.recognize(request = Rack::Request.new(env), request.path_info)
  if response.nil?
    body = "No route found"
    headers = {"Content-Type" => "text/plain", "Content-Length" => body.length.to_s}
    [404, headers, [body]]
  else
    params = response.path.route.default_values || {}
    response.params.each{ |hk| params[hk.first] = hk.last}
    
    # consume the path_info to the script_name response.remaining_path
    env["SCRIPT_NAME"] << response.matched_path   || ""
    env["PATH_INFO"] = response.remaining_path    || ""
              
    env['usher.params'].merge!(params)
    
    response.path.route.destination.call(env)
  end
end

#generate(route, params = nil, options = nil) ⇒ Object


65
66
67
# File 'lib/usher/interface/rack_interface.rb', line 65

def generate(route, params = nil, options = nil)
  @usher.generator.generate(route, params, options)
end

#parent_routeObject


36
37
38
# File 'lib/usher/interface/rack_interface.rb', line 36

def parent_route
  @router.parent_route
end

#parent_route=(route) ⇒ Object


32
33
34
# File 'lib/usher/interface/rack_interface.rb', line 32

def parent_route=(route)
  @router.parent_route = route
end

#reset!Object


40
41
42
# File 'lib/usher/interface/rack_interface.rb', line 40

def reset!
  @router.reset!
end