class Strelka::HTTPRequest::AcceptParam
A parser for request Accept , Accept-encoding , Accept-charset , and Accept-language header values. They provide weighted and wildcard comparisions between two values of the same field.
require 'strelka/httprequest/acceptparam' mediatype = Strelka::HTTPRequest::AcceptParam.parse_mediatype( "text/html;q=0.9;level=2" ) ap.type #=> 'text' ap.subtype #=> 'html' ap.qvalue #=> 0.9 ap =~ 'text/*' #=> true language = Strelka::HTTPRequest::AcceptParam.parse_language( "en-gb" ) ap.type #=> :en ap.subtype #=> :gb ap.qvalue #=> 1.0 ap =~ 'en' #=> true encoding = Strelka::HTTPRequest::AcceptParam.parse_encoding( "compress; q=0.7" ) ap.type #=> :compress ap.subtype #=> nil ap.qvalue #=> 0.7 ap =~ 'compress' #=> true charset = Strelka::HTTPRequest::AcceptParam.parse_charset( "koi8-r" ) ap.type #=> 'koi8-r' ap.subtype #=> nil ap.qvalue #=> 1.0 ap =~ 'koi8-r' #=> true
Authors¶ ↑
-
Michael Granger <ged@FaerieMUD.org>
-
Mahlon E. Smith <mahlon@martini.nu>
Constants
- Q_DEFAULT
The default quality value (weight) if none is specified
- Q_MAX
The maximum quality value
Attributes
An array of any accept-extensions specified with the parameter
The weight of the param
The 'subtype' part of the media range
The 'type' part of the media range
Public Class Methods
Create a new Strelka::HTTPRequest::AcceptParam
with the given media range
, quality value (qval
), and extensions
# File lib/strelka/httprequest/acceptparams.rb, line 74 def initialize( type, subtype='*', qval=Q_DEFAULT, *extensions ) type = nil if type == '*' subtype = nil if subtype == '*' @type = type @subtype = subtype @qvalue = normalize_qvalue( qval ) @extensions = extensions.flatten end
Public Instance Methods
Comparable interface. Sort parameters by weight: Returns -1 if other
is less specific than the receiver, 0 if other
is as specific as the receiver, and +1 if other
is more specific than the receiver.
# File lib/strelka/httprequest/acceptparams.rb, line 162 def <=>( other ) if rval = (other.qvalue <=> @qvalue).nonzero? return rval end if self.type.nil? return 1 if ! other.type.nil? elsif other.type.nil? return -1 end if self.subtype.nil? return 1 if ! other.subtype.nil? elsif other.subtype.nil? return -1 end if rval = (self.extensions.length <=> other.extensions.length).nonzero? return rval end return self.to_s <=> other.to_s end
Match operator – returns true if other
matches the receiving AcceptParam
.
# File lib/strelka/httprequest/acceptparams.rb, line 107 def =~( other ) unless other.is_a?( self.class ) other = self.class.parse( other.to_s ) rescue nil return false unless other end # */* returns true in either side of the comparison. # ASSUMPTION: There will never be a case when a type is wildcarded # and the subtype is specific. (e.g., */xml) # We gave up trying to read RFC 2045. return true if other.type.nil? || self.type.nil? # text/html =~ text/html # text/* =~ text/html # text/html =~ text/* if other.type == self.type return true if other.subtype.nil? || self.subtype.nil? return true if other.subtype == self.subtype end return false end
Return a String containing any extensions for this parameter, joined with ';'
# File lib/strelka/httprequest/acceptparams.rb, line 153 def extension_strings return nil if self.extensions.empty? return self.extensions.compact.join('; ') end
Return a human-readable version of the object
# File lib/strelka/httprequest/acceptparams.rb, line 132 def inspect return "#<%s:0x%07x '%s/%s' q=%0.3f %p>" % [ self.class.name, self.object_id * 2, self.type || '*', self.subtype || '*', self.qvalue, self.extensions, ] end
The weighting or “qvalue” of the parameter in the form “q=<value>”
# File lib/strelka/httprequest/acceptparams.rb, line 145 def qvaluestring # 3 digit precision, trim excess zeros return sprintf( "q=%0.3f", self.qvalue ).gsub(/0{1,2}$/, '') end
Private Instance Methods
Given an input qvalue
, return the Float equivalent.
# File lib/strelka/httprequest/acceptparams.rb, line 193 def normalize_qvalue( qvalue ) return Q_DEFAULT unless qvalue qvalue = Float( qvalue.to_s.sub(/q=/, '') ) unless qvalue.is_a?( Float ) if qvalue > Q_MAX self.log.warn "Squishing invalid qvalue %p to %0.1f" % [ qvalue, Q_DEFAULT ] return Q_DEFAULT end return qvalue end