Class: Vayacondios::Rack::JSONize

Inherits:
Object
  • Object
show all
Includes:
Goliath::Rack::AsyncMiddleware
Defined in:
lib/vayacondios/server/rack/jsonize.rb

Overview

Sets the Content-Type of the request and response to application/json.

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object

Sets the Content-Type header to application/json so downstream parameter parsing will work correctly.

Parameters:

  • env (Hash)

    the request environment



13
14
15
16
17
# File 'lib/vayacondios/server/rack/jsonize.rb', line 13

def call(env)
  env.logger.debug("Started #{env[Goliath::Request::REQUEST_METHOD]} \"#{env[Goliath::Request::REQUEST_PATH]}\" for #{env[Goliath::Request::REMOTE_ADDR]} as JSON")
  env['CONTENT_TYPE'] = 'application/json'
  super(env)
end

#post_process(env, status, headers, body) ⇒ Array

Sets the Content-Type header to application/json and serializes the response body to a JSON string.

Will not do this if the request was sent to /status which is expected to just return the string OK.

As per Infomart, the client expects an empty string when there is no body to return. This does not conform to proper JSON spec and will be an improper response for most other users.

Parameters:

  • env (Hash)

    the request environment

  • status (Integer)

    the HTTP status code of the response

  • headers (Hash)

    the HTTP headers of the response

  • body (Object)

    the upstream response body

Returns:

  • (Array)

    the response



34
35
36
37
38
39
# File 'lib/vayacondios/server/rack/jsonize.rb', line 34

def post_process(env, status, headers, body)
  return [status, headers, body] if env["REQUEST_PATH"] == '/status'
  headers['Content-Type'] = 'application/json'
  body = body.nil? ? [''] : [MultiJson.encode(body)]
  [status, headers, body]
end