Module: Utopia::Controller::Respond::Converter::ToJSON

Defined in:
lib/utopia/controller/respond.rb

Overview

To accept incoming requests with content-type JSON (e.g. POST with JSON data), consider using Rack::PostBodyContentTypeParser.

Constant Summary collapse

APPLICATION_JSON =
'application/json'.freeze
HEADERS =
{HTTP::CONTENT_TYPE => APPLICATION_JSON}.freeze

Class Method Summary collapse

Class Method Details

.call(context, response, media_range) ⇒ Object



80
81
82
83
84
# File 'lib/utopia/controller/respond.rb', line 80

def self.call(context, response, media_range)
  Converter.update_response(response, HEADERS) do |content|
    self.serialize(content, media_range)
  end
end

.content_type ⇒ Object



66
67
68
# File 'lib/utopia/controller/respond.rb', line 66

def self.content_type
  APPLICATION_JSON
end

.serialize(content, media_range) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/utopia/controller/respond.rb', line 70

def self.serialize(content, media_range)
  options = {}
  
  if version = media_range.parameters['version']
    options[:version] = version.to_s
  end
  
  return content.to_json(options)
end