Sha256: dd8172b66d8c18f572edb79e3727e13eb494423c04d57625a757d8f21c0828f6

Contents?: true

Size: 1.5 KB

Versions: 2

Compression:

Stored size: 1.5 KB

Contents

require 'goliath/rack/validation_error'

module Goliath
  module Rack
    module Validation
      # A middleware to validate that a parameter always has a value
      #
      # @example
      #  use Goliath::Rack::Validation::DefaultParams, {:key => 'order', :defaults => 'pubdate'}
      #
      class DefaultParams
        # Called by the framework to create the validator
        #
        # @param app The app object
        # @param opts [Hash] The options hash
        # @option opts [String] :key The key to access in the parameters
        # @option opts :defaults The default value to assign if the key is empty or non-existant
        # @return [Goliath::Rack::Validation::DefaultParams] The validator
        def initialize(app, opts = {})
          @app = app
          @defaults = opts[:defaults]
          raise Exception.new("Must provide defaults to DefaultParams") if @defaults.nil?

          @key = opts[:key]
          raise Exception.new("must provide key to DefaultParams") if @key.nil? || @key =~ /^\s*$/
        end

        def call(env)
          if !env['params'].has_key?(@key) || env['params'][@key].nil?
            env['params'][@key] = @defaults

          elsif env['params'][@key].is_a?(Array) && env['params'][@key].empty?
            env['params'][@key] = @defaults

          elsif env['params'][@key].is_a?(String)
            if env['params'][@key] =~ /^\s*$/
              env['params'][@key] = @defaults
            end
          end

          @app.call(env)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
goliath-0.9.1 lib/goliath/rack/validation/default_params.rb
goliath-0.9.0 lib/goliath/rack/validation/default_params.rb