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



96
97
98
99
100
101
# File 'lib/protocol/http/response.rb', line 96

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



103
104
105
# File 'lib/protocol/http/response.rb', line 103

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

Instance Method Details

#bad_request?Boolean

Whether the status is 400 (bad request).

Returns:

  • (Boolean)


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

def bad_request?
  @status == 400
end

#continue?Boolean

Whether the status is 100 (continue).

Returns:

  • (Boolean)


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

def continue?
  @status == 100
end

#failure?Boolean

Whether the status is considered a failure.

Returns:

  • (Boolean)


79
80
81
# File 'lib/protocol/http/response.rb', line 79

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

#final?Boolean

Whether the status is considered final. Note that 101 is considered final.

Returns:

  • (Boolean)


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

def final?
  # 101 is effectively a final status.
  @status and @status >= 200 || @status == 101
end

#hijack?Boolean

Returns:

  • (Boolean)


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

def hijack?
  false
end

#informational?Boolean

Whether the status is considered informational.

Returns:

  • (Boolean)


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

def informational?
  @status and @status >= 100 && @status < 200
end

#internal_server_error?Boolean Also known as: server_failure?

Whether the status is 500 (internal server error).

Returns:

  • (Boolean)


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

def internal_server_error?
  @status == 500
end

#not_modified?Boolean

Whether the status is 304 (not modified).

Returns:

  • (Boolean)


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

def not_modified?
  @status == 304
end

#ok?Boolean

Whether the status is 200 (ok).

Returns:

  • (Boolean)


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

def ok?
  @status == 200
end

#partial?Boolean

Whether the status is 206 (partial content).

Returns:

  • (Boolean)


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

def partial?
  @status == 206
end

#preserve_method?Boolean

Whether the status is 307 (temporary redirect) and should preserve the method of the request when following the redirect.

Returns:

  • (Boolean)


74
75
76
# File 'lib/protocol/http/response.rb', line 74

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

#redirection?Boolean

Whether the status is considered a redirection.

Returns:

  • (Boolean)


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

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

#success?Boolean

Whether the status is considered successful.

Returns:

  • (Boolean)


54
55
56
# File 'lib/protocol/http/response.rb', line 54

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

#to_aryObject



111
112
113
# File 'lib/protocol/http/response.rb', line 111

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

#to_sObject



107
108
109
# File 'lib/protocol/http/response.rb', line 107

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