Class: HTTY::Payload
- Inherits:
-
Object
- Object
- HTTY::Payload
- Defined in:
- lib/htty/payload.rb
Overview
Encapsulates the headers and body of an HTTP(S) request or response.
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the body of the payload.
Instance Method Summary collapse
-
#==(other_payload) ⇒ Object
(also: #eql?)
Returns
true
if other_payload is equivalent to the payload. - #cookies_from(cookies_header_key) ⇒ Object
- #header(key, otherwise = :__no_default_value_given) ⇒ Object
-
#headers ⇒ Object
Returns an array of the headers belonging to the payload.
- #headers_with_key(key) ⇒ Object
-
#initialize(attributes = {}) ⇒ Payload
constructor
protected
Initializes a new HTTY::Payload with attribute values specified in the attributes hash.
Constructor Details
#initialize(attributes = {}) ⇒ Payload (protected)
Initializes a new HTTY::Payload with attribute values specified in the attributes hash.
Valid attributes keys include:
-
:body
-
:headers
48 49 50 51 52 53 54 |
# File 'lib/htty/payload.rb', line 48 def initialize(attributes={}) @body = attributes[:body] @headers = HTTY::Headers.new Array(attributes[:headers]).each do |name, value| @headers[name] = value end end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the body of the payload.
7 8 9 |
# File 'lib/htty/payload.rb', line 7 def body @body end |
Instance Method Details
#==(other_payload) ⇒ Object Also known as: eql?
Returns true
if other_payload is equivalent to the payload.
10 11 12 13 |
# File 'lib/htty/payload.rb', line 10 def ==(other_payload) return false unless other_payload.kind_of?(HTTY::Payload) (other_payload.body == body) && (other_payload.headers == headers) end |
#cookies_from(cookies_header_key) ⇒ Object
35 36 37 |
# File 'lib/htty/payload.rb', line 35 def () HTTY::CookiesUtil. header(, nil) end |
#header(key, otherwise = :__no_default_value_given) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/htty/payload.rb', line 27 def header(key, otherwise=:__no_default_value_given) all_headers_with_key = headers_with_key(key) return all_headers_with_key.last.last unless all_headers_with_key.empty? raise otherwise if otherwise.is_a? Exception return otherwise unless otherwise == :__no_default_value_given raise HTTY::NoHeaderError.new(key) end |
#headers ⇒ Object
Returns an array of the headers belonging to the payload.
17 18 19 |
# File 'lib/htty/payload.rb', line 17 def headers @headers.to_a end |
#headers_with_key(key) ⇒ Object
21 22 23 24 25 |
# File 'lib/htty/payload.rb', line 21 def headers_with_key(key) headers.select do |header_key, header_value| key.downcase == header_key.downcase end end |