Class: Usher::Interface::Rack

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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. (Default true)

  • router_key - Key in which to put router into env. (Default usher.router)

  • request_methods - Request methods on Rack::Request to use in determining recognition. (Default [:request_method, :host, :port, :scheme])

  • generator - Route generator to use. (Default Usher::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. (Default false)



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(options = {}, &blk)
  @_app = options[:default_app] || proc{|env| ::Rack::Response.new("Not Found", 404).finish }
  @use_destinations = options.key?(:use_destinations) ? options.delete(:use_destinations) : true
  @router_key = options.delete(:router_key) || ENV_KEY_DEFAULT_ROUTER
  request_methods = options.delete(:request_methods) || [:request_method, :host, :port, :scheme]
  generator = options.delete(:generator) || Usher::Util::Generators::URL.new
  allow_identical_variable_names = options.key?(:allow_identical_variable_names) ? options[:allow_identical_variable_names] : false
  self.redirect_on_trailing_delimiters = options.key?(:redirect_on_trailing_delimiters) ? options.delete(:redirect_on_trailing_delimiters) : false
  if redirect_on_trailing_delimiters
    options[:ignore_trailing_delimiters] = true
  end
  usher_options = {:request_methods => request_methods, :generator => generator, :allow_identical_variable_names => allow_identical_variable_names, :detailed_failure => true}
  usher_options.merge!(options)
  @router = Usher.new(usher_options)
  @router.route_class = Rack::Route

  instance_eval(&blk) if blk
end

Instance Attribute Details

#redirect_on_trailing_delimitersObject

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

#routerObject (readonly)

Returns the value of attribute router.



14
15
16
# File 'lib/usher/interface/rack.rb', line 14

def router
  @router
end

#router_keyObject (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, 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



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_appObject



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, options = {})
  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.


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

#dupObject

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, options = nil)
  @router.generator.generate(route, options)
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, options = {})
  add(path, options.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, options = {})
  add(path, options.merge!(:conditions => {:request_method => ["GET"]}))
end

#parent_routeObject



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, options = {})
  add(path, options.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, options = {})
  add(path, options.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.

Returns:

  • (Boolean)


45
46
47
# File 'lib/usher/interface/rack.rb', line 45

def use_destinations?
  @use_destinations
end