Module: Roda::RodaPlugins::Json
- Defined in:
- lib/roda/plugins/json.rb
Overview
The json plugin allows match blocks to return arrays or hashes, and have those arrays or hashes be converted to json which is used as the response body. It also sets the response content type to application/json. So you can take code like:
r.root do
response['Content-Type'] = 'application/json'
[1, 2, 3].to_json
end
r.is "foo" do
response['Content-Type'] = 'application/json'
{'a'=>'b'}.to_json
end
and DRY it up:
plugin :json
r.root do
[1, 2, 3]
end
r.is "foo" do
{'a'=>'b'}
end
By default, only arrays and hashes are handled, but you can specifically set the allowed classes to json by adding using the :classes option when loading the plugin:
plugin :json, :classes=>[Array, Hash, Sequel::Model]
Defined Under Namespace
Modules: ClassMethods, RequestMethods
Class Method Summary collapse
-
.configure(app, opts = {}) ⇒ Object
Set the classes to automatically convert to JSON.
Class Method Details
.configure(app, opts = {}) ⇒ Object
Set the classes to automatically convert to JSON
37 38 39 40 41 42 43 |
# File 'lib/roda/plugins/json.rb', line 37 def self.configure(app, opts={}) classes = opts[:classes] || [Array, Hash] app.opts[:json_result_classes] ||= [] app.opts[:json_result_classes] += classes app.opts[:json_result_classes].uniq! app.opts[:json_result_classes].extend(RodaDeprecateMutation) end |