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 =
HTTP::Accept::ContentType.new('application', 'json', charset: 'utf-8').freeze
HEADERS =
{HTTP::CONTENT_TYPE => APPLICATION_JSON.to_s}.freeze

Class Method Summary collapse

Class Method Details

.call(context, response, media_range) ⇒ Object



90
91
92
93
94
# File 'lib/utopia/controller/respond.rb', line 90

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

.content_typeObject



72
73
74
# File 'lib/utopia/controller/respond.rb', line 72

def self.content_type
	APPLICATION_JSON
end

.serialize(content, media_range) ⇒ Object



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

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

.split(*args) ⇒ Object



76
77
78
# File 'lib/utopia/controller/respond.rb', line 76

def self.split(*args)
	self.content_type.split(*args)
end