Class: TentD::API::Router::SerializeResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/tentd/api/router/serialize_response.rb

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/tentd/api/router/serialize_response.rb', line 7

def call(env)
  response = if env.response
    env.response.kind_of?(String) ? env.response : env.response.to_json(serialization_options(env))
  end
  status = env['response.status'] || (response ? 200 : 404)
  headers = if env['response.type'] || status == 200 && response && !response.empty?
              { 'Content-Type' => env['response.type'] || MEDIA_TYPE } 
            else
              {}
            end
  headers.merge!('Access-Control-Allow-Origin' => '*') if env['HTTP_ORIGIN']
  [status, headers, [response.to_s]]
end