Class: HTTY::Payload

Inherits:
Object
  • Object
show all
Defined in:
lib/htty/payload.rb

Overview

Encapsulates the headers and body of an HTTP(S) request or response.

Direct Known Subclasses

Request, Response

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#bodyObject (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 cookies_from(cookies_header_key)
  HTTY::CookiesUtil.cookies_from_string header(cookies_header_key, 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

#headersObject

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