Class: Seahorse::Client::Http::Headers

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/seahorse/client/http/headers.rb

Overview

Provides a Hash-like interface for HTTP headers. Header names are treated indifferently as lower-cased strings. Header values are cast to strings.

headers = Http::Headers.new
headers['Content-Length'] = 100
headers[:Authorization] = 'Abc'

headers.keys
#=> ['content-length', 'authorization']

headers.values
#=> ['100', 'Abc']

You can get the header values as a vanilla hash by calling #to_h:

headers.to_h
#=> { 'content-length' => '100', 'authorization' => 'Abc' }

Instance Method Summary collapse

Constructor Details

#initialize(headers = {}) ⇒ Headers

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Headers.



31
32
33
34
35
36
# File 'lib/seahorse/client/http/headers.rb', line 31

def initialize(headers = {})
  @data = {}
  headers.each_pair do |key, value|
    self[key] = value
  end
end

Instance Method Details

#[](key) ⇒ String

Parameters:

  • key (String)

Returns:

  • (String)


40
41
42
# File 'lib/seahorse/client/http/headers.rb', line 40

def [](key)
  @data[key.to_s.downcase]
end

#[]=(key, value) ⇒ Object

Parameters:

  • key (String)
  • value (String)


46
47
48
# File 'lib/seahorse/client/http/headers.rb', line 46

def []=(key, value)
  @data[key.to_s.downcase] = value.to_s
end

#clearObject



64
65
66
# File 'lib/seahorse/client/http/headers.rb', line 64

def clear
  @data = {}
end

#delete(key) ⇒ Object

Parameters:

  • key (String)


60
61
62
# File 'lib/seahorse/client/http/headers.rb', line 60

def delete(key)
  @data.delete(key.to_s.downcase)
end

#each {|key, value| ... } ⇒ nil Also known as: each_pair

Yields:

  • (key, value)

Yield Parameters:

  • key (String)
  • value (String)

Returns:

  • (nil)


87
88
89
90
91
92
93
94
95
96
# File 'lib/seahorse/client/http/headers.rb', line 87

def each(&block)
  if block_given?
    @data.each_pair do |key, value|
      yield(key, value)
    end
    nil
  else
    @data.enum_for(:each)
  end
end

#inspectObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



113
114
115
# File 'lib/seahorse/client/http/headers.rb', line 113

def inspect
  @data.inspect
end

#key?(key) ⇒ Boolean Also known as: has_key?, include?

Returns ‘true` if the header is set.

Returns:

  • (Boolean)

    Returns ‘true` if the header is set.



100
101
102
# File 'lib/seahorse/client/http/headers.rb', line 100

def key?(key)
  @data.key?(key.to_s.downcase)
end

#keysArray<String>

Returns:

  • (Array<String>)


69
70
71
# File 'lib/seahorse/client/http/headers.rb', line 69

def keys
  @data.keys
end

#to_hashHash Also known as: to_h

Returns:

  • (Hash)


107
108
109
# File 'lib/seahorse/client/http/headers.rb', line 107

def to_hash
  @data.dup
end

#update(headers) ⇒ Headers

Parameters:

  • headers (Hash)

Returns:



52
53
54
55
56
57
# File 'lib/seahorse/client/http/headers.rb', line 52

def update(headers)
  headers.each_pair do |k, v|
    self[k] = v
  end
  self
end

#valuesArray<String>

Returns:

  • (Array<String>)


74
75
76
# File 'lib/seahorse/client/http/headers.rb', line 74

def values
  @data.values
end

#values_at(*keys) ⇒ Array<String>

Returns:

  • (Array<String>)


79
80
81
# File 'lib/seahorse/client/http/headers.rb', line 79

def values_at(*keys)
  @data.values_at(*keys.map{ |key| key.to_s.downcase })
end