Class: RSence::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/rsence/http/response.rb

Overview

Classic WEBrick -compatible Response object for Rack. Implements only the methods used by the framework.

Defined Under Namespace

Classes: ResponseBody

Instance Method Summary collapse

Constructor Details

#initializeResponse

Returns a new instance of Response.


39
40
41
42
43
44
45
46
47
# File 'lib/rsence/http/response.rb', line 39

def initialize
  @body = ResponseBody.new(1)
  @body[0] = ''
  @status = 200
  @header = {
    'Content-Type' => 'text/plain',
    'Server' => 'RSence'
  }
end

Instance Method Details

#[]=(header_key, header_val) ⇒ Object


75
76
77
# File 'lib/rsence/http/response.rb', line 75

def []=(header_key,header_val)
  @header[camelize( header_key )] = header_val.to_s
end

#bodyObject


52
53
54
# File 'lib/rsence/http/response.rb', line 52

def body
  @body
end

#body=(body_data) ⇒ Object


48
49
50
51
# File 'lib/rsence/http/response.rb', line 48

def body=(body_data)
  @body = ResponseBody.new(1)
  @body[0] = @body.sanitize_body_data( body_data )
end

#body_bytesObject


55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rsence/http/response.rb', line 55

def body_bytes
  bytes = 0
  @body.each do |part|
    if part.respond_to?(:to_s)
      bytes += part.to_s.bytesize
    else
      warn "body data part does not respond to to_s: #{part.inspect[0..100]}"
    end
  end
  return bytes.to_s
end

#camelize(header_key) ⇒ Object


72
73
74
# File 'lib/rsence/http/response.rb', line 72

def camelize( header_key )
  header_key.capitalize.gsub(/\-([a-z])/) { '-'+$1.upcase }
end

#content_typeObject


69
70
71
# File 'lib/rsence/http/response.rb', line 69

def content_type
  @header['Content-Type']
end

#content_type=(new_content_type) ⇒ Object


66
67
68
# File 'lib/rsence/http/response.rb', line 66

def content_type=(new_content_type)
  @header['Content-Type'] = new_content_type
end

#headerObject


84
85
86
# File 'lib/rsence/http/response.rb', line 84

def header
  @header
end

#statusObject


81
82
83
# File 'lib/rsence/http/response.rb', line 81

def status
  @status
end

#status=(new_val) ⇒ Object


78
79
80
# File 'lib/rsence/http/response.rb', line 78

def status=(new_val)
  @status = new_val.to_i
end