Sha256: a29507485ada42c0bcb21562c4f67467659e352eebb4cce3e95cae7aebf96bd2

Contents?: true

Size: 1.47 KB

Versions: 3

Compression:

Stored size: 1.47 KB

Contents

module Lookout::Rack::Utils
  module Subroute
    HTTP_METHODS = %w(DELETE GET HEAD OPTIONS LINK PATCH POST PUT TRACE UNLINK).freeze

    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, body.first]
    end

    # Returns true if the status given is 20x
    #
    # @param [Integer] status
    def succeeded?(status)
      status.is_a?(Fixnum) && (200..299).include?(status)
    end

    # Returns false if the status given is 20x
    #
    # @param [Integer] status
    def failed?(status)
      !succeeded?(status)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lookout-rack-utils-1.7.3 lib/lookout/rack/utils/subroute.rb
lookout-rack-utils-1.7.2 lib/lookout/rack/utils/subroute.rb
lookout-rack-utils-1.7.1 lib/lookout/rack/utils/subroute.rb