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
JSON_REQUEST_METHOD =
[:post, :put, :patch]

Constants included from Middleware

Middleware::UNRESERVED

Constants included from RestCore

ASYNC, CLIENT, DRY, FAIL, HIJACK, LOG, PROMISE, REQUEST_HEADERS, REQUEST_METHOD, REQUEST_PATH, REQUEST_PAYLOAD, REQUEST_QUERY, REQUEST_URI, RESPONSE_BODY, RESPONSE_HEADERS, RESPONSE_KEY, RESPONSE_SOCKET, RESPONSE_STATUS, TIMER, Universal, VERSION

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Middleware

contain_binary?, escape, #fail, #id, included, #log, merge_hash, percent_encode, request_uri, #run, string_keys

Methods included from RestCore

eagerload, id

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



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

def call env, &k
  return app.call(env, &k) unless json_request(env)
  return app.call(env, &k) unless
    JSON_REQUEST_METHOD.include?(env[REQUEST_METHOD])

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