Class: Flipper::Api::JsonParams

Inherits:
Object
  • Object
show all
Includes:
Rack::Utils
Defined in:
lib/flipper/api/json_params.rb

Constant Summary collapse

CONTENT_TYPE =
'CONTENT_TYPE'.freeze
QUERY_STRING =
'QUERY_STRING'.freeze
REQUEST_BODY =
'rack.input'.freeze
REWIND_BODY =
Gem::Version.new(Rack.release) < Gem::Version.new('3.0.0')

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ JsonParams

Returns a new instance of JsonParams.



8
9
10
# File 'lib/flipper/api/json_params.rb', line 8

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object

Public: Merge request body params with query string params This way can access all params with Rack::Request#params Rack does not add application/json params to Rack::Request#params Allows app to handle x-www-url-form-encoded / application/json request parameters the same way



22
23
24
25
26
27
28
29
# File 'lib/flipper/api/json_params.rb', line 22

def call(env)
  if env[CONTENT_TYPE] == 'application/json'
    body = env[REQUEST_BODY].read
    env[REQUEST_BODY].rewind if REWIND_BODY
    update_params(env, body)
  end
  @app.call(env)
end