Strelka::HTTPRequest::

AcceptParam

class
Superclass
Object
Included Modules
Comparable
Extended With
Loggability
Strelka::AbstractClass

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

Constants

Q_DEFAULT

The default quality value (weight) if none is specified

Q_MAX

The maximum quality value

Attributes

extensions[R]

An array of any accept-extensions specified with the parameter

qvalue[R]

The weight of the param

subtype[R]

The 'subtype' part of the media range

type[R]

The 'type' part of the media range

Public Class Methods

anchor
new( type, subtype='*', qval=Q_DEFAULT, *extensions )

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

anchor
<=>( other )

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
anchor
=~( other )

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
anchor
extension_strings()

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
anchor
inspect()

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
anchor
qvaluestring()

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