Class: HTTP::Headers

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

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

:nodoc:


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

def initialize
  @pile = []
end

Class Method Details

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

Initiates new Headers object from given object.

Parameters:

Returns:

Raises:

  • (Error)

    if given object can't be coerced


131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/http/headers.rb', line 131

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)

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

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

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

Smart version of #get

Returns:

  • (NilClass)

    if header was not set

  • (Object)

    if header has exactly one value

  • (Array<Object>)

    if header has more than one value


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

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.

Append header


38
39
40
41
# File 'lib/http/headers.rb', line 38

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

#delete(name) ⇒ void

This method returns an undefined value.

Removes header


30
31
32
33
# File 'lib/http/headers.rb', line 30

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

#get(name) ⇒ Array

Return array of header values if any.

Returns:

  • (Array)

47
48
49
50
# File 'lib/http/headers.rb', line 47

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

#initialize_copy(orig) ⇒ Object

:nodoc:


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

def initialize_copy(orig)
  super
  @pile = to_a
end

#inspectObject

:nodoc:


82
83
84
# File 'lib/http/headers.rb', line 82

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

#keysArray<String>

List of header names

Returns:

  • (Array<String>)

89
90
91
# File 'lib/http/headers.rb', line 89

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

#merge(other) ⇒ Headers

Returns new Headers instance with other headers merged in.

Returns:

See Also:


121
122
123
# File 'lib/http/headers.rb', line 121

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

#merge!(other) ⇒ void

This method returns an undefined value.

Merge in other headers

See Also:


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

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


21
22
23
24
# File 'lib/http/headers.rb', line 21

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

#to_aArray<[String, String]>

Array of key/value pairs

Returns:

  • (Array<[String, String]>)

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

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

#to_hHash

Converts headers into a Rack-compatible Hash

Returns:

  • (Hash)

70
71
72
# File 'lib/http/headers.rb', line 70

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