Sha256: 8d5edba01e567b886c5097efe35d5dc3fe9c815c28b9ced0e9fa88e608bdde07

Contents?: true

Size: 1.15 KB

Versions: 59

Compression:

Stored size: 1.15 KB

Contents

# frozen-string-literal: true

#
class Roda
  module RodaPlugins
    # The default_status plugin accepts a block which should
    # return a response status integer. This integer will be used as
    # the default response status (usually 200) if the body has been
    # written to, and you have not explicitly set a response status.
    #
    # Example:
    #
    #   # Use 201 default response status for all requests
    #   plugin :default_status do
    #     201
    #   end
    module DefaultStatus
      def self.configure(app, &block)
        raise RodaError, "default_status plugin requires a block" unless block
        if check_arity = app.opts.fetch(:check_arity, true)
          unless block.arity == 0
            if check_arity == :warn
              RodaPlugins.warn "Arity mismatch in block passed to plugin :default_status. Expected Arity 0, but arguments required for #{block.inspect}"
            end
            b = block
            block = lambda{instance_exec(&b)} # Fallback
          end
        end
        app::RodaResponse.send(:define_method, :default_status, &block)
      end
    end

    register_plugin(:default_status, DefaultStatus)
  end
end

Version data entries

59 entries across 59 versions & 1 rubygems

Version Path
roda-3.86.0 lib/roda/plugins/default_status.rb
roda-3.85.0 lib/roda/plugins/default_status.rb
roda-3.84.0 lib/roda/plugins/default_status.rb
roda-3.83.0 lib/roda/plugins/default_status.rb
roda-3.82.0 lib/roda/plugins/default_status.rb
roda-3.81.0 lib/roda/plugins/default_status.rb
roda-3.79.0 lib/roda/plugins/default_status.rb
roda-3.78.0 lib/roda/plugins/default_status.rb
roda-3.77.0 lib/roda/plugins/default_status.rb
roda-3.76.0 lib/roda/plugins/default_status.rb
roda-3.75.0 lib/roda/plugins/default_status.rb
roda-3.74.0 lib/roda/plugins/default_status.rb
roda-3.73.0 lib/roda/plugins/default_status.rb
roda-3.72.0 lib/roda/plugins/default_status.rb
roda-3.71.0 lib/roda/plugins/default_status.rb
roda-3.70.0 lib/roda/plugins/default_status.rb
roda-3.69.0 lib/roda/plugins/default_status.rb
roda-3.68.0 lib/roda/plugins/default_status.rb
roda-3.67.0 lib/roda/plugins/default_status.rb
roda-3.66.0 lib/roda/plugins/default_status.rb