Class: Strelka::HTTPRequest::AcceptParam

Inherits:
Object
  • Object
show all
Extended by:
Loggability, AbstractClass
Includes:
Comparable
Defined in:
lib/strelka/httprequest/acceptparams.rb

Overview

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

Direct Known Subclasses

Charset, Encoding, Language, MediaType

Constant Summary collapse

Q_DEFAULT =

The default quality value (weight) if none is specified

1.0
Q_MAX =

The maximum quality value

Q_DEFAULT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AbstractClass

extended, included, inherited, pure_virtual

Constructor Details

#initialize(type, subtype = '*', qval = Q_DEFAULT, *extensions) ⇒ AcceptParam

Create a new Strelka::HTTPRequest::AcceptParam with the given media range, quality value (+qval+), and extensions



75
76
77
78
79
80
81
82
83
# File 'lib/strelka/httprequest/acceptparams.rb', line 75

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

Instance Attribute Details

#extensionsObject (readonly)

An array of any accept-extensions specified with the parameter



103
104
105
# File 'lib/strelka/httprequest/acceptparams.rb', line 103

def extensions
  @extensions
end

#qvalueObject (readonly)

The weight of the param



100
101
102
# File 'lib/strelka/httprequest/acceptparams.rb', line 100

def qvalue
  @qvalue
end

#subtypeObject (readonly)

The 'subtype' part of the media range



97
98
99
# File 'lib/strelka/httprequest/acceptparams.rb', line 97

def subtype
  @subtype
end

#typeObject (readonly)

The 'type' part of the media range



94
95
96
# File 'lib/strelka/httprequest/acceptparams.rb', line 94

def type
  @type
end

Instance Method Details

#<=>(other) ⇒ Object

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.



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/strelka/httprequest/acceptparams.rb', line 163

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

#=~(other) ⇒ Object

Match operator -- returns true if other matches the receiving AcceptParam.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/strelka/httprequest/acceptparams.rb', line 108

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

#extension_stringsObject

Return a String containing any extensions for this parameter, joined with ';'



154
155
156
157
# File 'lib/strelka/httprequest/acceptparams.rb', line 154

def extension_strings
  return nil if self.extensions.empty?
  return self.extensions.compact.join('; ')
end

#inspectObject

Return a human-readable version of the object



133
134
135
136
137
138
139
140
141
142
# File 'lib/strelka/httprequest/acceptparams.rb', line 133

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

#qvaluestringObject

The weighting or "qvalue" of the parameter in the form "q="



146
147
148
149
# File 'lib/strelka/httprequest/acceptparams.rb', line 146

def qvaluestring
  # 3 digit precision, trim excess zeros
  return sprintf( "q=%0.3f", self.qvalue ).gsub(/0{1,2}$/, '')
end