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(app = nil, &blk) ⇒ RackInterface

Returns a new instance of RackInterface.



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

def initialize(app = nil, &blk)
  @app = app || lambda { |env| Rack::Response.new("No route found", 404).finish }
  @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.



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

def app
  @app
end

#routerObject (readonly)

Returns the value of attribute router.



34
35
36
# File 'lib/usher/interface/rack_interface.rb', line 34

def router
  @router
end

Instance Method Details

#add(path, options = nil) ⇒ Object



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

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

#after_match(request, response) ⇒ Object

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



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/usher/interface/rack_interface.rb', line 112

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

#call(env) ⇒ Object



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

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

#consume_path!(request, response) ⇒ Object

Consume the path from path_info to script_name



144
145
146
147
# File 'lib/usher/interface/rack_interface.rb', line 144

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

#default(app = nil, &block) ⇒ Object

default { |env| … } default DefaultApp



58
59
60
# File 'lib/usher/interface/rack_interface.rb', line 58

def default(app = nil, &block)
  @app = app ? app : block
end

#delete(path, options = {}) ⇒ Object



81
82
83
# File 'lib/usher/interface/rack_interface.rb', line 81

def delete(path, options = {})
  self.add(path, options.merge!(:conditions => {:request_method => "DELETE"}))
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.


133
134
135
136
137
138
139
140
141
# File 'lib/usher/interface/rack_interface.rb', line 133

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

#dupObject



43
44
45
46
47
48
49
50
# File 'lib/usher/interface/rack_interface.rb', line 43

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

#generate(route, options = nil) ⇒ Object



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

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

#get(path, options = {}) ⇒ Object

shortcuts for adding routes for HTTP methods, for example: add(“/url”, :conditions => => “POST”}) is the same as: post(“/url”)

if you need more complex setup, use method add directly, for example: add(“/url”, :conditions => => [“POST”, “PUT”]})



69
70
71
# File 'lib/usher/interface/rack_interface.rb', line 69

def get(path, options = {})
  self.add(path, options.merge!(:conditions => {:request_method => "GET"}))
end

#parent_routeObject



89
90
91
# File 'lib/usher/interface/rack_interface.rb', line 89

def parent_route
  @router.parent_route
end

#parent_route=(route) ⇒ Object



85
86
87
# File 'lib/usher/interface/rack_interface.rb', line 85

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

#post(path, options = {}) ⇒ Object



73
74
75
# File 'lib/usher/interface/rack_interface.rb', line 73

def post(path, options = {})
  self.add(path, options.merge!(:conditions => {:request_method => "POST"}))
end

#put(path, options = {}) ⇒ Object



77
78
79
# File 'lib/usher/interface/rack_interface.rb', line 77

def put(path, options = {})
  self.add(path, options.merge!(:conditions => {:request_method => "PUT"}))
end

#reset!Object



93
94
95
# File 'lib/usher/interface/rack_interface.rb', line 93

def reset!
  @router.reset!
end