Module: Lookout::Rack::Utils::Subroute
- Defined in:
- lib/lookout/rack/utils/subroute.rb
Constant Summary collapse
- HTTP_METHODS =
%w(DELETE GET HEAD OPTIONS LINK PATCH POST PUT TRACE UNLINK).freeze
Instance Method Summary collapse
-
#failed?(status) ⇒ Boolean
Returns false if the status given is 20x.
- #subroute!(relative_path, options = {}) ⇒ Object
-
#succeeded?(status) ⇒ Boolean
Returns true if the status given is 20x.
Instance Method Details
#failed?(status) ⇒ Boolean
Returns false if the status given is 20x
36 37 38 |
# File 'lib/lookout/rack/utils/subroute.rb', line 36 def failed?(status) !succeeded?(status) end |
#subroute!(relative_path, options = {}) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/lookout/rack/utils/subroute.rb', line 5 def subroute!(relative_path, ={}) # Create a copy of our App instance to preserve the state of the # caller's env hash subserver = dup request_opts = {'PATH_INFO' => relative_path} request_opts['REQUEST_METHOD'] = .delete(:request_method).upcase if [:request_method] http_verb = request_opts['REQUEST_METHOD'] || subserver.request.request_method raise ArgumentError, "Invalid http method: #{http_verb}" unless HTTP_METHODS.include?(http_verb) # modify rack environment using Rack::Request- store passed in key/value # pairs into hash associated with the parameters of the current http verb .each { |k,v| subserver.request.update_param(k, v) } # Invoking Sinatra::Base#call! on our duplicated app instance. Sinatra's # call will dup the app instance and then call!, so skip Sinatra's dup # since we've done that here. subcode, subheaders, body = subserver.call!(env.merge(request_opts)) return [subcode, subheaders, body.first] end |
#succeeded?(status) ⇒ Boolean
Returns true if the status given is 20x
29 30 31 |
# File 'lib/lookout/rack/utils/subroute.rb', line 29 def succeeded?(status) status.is_a?(Fixnum) && (200..299).include?(status) end |