lib/deas-json/view_handler.rb in deas-json-0.2.0 vs lib/deas-json/view_handler.rb in deas-json-0.3.0

- old
+ new

@@ -1,41 +1,36 @@ require 'deas/view_handler' +require 'much-plugin' module Deas::Json module ViewHandler + include MuchPlugin - DEF_STATUS = nil + DEF_STATUS = 200.freeze DEF_HEADERS = {}.freeze DEF_BODY = '{}'.freeze - def self.included(klass) - klass.class_eval do - include Deas::ViewHandler - include InstanceMethods - end + plugin_included do + include Deas::ViewHandler + include InstanceMethods + + before_init{ content_type('.json', 'charset' => 'utf-8') } end module InstanceMethods - def initialize(*args) - super(*args) - content_type :json - end - private # Some http clients will error when trying to parse an empty body when the # content type is 'json'. This will default the body to a string that # can be parsed to an empty json object def halt(*args) - super(DEF_STATUS, DEF_HEADERS, DEF_BODY) if args.empty? - body, headers, status = [ - !args.last.kind_of?(::Hash) && !args.last.kind_of?(::Integer) ? args.pop : DEF_BODY, - args.last.kind_of?(::Hash) ? args.pop : DEF_HEADERS, - args.first.kind_of?(::Integer) ? args.first : DEF_STATUS - ] - super(status, headers, body) + super( + args.first.instance_of?(::Fixnum) ? args.shift : DEF_STATUS, + args.first.kind_of?(::Hash) ? args.shift : DEF_HEADERS, + args.first.respond_to?(:each) ? args.shift : DEF_BODY + ) end end end