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

Constant Summary collapse

DEFAULT_APPLICATION =
lambda do |env|
  Rack::Response.new("No route found", 404).finish
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app = nil, &blk) ⇒ RackInterface

Returns a new instance of RackInterface.



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

def initialize(app = nil, &blk)
  @app = app || DEFAULT_APPLICATION
  @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

#appObject

Returns the value of attribute app.



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

def app
  @app
end

#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



43
44
45
# File 'lib/usher/interface/rack_interface.rb', line 43

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

#after_match(env, response) ⇒ Object

Allows a hook to be placed for sub classes to make use of between matching and calling the application



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/usher/interface/rack_interface.rb', line 73

def after_match(env, response)
  params = response.path.route.default_values ?
    response.path.route.default_values.merge(Hash[response.params]) :
    Hash[response.params]
  
  env['usher.params'] ?
    env['usher.params'].merge!(params) :
    env['usher.params'] = params
  
  # consume the path_info to the script_name
  # response.remaining_path
  consume_path!(env, response) if response.partial_match?
end

#call(env) ⇒ Object



59
60
61
62
63
# File 'lib/usher/interface/rack_interface.rb', line 59

def call(env)
  response = @router.recognize(request = Rack::Request.new(env), request.path_info)
  after_match(env, response) if response
  determine_respondant(response).call(env)
end

#consume_path!(env, response) ⇒ Object

Consume the path from path_info to script_name



105
106
107
108
# File 'lib/usher/interface/rack_interface.rb', line 105

def consume_path!(env, response)
  env["SCRIPT_NAME"] = (env["SCRIPT_NAME"] + response.matched_path)   || ""
  env["PATH_INFO"] = response.remaining_path    || ""
end

#determine_respondant(response) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Determines which application to respond with.

Within the request when determine respondant is called
If there is a matching route to an application, that
application is called, Otherwise the middleware application is called.


94
95
96
97
98
99
100
101
102
# File 'lib/usher/interface/rack_interface.rb', line 94

def determine_respondant(response)
  unless response
    app
  else
    respondant = response.path.route.destination
    respondant = app unless respondant.respond_to?(:call)
    respondant
  end
end

#dupObject



34
35
36
37
38
39
40
41
# File 'lib/usher/interface/rack_interface.rb', line 34

def dup
  new_one = super
  original = self
  new_one.instance_eval do
    @router = router.dup
  end
  new_one
end

#generate(route, options = nil) ⇒ Object



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

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

#parent_routeObject



51
52
53
# File 'lib/usher/interface/rack_interface.rb', line 51

def parent_route
  @router.parent_route
end

#parent_route=(route) ⇒ Object



47
48
49
# File 'lib/usher/interface/rack_interface.rb', line 47

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

#reset!Object



55
56
57
# File 'lib/usher/interface/rack_interface.rb', line 55

def reset!
  @router.reset!
end