Sha256: 611af8b6f46dab9ed43dfe66918a252114934e79d3420844a33b1a90848eac0d

Contents?: true

Size: 1.47 KB

Versions: 3

Compression:

Stored size: 1.47 KB

Contents

module Crystal
  module Processors    
    class HttpWriter < Processor
      attr_accessor :result_variable
      
      def initialize next_processor, result_variable = 'content'
        super(next_processor)
        @result_variable = result_variable
      end
      
      def call
        response = workspace.response.must_be.defined
        
        begin
          next_processor.call
          
          result = workspace[result_variable]
          response.body = result if response.body.blank? and result.present?
          response.content_type ||= Mime[workspace.params.format]
        rescue StandardError => e            
          raise e if config.test?
          
          # if workspace.error_processed?
          #   result = workspace[@result_variable]
          # 
          #   response.body << result if result
          #   response.content_type = Mime[workspace.params.format]
          # else
          
          if workspace.params.format? and workspace.params.format.json?
            response.body << {:error => (config.production? ? "Internal error!" : e.message)}.to_json
            response.content_type = Mime.json
          else
            response.body << (config.production? ? "Internal error!" : e.message)
            response.content_type = Mime.text
          end
          
          logger.error e
          logger.info "\n"
          
          # end
          
          # response.error = e # for RSpec helpers
        end
      end
    end 
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
crystal-0.0.13 lib/crystal/http/processors/http_writer.rb
crystal-0.0.12 lib/crystal/http/processors/http_writer.rb
crystal_ext-0.0.11 lib/crystal/http/processors/http_writer.rb