Class: HTTP::Headers

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/http/headers.rb,
lib/http/headers/mixin.rb

Overview

HTTP Headers container.

Defined Under Namespace

Modules: Mixin

Constant Summary collapse

CANONICAL_HEADER =

Matches HTTP header names when in "Canonical-Http-Format"

/^[A-Z][a-z]*(-[A-Z][a-z]*)*$/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHeaders

Class constructor.



15
16
17
# File 'lib/http/headers.rb', line 15

def initialize
  @pile = []
end

Class Method Details

.coerce(object) ⇒ Headers Also known as: []

Coerces given object into Headers.

Parameters:

Returns:

Raises:

  • (Error)

    if object can't be coerced



165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/http/headers.rb', line 165

def coerce(object)
  unless object.is_a? self
    object = case
             when object.respond_to?(:to_hash) then object.to_hash
             when object.respond_to?(:to_h)    then object.to_h
             when object.respond_to?(:to_a)    then object.to_a
             else fail Error, "Can't coerce #{object.inspect} to Headers"
             end
  end

  headers = new
  object.each { |k, v| headers.add k, v }
  headers
end

Instance Method Details

#==(other) ⇒ Boolean

Compares headers to another Headers or Array of key/value pairs

Returns:

  • (Boolean)


105
106
107
108
# File 'lib/http/headers.rb', line 105

def ==(other)
  return false unless other.respond_to? :to_a
  @pile == other.to_a
end

#[](name) ⇒ nil, ...

Smart version of #get.

Returns:

  • (nil)

    if header was not set

  • (String)

    if header has exactly one value

  • (Array<String>)

    if header has more than one value



64
65
66
67
68
69
70
71
72
# File 'lib/http/headers.rb', line 64

def [](name)
  values = get(name)

  case values.count
  when 0 then nil
  when 1 then values.first
  else        values
  end
end

#add(name, value) ⇒ void Also known as: append

This method returns an undefined value.

Appends header.

Parameters:

  • name (#to_s)

    header name

  • value (Array<#to_s>, #to_s)

    header value(s) to be appended



43
44
45
46
# File 'lib/http/headers.rb', line 43

def add(name, value)
  name  = canonicalize_header name.to_s
  Array(value).each { |v| @pile << [name, v.to_s] }
end

#delete(name) ⇒ void

This method returns an undefined value.

Removes header.

Parameters:

  • name (#to_s)

    header name



33
34
35
36
# File 'lib/http/headers.rb', line 33

def delete(name)
  name = canonicalize_header name.to_s
  @pile.delete_if { |k, _| k == name }
end

#each(&blk) ⇒ Enumerator, Headers

Calls the given block once for each key/value pair in headers container.

Returns:

  • (Enumerator)

    if no block given

  • (Headers)

    self-reference



114
115
116
117
118
119
# File 'lib/http/headers.rb', line 114

def each(&blk)
  return @pile.each unless blk

  @pile.each(&blk)
  self
end

#empty?Boolean

Returns true if self has no key/value pairs

Returns:

  • (Boolean)


125
# File 'lib/http/headers.rb', line 125

def_delegator :@pile, :empty?

#get(name) ⇒ Array<String>

Returns list of header values if any.

Returns:

  • (Array<String>)


54
55
56
57
# File 'lib/http/headers.rb', line 54

def get(name)
  name = canonicalize_header name.to_s
  @pile.select { |k, _| k == name }.map { |_, v| v }
end

#hashFixnum

Compute a hash-code for this headers container. Two conatiners with the same content will have the same hash code.



133
# File 'lib/http/headers.rb', line 133

def_delegator :@pile, :hash

#initialize_copy(orig) ⇒ Object

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.

Properly clones internal key/value storage.



138
139
140
141
# File 'lib/http/headers.rb', line 138

def initialize_copy(orig)
  super
  @pile = to_a
end

#inspectString

Returns human-readable representation of self instance.

Returns:

  • (String)


91
92
93
# File 'lib/http/headers.rb', line 91

def inspect
  "#<#{self.class} #{to_h.inspect}>"
end

#keysArray<String>

Returns list of header names.

Returns:

  • (Array<String>)


98
99
100
# File 'lib/http/headers.rb', line 98

def keys
  @pile.map { |k, _| k }.uniq
end

#merge(other) ⇒ Headers

Returns new instance with other headers merged in.

Returns:

See Also:



155
156
157
# File 'lib/http/headers.rb', line 155

def merge(other)
  dup.tap { |dupped| dupped.merge! other }
end

#merge!(other) ⇒ void

This method returns an undefined value.

Merges other headers into self.

See Also:



147
148
149
# File 'lib/http/headers.rb', line 147

def merge!(other)
  self.class.coerce(other).to_h.each { |name, values| set name, values }
end

#set(name, value) ⇒ void Also known as: []=

This method returns an undefined value.

Sets header.

Parameters:

  • name (#to_s)

    header name

  • value (Array<#to_s>, #to_s)

    header value(s) to be appended



23
24
25
26
# File 'lib/http/headers.rb', line 23

def set(name, value)
  delete(name)
  add(name, value)
end

#to_aArray<[String, String]>

Returns headers key/value pairs.

Returns:

  • (Array<[String, String]>)


84
85
86
# File 'lib/http/headers.rb', line 84

def to_a
  @pile.map { |pair| pair.map(&:dup) }
end

#to_hHash

Returns Rack-compatible headers Hash

Returns:

  • (Hash)


77
78
79
# File 'lib/http/headers.rb', line 77

def to_h
  Hash[keys.map { |k| [k, self[k]] }]
end