Class: Wreq::Headers

Inherits:
Object
  • Object
show all
Defined in:
lib/wreq_ruby/header.rb

Instance Method Summary collapse

Instance Method Details

#fetch(name, default = FETCH_UNDEFINED) {|name| ... } ⇒ String, ...

Return a header value, a fallback, or the result of a block.

The block takes precedence when both a default and block are provided.

Parameters:

  • name (String, Symbol)

    Header name

  • default (Object) (defaults to: FETCH_UNDEFINED)

    Optional fallback for a missing name

Yield Parameters:

  • name (String, Symbol)

    The missing name

Returns:

  • (String, Array<String>, Object)

Raises:

  • (KeyError)

    if the name is missing and no fallback is provided



261
262
263
264
265
266
267
268
# File 'lib/wreq_ruby/header.rb', line 261

def fetch(name, default = FETCH_UNDEFINED)
  value = self[name]
  return value unless value.nil?
  return yield(name) if block_given?
  return default unless default.equal?(FETCH_UNDEFINED)

  raise KeyError, "key not found: #{name.inspect}"
end

#inspectString

Return a compact representation for debugging.

Returns:

  • (String)


292
293
294
# File 'lib/wreq_ruby/header.rb', line 292

def inspect
  "#<Wreq::Headers [#{length} headers]>"
end

#to_aArray<Array(String, String)>

Convert every header occurrence to a name-value pair.

Returns:

  • (Array<Array(String, String)>)


273
274
275
# File 'lib/wreq_ruby/header.rb', line 273

def to_a
  each.to_a
end

#to_hHash{String => String, Array<String>} Also known as: to_hash

Convert unique normalized names to a Hash.

A name with one occurrence maps to a String, while multiple occurrences map to an Array.

Returns:

  • (Hash{String => String, Array<String>})


283
284
285
# File 'lib/wreq_ruby/header.rb', line 283

def to_h
  keys.to_h { |name| [name, self[name]] }
end