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
-
#args ⇒ Object
readonly
All the data the response contains.
-
#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
Returns the value of attribute content_type.
-
#http_version ⇒ Object
Returns the value of attribute http_version.
-
#request ⇒ 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.
-
#headers ⇒ Object
Returns headers given from the host for the result.
- #host ⇒ Object
-
#initialize(args = {}) ⇒ 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
-
#requested_url ⇒ Object
Returns the requested URL as a string.
- #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(args = {}) ⇒ Response
This method should not be called manually.
8 9 10 11 12 13 14 |
# File 'lib/http2/response.rb', line 8 def initialize(args = {}) @args = args @args[:headers] ||= {} @body = args[:body] || "" @debug = args[:debug] @request = args.fetch(:request) end |
Instance Attribute Details
#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 args @args end |
#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
Returns the value of attribute content_type.
5 6 7 |
# File 'lib/http2/response.rb', line 5 def content_type @content_type 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 |
Instance Method Details
#content_length ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/http2/response.rb', line 39 def content_length if header?("content-length") header("content-length").to_i elsif @body return @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”)
26 27 28 29 |
# File 'lib/http2/response.rb', line 26 def header(key) return false unless @args.fetch(:headers).key?(key) @args.fetch(: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”)
34 35 36 37 |
# File 'lib/http2/response.rb', line 34 def header?(key) return true if @args[:headers].key?(key) && @args[:headers][key].first.to_s.length > 0 false end |
#headers ⇒ Object
Returns headers given from the host for the result.
Examples
headers_hash = res.headers
19 20 21 |
# File 'lib/http2/response.rb', line 19 def headers @args.fetch(:headers) end |
#host ⇒ Object
80 81 82 |
# File 'lib/http2/response.rb', line 80 def host @request.http2.host end |
#inspect ⇒ Object
100 101 102 |
# File 'lib/http2/response.rb', line 100 def inspect to_s end |
#json ⇒ Object
76 77 78 |
# File 'lib/http2/response.rb', line 76 def json @json ||= JSON.parse(body) end |
#json? ⇒ Boolean
Returns true if the result is JSON.
72 73 74 |
# File 'lib/http2/response.rb', line 72 def json? content_type == "application/json" end |
#path ⇒ Object
92 93 94 |
# File 'lib/http2/response.rb', line 92 def path @request.path end |
#port ⇒ Object
84 85 86 |
# File 'lib/http2/response.rb', line 84 def port @request.http2.port end |
#requested_url ⇒ Object
Returns the requested URL as a string.
Examples
res.requested_url #=> “?show=status&action=getstatus”
60 61 62 63 |
# File 'lib/http2/response.rb', line 60 def requested_url raise "URL could not be detected." unless @args[:request_args][:url] @args[:request_args][:url] end |
#ssl? ⇒ Boolean
88 89 90 |
# File 'lib/http2/response.rb', line 88 def ssl? @request.http2.ssl? end |
#to_s ⇒ Object
96 97 98 |
# File 'lib/http2/response.rb', line 96 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.
66 67 68 69 |
# File 'lib/http2/response.rb', line 66 def validate! puts "Http2: Validating response length." if @debug validate_body_versus_content_length! end |