lib/sinatra/json.rb in sinatra-contrib-2.2.4 vs lib/sinatra/json.rb in sinatra-contrib-3.0.0

- old
+ new

@@ -1,9 +1,10 @@ +# frozen_string_literal: true + require 'sinatra/base' require 'multi_json' module Sinatra - # = Sinatra::JSON # # <tt>Sinatra::JSON</tt> adds a helper method, called +json+, for (obviously) # json generation. # @@ -93,11 +94,11 @@ end end def json(object, options = {}) content_type resolve_content_type(options) - resolve_encoder_action object, resolve_encoder(options) + resolve_encoder_action object, resolve_encoder(options) end private def resolve_content_type(options = {}) @@ -107,19 +108,17 @@ def resolve_encoder(options = {}) options[:json_encoder] || settings.json_encoder end def resolve_encoder_action(object, encoder) - [:encode, :generate].each do |method| + %i[encode generate].each do |method| return encoder.send(method, object) if encoder.respond_to? method end - if encoder.is_a? Symbol - object.__send__(encoder) - else - fail "#{encoder} does not respond to #generate nor #encode" - end #if - end #resolve_encoder_action - end #JSON + raise "#{encoder} does not respond to #generate nor #encode" unless encoder.is_a? Symbol + + object.__send__(encoder) + end + end Base.set :json_encoder do ::MultiJson end