Class: Winhttp::Response
- Inherits:
-
Object
- Object
- Winhttp::Response
- Defined in:
- lib/winhttp.rb
Overview
A plain, immutable HTTP response. Built in Ruby from the values the state machine queried out of WinHTTP.
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#final_url ⇒ Object
readonly
Returns the value of attribute final_url.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#raw_headers ⇒ Object
readonly
Returns the value of attribute raw_headers.
-
#reason ⇒ Object
readonly
Returns the value of attribute reason.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
-
#[](name) ⇒ Object
Case-insensitive single-header lookup (merged value), or nil.
- #http2? ⇒ Boolean
-
#initialize(status, raw, final_url, http2, body) ⇒ Response
constructor
status: Integer; raw: the WINHTTP_QUERY_RAW_HEADERS_CRLF blob (status line + header lines, CRLF-separated); final_url: String; http2: bool; body: String (ASCII-8BIT) or nil when streamed.
- #success? ⇒ Boolean
-
#text(encoding = nil) ⇒ Object
A copy of the body tagged with
encoding(else charset= from Content-Type, else UTF-8).
Constructor Details
#initialize(status, raw, final_url, http2, body) ⇒ Response
status: Integer; raw: the WINHTTP_QUERY_RAW_HEADERS_CRLF blob (status line
- header lines, CRLF-separated); final_url: String; http2: bool; body: String (ASCII-8BIT) or nil when streamed.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/winhttp.rb', line 61 def initialize(status, raw, final_url, http2, body) @status = status @http2 = http2 @final_url = final_url @body = body reason, pairs = Winhttp.send(:parse_raw_headers, raw) @reason = reason @raw_headers = pairs.map { |k, v| [k.dup.freeze, v.dup.freeze].freeze }.freeze merged = {} pairs.each do |k, v| lk = k.downcase merged[lk] = merged.key?(lk) ? "#{merged[lk]}, #{v}" : v end merged.each_value(&:freeze) @headers = merged.freeze freeze end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
56 57 58 |
# File 'lib/winhttp.rb', line 56 def body @body end |
#final_url ⇒ Object (readonly)
Returns the value of attribute final_url.
56 57 58 |
# File 'lib/winhttp.rb', line 56 def final_url @final_url end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
56 57 58 |
# File 'lib/winhttp.rb', line 56 def headers @headers end |
#raw_headers ⇒ Object (readonly)
Returns the value of attribute raw_headers.
56 57 58 |
# File 'lib/winhttp.rb', line 56 def raw_headers @raw_headers end |
#reason ⇒ Object (readonly)
Returns the value of attribute reason.
56 57 58 |
# File 'lib/winhttp.rb', line 56 def reason @reason end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
56 57 58 |
# File 'lib/winhttp.rb', line 56 def status @status end |
Instance Method Details
#[](name) ⇒ Object
Case-insensitive single-header lookup (merged value), or nil.
80 81 82 |
# File 'lib/winhttp.rb', line 80 def [](name) @headers[name.to_s.downcase] end |
#http2? ⇒ Boolean
84 85 86 |
# File 'lib/winhttp.rb', line 84 def http2? @http2 end |
#success? ⇒ Boolean
88 89 90 |
# File 'lib/winhttp.rb', line 88 def success? (200..299).cover?(@status) end |
#text(encoding = nil) ⇒ Object
A copy of the body tagged with encoding (else charset= from
Content-Type, else UTF-8). Tags only (force_encoding) — never transcodes;
unknown charset names fall back to UTF-8. Raises if the body was streamed.
95 96 97 98 99 100 101 102 |
# File 'lib/winhttp.rb', line 95 def text(encoding = nil) raise Error, "winhttp: body was streamed (use the chunk block)" if @body.nil? enc = encoding || charset_encoding || Encoding::UTF_8 @body.dup.force_encoding(enc) rescue ArgumentError @body.dup.force_encoding(Encoding::UTF_8) end |