Class: Http2::Response
- Inherits:
-
Object
- Object
- Http2::Response
- Defined in:
- lib/http2/response.rb
Overview
This object will be returned as the response for each request.
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#charset ⇒ Object
Returns the value of attribute charset.
-
#code ⇒ Object
Returns the value of attribute code.
- #content_type ⇒ Object
-
#headers ⇒ Object
readonly
All the data the response contains.
-
#http_version ⇒ Object
Returns the value of attribute http_version.
-
#request ⇒ Object
readonly
All the data the response contains.
-
#request_args ⇒ Object
readonly
All the data the response contains.
-
#requested_url ⇒ Object
readonly
All the data the response contains.
Instance Method Summary collapse
- #content_length ⇒ Object
-
#header(key) ⇒ Object
Returns a certain header by name or false if not found.
-
#header?(key) ⇒ Boolean
Returns true if a header of the given string exists.
- #host ⇒ Object
-
#initialize(body: "", debug: false, headers: {}, request:) ⇒ Response
constructor
This method should not be called manually.
- #inspect ⇒ Object
- #json ⇒ Object
-
#json? ⇒ Boolean
Returns true if the result is JSON.
- #path ⇒ Object
- #port ⇒ Object
- #ssl? ⇒ Boolean
- #to_s ⇒ Object
-
#validate! ⇒ Object
Checks the data that has been sat on the object and raises various exceptions, if it does not validate somehow.
Constructor Details
#initialize(body: "", debug: false, headers: {}, request:) ⇒ Response
This method should not be called manually.
9 10 11 12 13 14 15 |
# File 'lib/http2/response.rb', line 9 def initialize(body: "", debug: false, headers: {}, request:) @body = body @debug = debug @headers = headers @request = request @requested_url = request.path end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
5 6 7 |
# File 'lib/http2/response.rb', line 5 def body @body end |
#charset ⇒ Object
Returns the value of attribute charset.
5 6 7 |
# File 'lib/http2/response.rb', line 5 def charset @charset end |
#code ⇒ Object
Returns the value of attribute code.
5 6 7 |
# File 'lib/http2/response.rb', line 5 def code @code end |
#content_type ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/http2/response.rb', line 45 def content_type if header?("content-type") header("content-type") else raise "No content-type was given." end end |
#headers ⇒ Object (readonly)
All the data the response contains. Headers, body, cookies, requested URL and more.
4 5 6 |
# File 'lib/http2/response.rb', line 4 def headers @headers end |
#http_version ⇒ Object
Returns the value of attribute http_version.
5 6 7 |
# File 'lib/http2/response.rb', line 5 def http_version @http_version end |
#request ⇒ Object (readonly)
All the data the response contains. Headers, body, cookies, requested URL and more.
4 5 6 |
# File 'lib/http2/response.rb', line 4 def request @request end |
#request_args ⇒ Object (readonly)
All the data the response contains. Headers, body, cookies, requested URL and more.
4 5 6 |
# File 'lib/http2/response.rb', line 4 def request_args @request_args end |
#requested_url ⇒ Object (readonly)
All the data the response contains. Headers, body, cookies, requested URL and more.
4 5 6 |
# File 'lib/http2/response.rb', line 4 def requested_url @requested_url end |
Instance Method Details
#content_length ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/http2/response.rb', line 35 def content_length if header?("content-length") header("content-length").to_i elsif @body @body.bytesize else raise "Couldn't calculate content-length." end end |
#header(key) ⇒ Object
Returns a certain header by name or false if not found.
Examples
val = res.header(“content-type”)
20 21 22 23 24 |
# File 'lib/http2/response.rb', line 20 def header(key) return false unless headers.key?(key) headers.fetch(key).first.to_s end |
#header?(key) ⇒ Boolean
Returns true if a header of the given string exists.
Examples
print “No content-type was given.” if !http.header?(“content-type”)
29 30 31 32 33 |
# File 'lib/http2/response.rb', line 29 def header?(key) return true if headers.key?(key) && !headers[key].first.to_s.empty? false end |
#host ⇒ Object
68 69 70 |
# File 'lib/http2/response.rb', line 68 def host @request.http2.host end |
#inspect ⇒ Object
88 89 90 |
# File 'lib/http2/response.rb', line 88 def inspect to_s end |
#json ⇒ Object
64 65 66 |
# File 'lib/http2/response.rb', line 64 def json @json ||= JSON.parse(body) end |
#json? ⇒ Boolean
Returns true if the result is JSON.
60 61 62 |
# File 'lib/http2/response.rb', line 60 def json? content_type == "application/json" end |
#path ⇒ Object
80 81 82 |
# File 'lib/http2/response.rb', line 80 def path @request.path end |
#port ⇒ Object
72 73 74 |
# File 'lib/http2/response.rb', line 72 def port @request.http2.port end |
#ssl? ⇒ Boolean
76 77 78 |
# File 'lib/http2/response.rb', line 76 def ssl? @request.http2.ssl? end |
#to_s ⇒ Object
84 85 86 |
# File 'lib/http2/response.rb', line 84 def to_s "#<Http::Response host=\"#{host}\" port=#{port} ssl=#{ssl?} path=\"#{path}\">" end |
#validate! ⇒ Object
Checks the data that has been sat on the object and raises various exceptions, if it does not validate somehow.
54 55 56 57 |
# File 'lib/http2/response.rb', line 54 def validate! puts "Http2: Validating response length." if @debug validate_body_versus_content_length! end |