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, reason = nil, headers = [], body = nil, protocol = nil) ⇒ Response

Returns a new instance of Response.



29
30
31
32
33
34
35
36
# File 'lib/protocol/http/response.rb', line 29

def initialize(version = nil, status = 200, reason = nil, headers = [], body = nil, protocol = nil)
	@version = version
	@status = status
	@reason = reason
	@headers = headers
	@body = body
	@protocol = protocol
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



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

def body
  @body
end

#headersObject

Returns the value of attribute headers.



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

def headers
  @headers
end

#protocolObject

Returns the value of attribute protocol.



42
43
44
# File 'lib/protocol/http/response.rb', line 42

def protocol
  @protocol
end

#statusObject

Returns the value of attribute status.



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

def status
  @status
end

#versionObject

Returns the value of attribute version.



38
39
40
# File 'lib/protocol/http/response.rb', line 38

def version
  @version
end

Class Method Details

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



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

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

.for_exception(exception) ⇒ Object



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

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)


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

def bad_request?
	status == 400
end

#continue?Boolean

Returns:

  • (Boolean)


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

def continue?
	status == 100
end

#failure?Boolean

Returns:

  • (Boolean)


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

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

#hijack?Boolean

Returns:

  • (Boolean)


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

def hijack?
	false
end

#partial?Boolean

Returns:

  • (Boolean)


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

def partial?
	status == 206
end

#preserve_method?Boolean

Returns:

  • (Boolean)


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

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

#redirection?Boolean

Returns:

  • (Boolean)


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

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

#server_failure?Boolean

Returns:

  • (Boolean)


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

def server_failure?
	status == 500
end

#success?Boolean

Returns:

  • (Boolean)


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

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

#to_aryObject



94
95
96
# File 'lib/protocol/http/response.rb', line 94

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

#to_sObject



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

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