Sha256: 33b519d7939536e58b467e7e42372e14815d8c4875f92d6dfedb1b223a533225

Contents?: true

Size: 990 Bytes

Versions: 6

Compression:

Stored size: 990 Bytes

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.
  #
  # http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.2
  class Charset
    include Header

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

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

    # Returns an array of character sets from this 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
  end
end

Version data entries

6 entries across 6 versions & 3 rubygems

Version Path
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rack-accept-0.4.5/lib/rack/accept/charset.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/rack-accept-0.4.5/lib/rack/accept/charset.rb
fragrant-0.0.5 vendor/bundle/ruby/1.9.1/gems/rack-accept-0.4.5/lib/rack/accept/charset.rb
rack-accept-0.4.5 lib/rack/accept/charset.rb
rack-accept-0.4.4 lib/rack/accept/charset.rb
rack-accept-0.4.3 lib/rack/accept/charset.rb