Class: Lennarb::Response

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

Overview

Response object

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResponse

Initialize the response object



40
41
42
43
44
45
# File 'lib/lennarb/response.rb', line 40

def initialize
  @status = 200
  @headers = {}
  @body = []
  @length = 0
end

Instance Attribute Details

#bodyArray (readonly)

Returns:

  • (Array)


13
14
15
# File 'lib/lennarb/response.rb', line 13

def body
  @body
end

#headersHash (readonly)

Returns:

  • (Hash)


18
19
20
# File 'lib/lennarb/response.rb', line 18

def headers
  @headers
end

#lengthInteger (readonly)

Returns:

  • (Integer)


23
24
25
# File 'lib/lennarb/response.rb', line 23

def length
  @length
end

#statusInteger

Returns:

  • (Integer)


8
9
10
# File 'lib/lennarb/response.rb', line 8

def status
  @status
end

Instance Method Details

#[](key) ⇒ String

Set the response header

Parameters:

  • key (String)

Returns:

  • (String)

    value



53
54
55
# File 'lib/lennarb/response.rb', line 53

def [](key)
  @headers[key]
end

#[]=(key, value) ⇒ String

Get the response header

Parameters:

  • key (String)
  • value (String)

Returns:

  • (String)

    value



64
65
66
# File 'lib/lennarb/response.rb', line 64

def []=(key, value)
  @headers[key] = value
end

#finishArray

Finish the response

Returns:

  • (Array)

    response



141
142
143
# File 'lib/lennarb/response.rb', line 141

def finish
  [@status, @headers, @body]
end

#html(str) ⇒ String

Set the response type to html

Parameters:

  • str (String)

Returns:

  • (String)

    str



98
99
100
101
# File 'lib/lennarb/response.rb', line 98

def html(str)
  @headers[CONTENT_TYPE] = Lennarb::CONTENT_TYPE[:HTML]
  write(str)
end

#json(str) ⇒ String

Set the response type to json

Parameters:

  • str (String)

Returns:

  • (String)

    str



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/lennarb/response.rb', line 109

def json(str)
  json_str = JSON.generate(str)
  @headers[CONTENT_TYPE] = Lennarb::CONTENT_TYPE[:JSON]
  write(json_str)
# Rescues JSON::JSONError rather than JSON::GeneratorError: a circular or
# over-deep object graph raises JSON::NestingError, which descends from
# ParserError, not GeneratorError, and so escaped this rescue entirely.
#
# The body is static because the exception message can carry `inspect`
# output of the object being serialized, which may hold credentials.
rescue JSON::JSONError
  @status = 500
  @headers[CONTENT_TYPE] = Lennarb::CONTENT_TYPE[:TEXT]
  write("JSON generation error")
end

#redirect(path, status = 302) ⇒ Object

Redirect the response

Parameters:

  • path (String)
  • status, (Integer)

    default: 302



130
131
132
133
134
135
# File 'lib/lennarb/response.rb', line 130

def redirect(path, status = 302)
  @headers[LOCATION] = path
  @status = status

  throw :halt, finish
end

#text(str) ⇒ String

Set the response type to text

Parameters:

  • str (String)

Returns:

  • (String)

    str



87
88
89
90
# File 'lib/lennarb/response.rb', line 87

def text(str)
  @headers[CONTENT_TYPE] = Lennarb::CONTENT_TYPE[:TEXT]
  write(str)
end

#write(str) ⇒ String

Write to the response body

Parameters:

  • str (String)

Returns:

  • (String)

    str



74
75
76
77
78
79
# File 'lib/lennarb/response.rb', line 74

def write(str)
  str = str.to_s
  @length += str.bytesize
  @headers[CONTENT_LENGTH] = @length.to_s
  @body << str
end