Class: RestCore::JsonRequest

Inherits:
Object
  • Object
show all
Includes:
Middleware
Defined in:
lib/rest-core/middleware/json_request.rb

Constant Summary

JSON_REQUEST_HEADER =
{'Content-Type' => 'application/json'}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.membersObject



6
# File 'lib/rest-core/middleware/json_request.rb', line 6

def self.members; [:json_request]; end

Instance Method Details

#call(env, &k) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/rest-core/middleware/json_request.rb', line 11

def call env, &k
  return app.call(env, &k) unless json_request(env)
  return app.call(env, &k) unless has_payload?(env)

  headers = env[REQUEST_HEADERS] || {}
  app.call(env.merge(
    REQUEST_HEADERS => JSON_REQUEST_HEADER.merge(headers),
    REQUEST_PAYLOAD => Json.encode(env[REQUEST_PAYLOAD])), &k)
end