Class: KisHttp::Headers

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/kis_http/headers.rb

Overview

A header builder class for outgoing requests

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Headers

Returns a new instance of Headers.



19
20
21
# File 'lib/kis_http/headers.rb', line 19

def initialize(args = {})
  build(args)
end

Instance Method Details

#+(other) ⇒ Headers

Add the values of one ‘Headers` into another

Parameters:

  • other (Headers)

    instance of ‘Headers`

Returns:

Raises:

  • (TypeError)


57
58
59
60
61
62
63
64
# File 'lib/kis_http/headers.rb', line 57

def +(other)
  raise TypeError, "Headers type expected, #{other.class} given" \
    unless other.is_a? Headers

  @heads.merge(other.instance_variable_get(:@heads))

  self
end

#assign_each_to(obj) ⇒ Object

Assign each header to object via :[]



37
38
39
40
41
42
43
# File 'lib/kis_http/headers.rb', line 37

def assign_each_to(obj)
  each_pair do |header, value|
    obj[header] = value
  end

  obj
end

#build(args = {}) ⇒ Headers

Add or update the request headers

Returns:



26
27
28
29
30
31
32
33
34
# File 'lib/kis_http/headers.rb', line 26

def build(args = {})
  @heads ||= {}

  args.to_h.each do |(key, value)|
    @heads[key] = value
  end

  self
end

#each_pairObject

Forwards to :@heads.

See Also:

  • Hash#each_pair


17
# File 'lib/kis_http/headers.rb', line 17

def_delegators :@heads, :each_pair, :fetch

#fetchObject

Forwards to :@heads.

See Also:

  • Hash#fetch


17
# File 'lib/kis_http/headers.rb', line 17

def_delegators :@heads, :each_pair, :fetch

#remove(key) ⇒ String, ...

Remove key/value from headers via key

Parameters:

  • key (Symbol, String)

    key to look up

Returns:

  • (String, Symbol, nil)

    returns value if key found, ‘nil` otherwise.



49
50
51
# File 'lib/kis_http/headers.rb', line 49

def remove(key)
  @heads.delete(key)
end

#to_hHash

Returns hash of the ‘Headers`.

Returns:

  • (Hash)

    hash of the ‘Headers`



67
68
69
# File 'lib/kis_http/headers.rb', line 67

def to_h
  @heads
end