lib/roda/plugins/json.rb in roda-2.29.0 vs lib/roda/plugins/json.rb in roda-3.0.0
- old
+ new
@@ -31,48 +31,43 @@
#
# 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]
+ # plugin :json, classes: [Array, Hash, Sequel::Model]
#
# By default objects are serialized with +to_json+, but you
# can pass in a custom serializer, which can be any object
# that responds to +call(object)+.
#
- # plugin :json, :serializer=>proc{|o| o.to_json(root: true)}
+ # plugin :json, serializer: proc{|o| o.to_json(root: true)}
#
# If you need the request information during serialization, such
# as HTTP headers or query parameters, you can pass in the
# +:include_request+ option, which will pass in the request
# object as the second argument when calling the serializer.
#
- # plugin :json, :include_request=>true, :serializer=>proc{|o, request| ...}
+ # plugin :json, include_request: true, serializer: proc{|o, request| ...}
#
# The default content-type is 'application/json', but you can change that
# using the +:content_type+ option:
#
- # plugin :json, :content_type=>'application/xml'
+ # plugin :json, content_type: 'application/xml'
module Json
DEFAULT_SERIALIZER = :to_json.to_proc
- OPTS = {}.freeze
- RodaPlugins.deprecate_constant(self, :OPTS)
- DEFAULT_CONTENT_TYPE = 'application/json'.freeze
- RodaPlugins.deprecate_constant(self, :DEFAULT_CONTENT_TYPE)
-
# Set the classes to automatically convert to JSON, and the serializer to use.
- def self.configure(app, opts=RodaPlugins::OPTS)
+ def self.configure(app, opts=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].freeze
app.opts[:json_result_serializer] = opts[:serializer] || app.opts[:json_result_serializer] || DEFAULT_SERIALIZER
- app.opts[:json_result_include_request] = opts[:include_request] || app.opts[:json_result_include_request]
+ app.opts[:json_result_include_request] = opts[:include_request] if opts.has_key?(:include_request)
app.opts[:json_result_content_type] = opts[:content_type] || 'application/json'.freeze
end
module ClassMethods
@@ -81,12 +76,9 @@
opts[:json_result_classes]
end
end
module RequestMethods
- CONTENT_TYPE = 'Content-Type'.freeze
- RodaPlugins.deprecate_constant(self, :CONTENT_TYPE)
-
private
# If the result is an instance of one of the json_result_classes,
# convert the result to json and return it as the body, using the
# application/json content-type.