Class: HTTP::Headers

### Home Classes Methods

### In Files

- lib/http/headers.rb
- lib/http/headers/mixin.rb

### Parent

Object

### Namespace

- MODULE HTTP::Headers::Mixin

### Methods

- ::[]
- ::coerce
- ::new
- #==
- #[]
- #[]=
- #add
- #append
- #delete
- #each
- #get
- #initialize_copy
- #inspect
- #keys
- #merge
- #merge!
- #set
- #to_a
- #to_h

### Included Modules

- Enumerable

### Class/Module Index

Quicksearch

- HTTP
- HTTP::Chainable
- HTTP::Client
- HTTP::Error
- HTTP::Headers
- HTTP::Headers::Mixin
- HTTP::MimeType
- HTTP::MimeType::Adapter
- HTTP::MimeType::JSON
- HTTP::Options
- HTTP::Redirector
- HTTP::Redirector::EndlessRedirectError
- HTTP::Redirector::TooManyRedirectsError
- HTTP::Request
- HTTP::Request::UnsupportedMethodError
- HTTP::Request::UnsupportedSchemeError
- HTTP::Request::Writer
- HTTP::RequestError
- HTTP::Response
- HTTP::Response::Body
- HTTP::Response::Parser
- HTTP::Response::Status
- HTTP::ResponseError
- HTTP::StateError
- Object

No matching classes.

# HTTP::Headers

HTTP Headers container.

### Constants

CANONICAL_HEADER

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

### Public Class Methods

[](object) click to toggle source
Alias for: coerce
coerce(object) click to toggle source

Coerces given `object` into Headers.

@raise [Error] if object can't be coerced @param [to_hash, to_h, to_a] object @return [Headers]

    # 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

Also aliased as: []
new() click to toggle source

Class constructor.

    # File lib/http/headers.rb, line 15
    def initialize
      @pile = []
    end

### Public Instance Methods

==(other) click to toggle source

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

@return [Boolean]

    # File lib/http/headers.rb, line 105
    def ==(other)
      return false unless other.respond_to? :to_a
      @pile == other.to_a
    end

[](name) click to toggle source

Smart version of {get}.

@return [nil] if header was not set @return [String] if header has exactly one value @return [Array<String>] if header has more than one value

    # 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

[]=(name, value) click to toggle source
Alias for: set
add(name, value) click to toggle source

Appends header.

@param [to_s] name header name @param [Array<to_s>, to_s] value header value(s) to be appended @return [void]

    # 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

Also aliased as: append
append(name, value) click to toggle source

@deprecated Will be removed in 1.0.0

Alias for: add
delete(name) click to toggle source

Removes header.

@param [to_s] name header name @return [void]

    # 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) click to toggle source

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

@return [Enumerator] if no block given @return [Headers] self-reference

    # File lib/http/headers.rb, line 114
    def each(&blk)
      return @pile.each unless blk

      @pile.each(&blk)
      self
    end

get(name) click to toggle source

Returns list of header values if any.

@return [Array<String>]

    # 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

initialize_copy(orig) click to toggle source

Properly clones internal key/value storage.

@api private

    # File lib/http/headers.rb, line 138
    def initialize_copy(orig)
      super
      @pile = to_a
    end

inspect() click to toggle source

Returns human-readable representation of `self` instance.

@return [String]

    # File lib/http/headers.rb, line 91
    def inspect
      "#<#{self.class} #{to_h.inspect}>"
    end

keys() click to toggle source

Returns list of header names.

@return [Array<String>]

    # File lib/http/headers.rb, line 98
    def keys
      @pile.map { |k, _| k }.uniq
    end

merge(other) click to toggle source

Returns new instance with `other` headers merged in.

@see merge! @return [Headers]

    # File lib/http/headers.rb, line 155
    def merge(other)
      dup.tap { |dupped| dupped.merge! other }
    end

merge!(other) click to toggle source

Merges `other` headers into `self`.

@see merge @return [void]

    # 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) click to toggle source

Sets header.

@param (see add) @return [void]

    # File lib/http/headers.rb, line 23
    def set(name, value)
      delete(name)
      add(name, value)
    end

Also aliased as: []=
to_a() click to toggle source

Returns headers key/value pairs.

@return [Array<[String, String]>]

    # File lib/http/headers.rb, line 84
    def to_a
      @pile.map { |pair| pair.map(&:dup) }
    end

to_h() click to toggle source

Returns Rack-compatible headers Hash

@return [Hash]

    # File lib/http/headers.rb, line 77
    def to_h
      Hash[keys.map { |k| [k, self[k]] }]
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.