Class: Protocol::HTTP::Response

Inherits:
Object
  • Object
show all
Includes:
Body::Reader
Defined in:
lib/protocol/http/response.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Body::Reader

#body?, #close, #each, #finish, #read, #save

Constructor Details

#initialize(version = nil, status = 200, headers = Headers.new, body = nil, protocol = nil) ⇒ Response

Returns a new instance of Response.



14
15
16
17
18
19
20
# File 'lib/protocol/http/response.rb', line 14

def initialize(version = nil, status = 200, headers = Headers.new, body = nil, protocol = nil)
	@version = version
	@status = status
	@headers = headers
	@body = body
	@protocol = protocol
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



25
26
27
# File 'lib/protocol/http/response.rb', line 25

def body
  @body
end

#headersObject

Returns the value of attribute headers.



24
25
26
# File 'lib/protocol/http/response.rb', line 24

def headers
  @headers
end

#protocolObject

Returns the value of attribute protocol.



26
27
28
# File 'lib/protocol/http/response.rb', line 26

def protocol
  @protocol
end

#statusObject

Returns the value of attribute status.



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

def status
  @status
end

#versionObject

Returns the value of attribute version.



22
23
24
# File 'lib/protocol/http/response.rb', line 22

def version
  @version
end

Class Method Details

.[](status, headers = nil, body = nil, protocol = nil) ⇒ Object



75
76
77
78
79
80
# File 'lib/protocol/http/response.rb', line 75

def self.[](status, headers = nil, body = nil, protocol = nil)
	body = Body::Buffered.wrap(body)
	headers = ::Protocol::HTTP::Headers[headers]
	
	self.new(nil, status, headers, body, protocol)
end

.for_exception(exception) ⇒ Object



82
83
84
# File 'lib/protocol/http/response.rb', line 82

def self.for_exception(exception)
	Response[500, Headers['content-type' => 'text/plain'], ["#{exception.class}: #{exception.message}"]]
end

Instance Method Details

#bad_request?Boolean

Returns:

  • (Boolean)


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

def bad_request?
	@status == 400
end

#continue?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/protocol/http/response.rb', line 32

def continue?
	@status == 100
end

#failure?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/protocol/http/response.rb', line 60

def failure?
	@status and @status >= 400 && @status < 600
end

#hijack?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/protocol/http/response.rb', line 28

def hijack?
	false
end

#internal_server_error?Boolean Also known as: server_failure?

Returns:

  • (Boolean)


68
69
70
# File 'lib/protocol/http/response.rb', line 68

def internal_server_error?
	@status == 500
end

#not_modified?Boolean

Returns:

  • (Boolean)


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

def not_modified?
	@status == 304
end

#ok?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/protocol/http/response.rb', line 36

def ok?
	@status == 200
end

#partial?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/protocol/http/response.rb', line 44

def partial?
	@status == 206
end

#preserve_method?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/protocol/http/response.rb', line 56

def preserve_method?
	@status == 307 || @status == 308
end

#redirection?Boolean

Returns:

  • (Boolean)


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

def redirection?
	@status and @status >= 300 && @status < 400
end

#success?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/protocol/http/response.rb', line 40

def success?
	@status and @status >= 200 && @status < 300
end

#to_aryObject



90
91
92
# File 'lib/protocol/http/response.rb', line 90

def to_ary
	return @status, @headers, @body
end

#to_sObject



86
87
88
# File 'lib/protocol/http/response.rb', line 86

def to_s
	"#{@status} #{@version}"
end