Class: Usher::Interface::Rack
- Inherits:
-
Object
- Object
- Usher::Interface::Rack
- Defined in:
- lib/usher/interface/rack.rb,
lib/usher/interface/rack/route.rb,
lib/usher/interface/rack/builder.rb,
lib/usher/interface/rack/middleware.rb
Defined Under Namespace
Classes: Builder, Middleware, Route
Constant Summary collapse
- ENV_KEY_RESPONSE =
'usher.response'.freeze
- ENV_KEY_PARAMS =
'usher.params'.freeze
- ENV_KEY_DEFAULT_ROUTER =
'usher.router'.freeze
Instance Attribute Summary collapse
-
#redirect_on_trailing_delimiters ⇒ Object
Returns the value of attribute redirect_on_trailing_delimiters.
-
#router ⇒ Object
readonly
Returns the value of attribute router.
-
#router_key ⇒ Object
readonly
Returns the value of attribute router_key.
Instance Method Summary collapse
-
#add(path, options = nil) ⇒ Object
(also: #path)
Adds a route to the route set with a ‘path` and optional `options`.
-
#after_match(request, response) ⇒ Object
Allows a hook to be placed for sub classes to make use of between matching and calling the application.
- #call(env) ⇒ Object
-
#consume_path!(request, response) ⇒ Object
Consume the path from path_info to script_name.
-
#default(app = nil, &block) ⇒ Object
Sets the default application when route matching is unsuccessful.
- #default_app ⇒ Object
-
#delete(path, options = {}) ⇒ Object
Convenience method for adding a route that only matches request method ‘DELETE`.
-
#determine_respondant(response) ⇒ Object
private
Determines which application to respond with.
-
#dup ⇒ Object
Creates a deep copy of the current route set.
- #generate(route, options = nil) ⇒ Object
-
#get(path, options = {}) ⇒ Object
Convenience method for adding a route that only matches request methods ‘GET` and `HEAD`.
-
#initialize(options = {}, &blk) ⇒ Rack
constructor
Constructor for Rack interface for Usher.
-
#only_get(path, options = {}) ⇒ Object
Convenience method for adding a route that only matches request method ‘GET`.
- #parent_route ⇒ Object
- #parent_route=(route) ⇒ Object
-
#post(path, options = {}) ⇒ Object
Convenience method for adding a route that only matches request method ‘POST`.
-
#put(path, options = {}) ⇒ Object
Convenience method for adding a route that only matches request method ‘PUT`.
- #reset! ⇒ Object
-
#use_destinations? ⇒ Boolean
Returns whether the route set has use_destinations? enabled.
Constructor Details
#initialize(options = {}, &blk) ⇒ Rack
Constructor for Rack interface for Usher. app
- the default application to route to if no matching route is found. The default is a 404 response. options
- options to configure the router
-
use_destinations
- option to disable using the destinations passed into routes. (Defaulttrue
) -
router_key
- Key in which to put router into env. (Defaultusher.router
) -
request_methods
- Request methods onRack::Request
to use in determining recognition. (Default[:request_method, :host, :port, :scheme]
) -
generator
- Route generator to use. (DefaultUsher::Util::Generators::URL.new
) -
allow_identical_variable_names
- Option to prevent routes with identical variable names to be added. eg, /:variable/:variable would raise an exception if this option is not enabled. (Defaultfalse
)
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/usher/interface/rack.rb', line 25 def initialize( = {}, &blk) @_app = [:default_app] || proc{|env| ::Rack::Response.new("Not Found", 404).finish } @use_destinations = .key?(:use_destinations) ? .delete(:use_destinations) : true @router_key = .delete(:router_key) || ENV_KEY_DEFAULT_ROUTER request_methods = .delete(:request_methods) || [:request_method, :host, :port, :scheme] generator = .delete(:generator) || Usher::Util::Generators::URL.new allow_identical_variable_names = .key?(:allow_identical_variable_names) ? [:allow_identical_variable_names] : false self.redirect_on_trailing_delimiters = .key?(:redirect_on_trailing_delimiters) ? .delete(:redirect_on_trailing_delimiters) : false if redirect_on_trailing_delimiters [:ignore_trailing_delimiters] = true end = {:request_methods => request_methods, :generator => generator, :allow_identical_variable_names => allow_identical_variable_names, :detailed_failure => true} .merge!() @router = Usher.new() @router.route_class = Rack::Route instance_eval(&blk) if blk end |
Instance Attribute Details
#redirect_on_trailing_delimiters ⇒ Object
Returns the value of attribute redirect_on_trailing_delimiters.
15 16 17 |
# File 'lib/usher/interface/rack.rb', line 15 def redirect_on_trailing_delimiters @redirect_on_trailing_delimiters end |
#router ⇒ Object (readonly)
Returns the value of attribute router.
14 15 16 |
# File 'lib/usher/interface/rack.rb', line 14 def router @router end |
#router_key ⇒ Object (readonly)
Returns the value of attribute router_key.
14 15 16 |
# File 'lib/usher/interface/rack.rb', line 14 def router_key @router_key end |
Instance Method Details
#add(path, options = nil) ⇒ Object Also known as: path
Adds a route to the route set with a ‘path` and optional `options`. See Usher#add_route
for more details about the format of the route and options accepted here.
61 62 63 |
# File 'lib/usher/interface/rack.rb', line 61 def add(path, = nil) @router.add_route(path, ) 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
141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/usher/interface/rack.rb', line 141 def after_match(request, response) params = response.path.route.default_values ? response.path.route.default_values.merge(response.params_as_hash) : response.params_as_hash request.env[ENV_KEY_RESPONSE] ||= [] request.env[ENV_KEY_RESPONSE] << response request.env[ENV_KEY_PARAMS] ? request.env[ENV_KEY_PARAMS].merge!(params) : (request.env[ENV_KEY_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
119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/usher/interface/rack.rb', line 119 def call(env) env[router_key] = self request = ::Rack::Request.new(env) response = @router.recognize(request, request.path_info) if response.succeeded? && redirect_on_trailing_delimiters and response.only_trailing_delimiters and (request.get? || request.head?) response = ::Rack::Response.new response.redirect(request.path_info[0, request.path_info.size - 1], 302) response.finish else after_match(request, response) if response.succeeded? determine_respondant(response).call(env) end end |
#consume_path!(request, response) ⇒ Object
Consume the path from path_info to script_name
179 180 181 182 |
# File 'lib/usher/interface/rack.rb', line 179 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
Sets the default application when route matching is unsuccessful. Accepts either an application ‘app` or a block to call.
default { |env| … } default DefaultApp
70 71 72 |
# File 'lib/usher/interface/rack.rb', line 70 def default(app = nil, &block) @_app = app ? app : block end |
#default_app ⇒ Object
184 185 186 |
# File 'lib/usher/interface/rack.rb', line 184 def default_app _app end |
#delete(path, options = {}) ⇒ Object
Convenience method for adding a route that only matches request method ‘DELETE`.
103 104 105 |
# File 'lib/usher/interface/rack.rb', line 103 def delete(path, = {}) add(path, .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.
163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/usher/interface/rack.rb', line 163 def determine_respondant(response) usable_response = response.succeeded? && use_destinations? && response && response.destination if usable_response && response.destination.respond_to?(:call) response.destination elsif usable_response && response.destination.respond_to?(:args) && response.destination.args.first.respond_to?(:call) response.args.first elsif !response.succeeded? && response.request_method? rack_response = ::Rack::Response.new("Method Not Allowed", 405) rack_response['Allow'] = response.acceptable_responses_only_strings.join(", ") proc { |env| rack_response.finish } else _app end end |
#dup ⇒ Object
Creates a deep copy of the current route set.
50 51 52 53 54 55 56 57 |
# File 'lib/usher/interface/rack.rb', line 50 def dup new_one = super original = self new_one.instance_eval do @router = router.dup end new_one end |
#generate(route, options = nil) ⇒ Object
133 134 135 |
# File 'lib/usher/interface/rack.rb', line 133 def generate(route, = nil) @router.generator.generate(route, ) end |
#get(path, options = {}) ⇒ Object
Convenience method for adding a route that only matches request methods ‘GET` and `HEAD`.
88 89 90 |
# File 'lib/usher/interface/rack.rb', line 88 def get(path, = {}) add(path, .merge!(:conditions => {:request_method => ["HEAD", "GET"]})) end |
#only_get(path, options = {}) ⇒ Object
Convenience method for adding a route that only matches request method ‘GET`.
83 84 85 |
# File 'lib/usher/interface/rack.rb', line 83 def only_get(path, = {}) add(path, .merge!(:conditions => {:request_method => ["GET"]})) end |
#parent_route ⇒ Object
111 112 113 |
# File 'lib/usher/interface/rack.rb', line 111 def parent_route @router.parent_route end |
#parent_route=(route) ⇒ Object
107 108 109 |
# File 'lib/usher/interface/rack.rb', line 107 def parent_route=(route) @router.parent_route = route end |
#post(path, options = {}) ⇒ Object
Convenience method for adding a route that only matches request method ‘POST`.
93 94 95 |
# File 'lib/usher/interface/rack.rb', line 93 def post(path, = {}) add(path, .merge!(:conditions => {:request_method => "POST"})) end |
#put(path, options = {}) ⇒ Object
Convenience method for adding a route that only matches request method ‘PUT`.
98 99 100 |
# File 'lib/usher/interface/rack.rb', line 98 def put(path, = {}) add(path, .merge!(:conditions => {:request_method => "PUT"})) end |
#reset! ⇒ Object
115 116 117 |
# File 'lib/usher/interface/rack.rb', line 115 def reset! @router.reset! end |
#use_destinations? ⇒ Boolean
Returns whether the route set has use_destinations? enabled.
45 46 47 |
# File 'lib/usher/interface/rack.rb', line 45 def use_destinations? @use_destinations end |