Class: Hyperdrive::Middleware::RequiredParams

Inherits:
Object
  • Object
show all
Defined in:
lib/hyperdrive/middleware/required_params.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ RequiredParams

Returns a new instance of RequiredParams.



6
7
8
# File 'lib/hyperdrive/middleware/required_params.rb', line 6

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/hyperdrive/middleware/required_params.rb', line 10

def call(env)
  @env = env
  if %w(GET HEAD).include? env['REQUEST_METHOD']
    check_required_params(env['hyperdrive.resource'].filters)
  elsif %W(POST PUT PATCH DELETE).include? env['REQUEST_METHOD']
    check_required_params(env['hyperdrive.resource'].params)
  end
  @app.call(env)
end

#check_required_params(params) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/hyperdrive/middleware/required_params.rb', line 20

def check_required_params(params)
  params.each do |param, options|
    if @env['hyperdrive.resource'].required?(param, @env['REQUEST_METHOD'])
      if @env['hyperdrive.params'].key?(param)
        if @env['hyperdrive.params'][param] == ''
          raise Errors::MissingRequiredParam.new(param, @env['REQUEST_METHOD'])
        end
      else
        raise Errors::MissingRequiredParam.new(param, @env['REQUEST_METHOD'])
      end
    end
  end
end