Class: Crystal::Processors::HttpWriter

Inherits:
Crystal::Processor show all
Defined in:
lib/crystal/http/processors/http_writer.rb

Instance Attribute Summary collapse

Attributes inherited from Crystal::Processor

#next_processor

Instance Method Summary collapse

Methods inherited from Crystal::Processor

inspect

Constructor Details

#initialize(next_processor, result_variable = 'content') ⇒ HttpWriter

Returns a new instance of HttpWriter.



6
7
8
9
# File 'lib/crystal/http/processors/http_writer.rb', line 6

def initialize next_processor, result_variable = 'content'
  super(next_processor)
  @result_variable = result_variable
end

Instance Attribute Details

#result_variableObject

Returns the value of attribute result_variable.



4
5
6
# File 'lib/crystal/http/processors/http_writer.rb', line 4

def result_variable
  @result_variable
end

Instance Method Details

#callObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/crystal/http/processors/http_writer.rb', line 11

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