Class: Simple::HTTP::Response
- Inherits:
-
Object
- Object
- Simple::HTTP::Response
- Defined in:
- lib/simple/http/response.rb
Constant Summary collapse
- BodyBuilder =
Simple::HTTP::BodyBuilder
Instance Attribute Summary collapse
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#message ⇒ Object
readonly
Returns the value of attribute message.
-
#original_body ⇒ Object
readonly
Returns the value of attribute original_body.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Class Method Summary collapse
Instance Method Summary collapse
-
#body ⇒ Object
returns the body.
- #bytes ⇒ Object
- #checked_content(into:) ⇒ Object
-
#content(into: nil) ⇒ Object
evaluate and potentially parse response.
-
#media_type ⇒ Object
e.g “text/plain”.
- #to_s ⇒ Object
Instance Attribute Details
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
26 27 28 |
# File 'lib/simple/http/response.rb', line 26 def headers @headers end |
#message ⇒ Object (readonly)
Returns the value of attribute message.
25 26 27 |
# File 'lib/simple/http/response.rb', line 25 def end |
#original_body ⇒ Object (readonly)
Returns the value of attribute original_body.
27 28 29 |
# File 'lib/simple/http/response.rb', line 27 def original_body @original_body end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
23 24 25 |
# File 'lib/simple/http/response.rb', line 23 def request @request end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
24 25 26 |
# File 'lib/simple/http/response.rb', line 24 def status @status end |
Class Method Details
.body_permitted?(verb) ⇒ Boolean
13 14 15 16 17 18 |
# File 'lib/simple/http/response.rb', line 13 def body_permitted?(verb) return false if verb == :HEAD return false if verb == :OPTIONS true end |
.build(request:, body:, headers:, status:, message:) ⇒ Object
5 6 7 8 9 10 11 |
# File 'lib/simple/http/response.rb', line 5 def build(request:, body:, headers:, status:, message:) unless body_permitted?(request.verb) body = nil end new request, body, headers, status, end |
Instance Method Details
#body ⇒ Object
returns the body
This method reencodes the text body into UTF-8. Non-text bodies should be encoded as ASCII-8BIT (a.k.a. “BINARY”)
57 58 59 |
# File 'lib/simple/http/response.rb', line 57 def body @body ||= @body_builder.reencode(@original_body) end |
#bytes ⇒ Object
61 62 63 |
# File 'lib/simple/http/response.rb', line 61 def bytes @original_body&.bytesize || 0 end |
#checked_content(into:) ⇒ Object
117 118 119 120 |
# File 'lib/simple/http/response.rb', line 117 def checked_content(into:) check_response_status! content(into: into) end |
#content(into: nil) ⇒ Object
evaluate and potentially parse response
70 71 72 73 74 75 76 77 78 |
# File 'lib/simple/http/response.rb', line 70 def content(into: nil) return parsed_content if into.nil? if parsed_content.is_a?(Array) parsed_content.map { |entry| convert_into(entry, into: into) } else convert_into(parsed_content, into: into) end end |
#media_type ⇒ Object
e.g “text/plain”
49 50 51 |
# File 'lib/simple/http/response.rb', line 49 def media_type @body_builder.media_type end |
#to_s ⇒ Object
65 66 67 |
# File 'lib/simple/http/response.rb', line 65 def to_s "#{status} #{message.gsub(/\s+$/, "")} (#{bytes} byte)" end |