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



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

def initialize(version = nil, status = 200, headers = [], 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.



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

def body
  @body
end

#headersObject

Returns the value of attribute headers.



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

def headers
  @headers
end

#protocolObject

Returns the value of attribute protocol.



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

def protocol
  @protocol
end

#statusObject

Returns the value of attribute status.



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

def status
  @status
end

#versionObject

Returns the value of attribute version.



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

def version
  @version
end

Class Method Details

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



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

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

.for_exception(exception) ⇒ Object



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

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

Instance Method Details

#bad_request?Boolean



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

def bad_request?
  status == 400
end

#continue?Boolean



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

def continue?
  status == 100
end

#failure?Boolean



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

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

#hijack?Boolean



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

def hijack?
  false
end

#partial?Boolean



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

def partial?
  status == 206
end

#preserve_method?Boolean



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

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

#redirection?Boolean



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

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

#server_failure?Boolean



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

def server_failure?
  status == 500
end

#success?Boolean



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

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

#to_aryObject



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

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

#to_sObject



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

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