Class: Lydia::Response

Inherits:
Rack::Response
  • Object
show all
Defined in:
lib/lydia/response.rb

Instance Method Summary collapse

Constructor Details

#initializeResponse

Returns a new instance of Response.



6
7
8
9
# File 'lib/lydia/response.rb', line 6

def initialize(*)
  super
  headers['Content-Type'] = 'text/html' if headers['Content-Type'].nil?
end

Instance Method Details

#build(input) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/lydia/response.rb', line 11

def build(input)
  case input.class.to_s
    when 'String'
      write input
    when 'Array'
      @status = input.first
      write input.last.is_a?(Array) ? input.last[0] : input.last
      headers.merge!(input[1]) if input.count == 3        
    when 'Fixnum'
      @status = input
    when 'Hash'
      headers['Content-Type'] = 'application/json'
      write input.to_json        
    else
      if input.respond_to?(:each)
        write input
      else  
        raise ArgumentError.new("#{input.class} is not a valid allowed return type")
      end
  end
  finish
end

#finish(&block) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/lydia/response.rb', line 34

def finish(&block)
  @block = block

  if [204, 205, 304].include?(status.to_i)
    headers.delete 'Content-Length'
    headers.delete 'Content-Type'
    close
    [status.to_i, header, []]
  else
    [status.to_i, header, @body]
  end
end