Class: Goliath::Rack::Validation::DefaultParams
- Inherits:
-
Object
- Object
- Goliath::Rack::Validation::DefaultParams
- Defined in:
- lib/goliath/rack/validation/default_params.rb
Overview
A middleware to validate that a parameter always has a value
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, opts = {}) ⇒ Goliath::Rack::Validation::DefaultParams
constructor
Called by the framework to create the validator.
Constructor Details
#initialize(app, opts = {}) ⇒ Goliath::Rack::Validation::DefaultParams
Called by the framework to create the validator
17 18 19 20 21 22 23 24 |
# File 'lib/goliath/rack/validation/default_params.rb', line 17 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 |
Instance Method Details
#call(env) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/goliath/rack/validation/default_params.rb', line 26 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 |