class Typhoeus::Response::Header

This class represents the response header. It can be accessed like a hash.

@api private

Public Class Methods

new(raw) click to toggle source

Create a new header.

@example Create new header.

Header.new(raw)

@param [ String ] raw The raw header.

# File lib/typhoeus/response/header.rb, line 16
def initialize(raw)
  @raw = raw
  @sanitized = {}
  parse
  set_default_proc_on(self, lambda { |h, k| @sanitized[k.to_s.downcase] })
end

Public Instance Methods

parse() click to toggle source

Parses the raw header.

@example Parse header.

header.parse
# File lib/typhoeus/response/header.rb, line 27
def parse
  case @raw
  when Hash
    raw.each do |k, v|
      process_pair(k, v)
    end
  when String
    raw.lines.each do |header|
      next if header.empty? || header =~ /^HTTP\/1.[01]/
      process_line(header)
    end
  end
end

Private Instance Methods

process_line(header) click to toggle source

Processes line and saves the result.

@return [ void ]

# File lib/typhoeus/response/header.rb, line 46
def process_line(header)
  key, value = header.split(':', 2).map(&:strip)
  process_pair(key, value)
end
process_pair(key, value) click to toggle source

Sets key value pair for self and @sanitized.

@return [ void ]

# File lib/typhoeus/response/header.rb, line 54
def process_pair(key, value)
  set_value(key, value, self)
  set_value(key.downcase, value, @sanitized)
end
raw() click to toggle source

Returns the raw header or empty string.

@example Return raw header.

header.raw

@return [ String ] The raw header.

# File lib/typhoeus/response/header.rb, line 77
def raw
  @raw || ''
end
set_default_proc_on(hash, default_proc) click to toggle source

Sets the default proc for the specified hash independent of the Ruby version.

@return [ void ]

# File lib/typhoeus/response/header.rb, line 84
def set_default_proc_on(hash, default_proc)
  if hash.respond_to?(:default_proc=)
    hash.default_proc = default_proc
  else
    hash.replace(Hash.new(&default_proc).merge(hash))
  end
end
set_value(key, value, hash) click to toggle source

Sets value for key in specified hash

@return [ void ]

# File lib/typhoeus/response/header.rb, line 62
def set_value(key, value, hash)
  if hash.has_key?(key)
    hash[key] = [hash[key]] unless hash[key].is_a? Array
    hash[key].push(value)
  else
    hash[key] = value
  end
end