Sha256: 044f18363363e2589c32276d7bde2cd5b8dd71a93e2106eab408e11903afc864

Contents?: true

Size: 1.52 KB

Versions: 2

Compression:

Stored size: 1.52 KB

Contents

module Rack::Accept

  # Represents an HTTP Accept-Charset header according to the HTTP 1.1
  # specification, and provides several convenience methods for determining
  # acceptable character sets.
  #
  # See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html for more
  # information.
  class Charset

    include Header

    attr_reader :qvalues

    def initialize(header)
      @qvalues = parse(header)
    end

    # The name of this header.
    def name
      'Accept-Charset'
    end

    # The value of this header, built from its internal representation.
    def value
      join(@qvalues)
    end

    # Returns an array of all character set values that were specified in the
    # original header, in no particular order.
    def values
      @qvalues.keys
    end

    # Determines the quality factor (qvalue) of the given +charset+,
    # according to the specifications of the original header.
    def qvalue(charset)
      m = matches(charset)
      if m.empty?
        charset == 'iso-8859-1' ? 1 : 0
      else
        @qvalues[m.first]
      end
    end

    # Returns an array of character sets from the original header that match
    # the given +charset+, ordered by precedence.
    def matches(charset)
      values.select {|v|
        v == charset || v == '*'
      }.sort {|a, b|
        # "*" gets least precedence, any others should be equal.
        a == '*' ? 1 : (b == '*' ? -1 : 0)
      }
    end

    # Returns a string representation of this header.
    def to_s
      [name, value].join(': ')
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rack-accept-0.1.1 lib/rack/accept/charset.rb
rack-accept-0.1 lib/rack/accept/charset.rb