Sha256: 5ea8a6e7022de3040ff58a441ad43419eff35ef044a12a631e44db23427eb6ff

Contents?: true

Size: 800 Bytes

Versions: 11

Compression:

Stored size: 800 Bytes

Contents

module Webmachine
  # Case-insensitive Hash of Request headers
  class Headers < ::Hash
    # Convert CGI-style Hash into Request headers
    # @param [Hash] env a hash of CGI-style env/headers
    def self.from_cgi(env)
      env.inject(new) do |h,(k,v)|
        if k =~ /^HTTP_(\w+)$/
          h[$1.tr("_", "-")] = v
        end
        h
      end
    end

    # Fetch a header
    def [](key)
      super transform_key(key)
    end

    # Set a header
    def []=(key,value)
      super transform_key(key), value
    end

    # Delete a header
    def delete(key)
      super transform_key(key)
    end

    # Select matching headers
    def grep(pattern)
      self.class[select { |k,_| pattern === k }]
    end

    private
    def transform_key(key)
      key.to_s.downcase
    end
  end
end

Version data entries

11 entries across 3 versions & 1 rubygems

Version Path
webmachine-0.4.1 pkg/webmachine-0.4.0/pkg/webmachine-0.3.0/pkg/webmachine-0.2.0/lib/webmachine/headers.rb
webmachine-0.4.1 pkg/webmachine-0.2.0/lib/webmachine/headers.rb
webmachine-0.4.1 pkg/webmachine-0.3.0/lib/webmachine/headers.rb
webmachine-0.4.1 pkg/webmachine-0.3.0/pkg/webmachine-0.2.0/lib/webmachine/headers.rb
webmachine-0.4.1 pkg/webmachine-0.4.0/pkg/webmachine-0.2.0/lib/webmachine/headers.rb
webmachine-0.4.1 pkg/webmachine-0.4.0/pkg/webmachine-0.3.0/lib/webmachine/headers.rb
webmachine-0.4.0 pkg/webmachine-0.2.0/lib/webmachine/headers.rb
webmachine-0.4.0 pkg/webmachine-0.3.0/lib/webmachine/headers.rb
webmachine-0.4.0 pkg/webmachine-0.3.0/pkg/webmachine-0.2.0/lib/webmachine/headers.rb
webmachine-0.3.0 pkg/webmachine-0.2.0/lib/webmachine/headers.rb
webmachine-0.3.0 lib/webmachine/headers.rb