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

Instance Method Details

#failed?(status) ⇒ Boolean

Returns false if the status given is 20x

Parameters:

  • status (Integer)

Returns:

  • (Boolean)


36
37
38
# File 'lib/lookout/rack/utils/subroute.rb', line 36

def failed?(status)
  !succeeded?(status)
end

#subroute!(relative_path, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


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, options={})
  # 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'] = options.delete(:request_method).upcase if options[: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
  options.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

Parameters:

  • status (Integer)

Returns:

  • (Boolean)


29
30
31
# File 'lib/lookout/rack/utils/subroute.rb', line 29

def succeeded?(status)
  status.is_a?(Fixnum) && (200..299).include?(status)
end