Class: Rack::Accept::Encoding

Inherits:
Object
  • Object
show all
Includes:
Header
Defined in:
lib/rack/accept/encoding.rb

Overview

Represents an HTTP Accept-Encoding header according to the HTTP 1.1 specification, and provides several convenience methods for determining acceptable content encodings.

www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3

Instance Attribute Summary

Attributes included from Header::PublicInstanceMethods

#qvalues

Instance Method Summary collapse

Methods included from Header

join, normalize_qvalue, parse, parse_media_type, parse_range_params

Methods included from Header::PublicInstanceMethods

#accept?, #best_of, #initialize, #sort, #sort_with_qvalues, #to_s, #value, #values

Instance Method Details

#matches(encoding) ⇒ Object

Returns an array of encodings from this header that match the given encoding, ordered by precedence.



27
28
29
30
31
32
33
34
# File 'lib/rack/accept/encoding.rb', line 27

def matches(encoding)
  values.select {|v|
    v == encoding || v == '*'
  }.sort {|a, b|
    # "*" gets least precedence, any others should be equal.
    a == '*' ? 1 : (b == '*' ? -1 : 0)
  }
end

#nameObject

The name of this header.



11
12
13
# File 'lib/rack/accept/encoding.rb', line 11

def name
  'Accept-Encoding'
end

#qvalue(encoding) ⇒ Object

Determines the quality factor (qvalue) of the given encoding.



16
17
18
19
20
21
22
23
# File 'lib/rack/accept/encoding.rb', line 16

def qvalue(encoding)
  m = matches(encoding)
  if m.empty?
    encoding == 'identity' ? 1 : 0
  else
    normalize_qvalue(@qvalues[m.first])
  end
end