module Roda::RodaPlugins::Json
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 automatically convert other types to json by adding them to json_result_classes:
plugin :json json_result_classes << Sequel::Model
Public Class Methods
configure(app)
click to toggle source
Set the classes to automatically convert to JSON
# File lib/roda/plugins/json.rb, line 38 def self.configure(app) app.instance_eval do @json_result_classes ||= [Array, Hash] end end