lib/angelo/responder.rb in angelo-0.1.10 vs lib/angelo/responder.rb in angelo-0.1.11

- old
+ new

@@ -45,18 +45,19 @@ end def request= request @params = nil @redirect = nil + @body = nil @request = request handle_request end def handle_request if @response_handler @base.before if @base.respond_to? :before - @body = @response_handler.bind(@base).call || '' + @body = catch(:halt) { @response_handler.bind(@base).call || '' } @base.after if @base.respond_to? :after respond else raise NotImplementedError end @@ -120,21 +121,31 @@ type == :html end end def respond - @body = case @body - when String - JSON.parse @body if respond_with? :json # for the raises - @body - when Hash - raise 'html response requires String' if respond_with? :html - @body.to_json if respond_with? :json - when NilClass - EMPTY_STRING - end + status = nil + case @body + when HALT_STRUCT + status = @body.status + @body = @body.body + if Hash === @body + @body = {error: @body} if status != :ok or status < 200 && status >= 300 + @body = @body.to_json if respond_with? :json + end - status = @redirect.nil? ? :ok : :moved_permanently + when String + JSON.parse @body if respond_with? :json # for the raises + + when Hash + raise 'html response requires String' if respond_with? :html + @body = @body.to_json if respond_with? :json + + when NilClass + @body = EMPTY_STRING + end + + status ||= @redirect.nil? ? :ok : :moved_permanently headers LOCATION_HEADER_KEY => @redirect if @redirect Angelo.log @connection, @request, nil, status, @body.size @connection.respond status, headers, @body rescue => e