Sha256: b51e030dc6ca7e84c68be892444065e80e138235e012f11092b5110ce8770a77

Contents?: true

Size: 1.04 KB

Versions: 2

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

require 'forwardable'

module Trav3
  class Headers
    extend Forwardable
    def_delegators :@heads, :each_pair, :fetch

    def initialize(args = {})
      build(args)
    end

    # Add or update the request headers
    #
    # @return [Headers] self
    def build(args = {})
      @heads ||= {}

      args.each do |(key, value)|
        @heads[key] = value
      end

      self
    end

    # Remove key/value from headers via key
    #
    # @param key [Symbol, String] key to look up
    # @return [String, Symbol, nil] returns value if key found, `nil` otherwise.
    def remove(key)
      @heads.delete(key)
    end

    # Add the values of one `Headers` into another
    #
    # @param other [Headers] instance of `Headers`
    # @return [Headers]
    def +(other)
      raise TypeError, "Headers type expected, #{other.class} given" unless other.is_a? Headers

      @heads.merge(other.instance_variable_get(:@heads))

      self
    end

    # @return [Hash] hash of the `Headers`
    def to_h
      @heads
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
trav3-0.4.1 lib/trav3/headers.rb
trav3-0.4.0 lib/trav3/headers.rb