Sha256: 6908686b34a9fb1bdc70bf1d0a8d3cc454fcefdce1d7be0a9333ab13e3aa5769

Contents?: true

Size: 969 Bytes

Versions: 12

Compression:

Stored size: 969 Bytes

Contents

# encoding: utf-8

module Punchblock
  module HasHeaders
    ##
    # @return [Hash] hash of key-value pairs of headers
    #
    def headers_hash
      headers.inject({}) do |hash, header|
        hash[header.name.downcase.gsub('-', '_').to_sym] = header.value
        hash
      end
    end

    ##
    # @return [Array[Header]] headers
    #
    def headers
      find('//ns:header', :ns => self.class.registered_ns).map do |i|
        Header.new i
      end
    end

    ##
    # @param [Hash, Array] headers A hash of key-value header pairs, or an array of Header objects
    #
    def headers=(headers)
      find('//ns:header', :ns => self.class.registered_ns).each(&:remove)
      if headers.is_a? Hash
        headers.each_pair { |k,v| self << Header.new(k, v) }
      elsif headers.is_a? Array
        [headers].flatten.each { |i| self << Header.new(i) }
      end
    end

    def inspect_attributes # :nodoc:
      [:headers_hash] + super
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
punchblock-1.9.4 lib/punchblock/has_headers.rb
punchblock-1.9.3 lib/punchblock/has_headers.rb
punchblock-1.9.2 lib/punchblock/has_headers.rb
punchblock-1.9.1 lib/punchblock/has_headers.rb
punchblock-1.9.0 lib/punchblock/has_headers.rb
punchblock-1.8.2 lib/punchblock/has_headers.rb
punchblock-1.8.1 lib/punchblock/has_headers.rb
punchblock-1.8.0 lib/punchblock/has_headers.rb
punchblock-1.7.1 lib/punchblock/has_headers.rb
punchblock-1.7.0 lib/punchblock/has_headers.rb
punchblock-1.6.1 lib/punchblock/has_headers.rb
punchblock-1.6.0 lib/punchblock/has_headers.rb