Sha256: 8336bfba298bb146b1ac763b6d127eb9fd389a3235cae795698dd2a3ad776a31

Contents?: true

Size: 1.49 KB

Versions: 14

Compression:

Stored size: 1.49 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, subheaders, 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

14 entries across 14 versions & 1 rubygems

Version Path
lookout-rack-utils-5.0.0.49 lib/lookout/rack/utils/subroute.rb
lookout-rack-utils-4.0.0.44 lib/lookout/rack/utils/subroute.rb
lookout-rack-utils-3.8.0.39 lib/lookout/rack/utils/subroute.rb
lookout-rack-utils-3.7.0.37 lib/lookout/rack/utils/subroute.rb
lookout-rack-utils-3.7.0.34 lib/lookout/rack/utils/subroute.rb
lookout-rack-utils-3.3.0.24 lib/lookout/rack/utils/subroute.rb
lookout-rack-utils-3.3.0.22 lib/lookout/rack/utils/subroute.rb
lookout-rack-utils-3.2.0.19 lib/lookout/rack/utils/subroute.rb
lookout-rack-utils-3.2.0.16 lib/lookout/rack/utils/subroute.rb
lookout-rack-utils-3.1.0.12 lib/lookout/rack/utils/subroute.rb
lookout-rack-utils-3.0.1.11 lib/lookout/rack/utils/subroute.rb
lookout-rack-utils-3.0.1 lib/lookout/rack/utils/subroute.rb
lookout-rack-utils-3.0.0 lib/lookout/rack/utils/subroute.rb
lookout-rack-utils-2.0.0 lib/lookout/rack/utils/subroute.rb