Class: Merb::AcceptType
- Defined in:
- lib/merb-core/controller/mixins/responder.rb
Instance Attribute Summary collapse
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#media_range ⇒ Object
readonly
Returns the value of attribute media_range.
-
#quality ⇒ Object
readonly
Returns the value of attribute quality.
-
#sub_type ⇒ Object
readonly
Returns the value of attribute sub_type.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#<=>(entry) ⇒ Object
Compares two accept types for sorting purposes.
-
#==(entry) ⇒ Object
An alias for eql?.
-
#eql?(entry) ⇒ Boolean
Parameters entry<AcceptType>:: The accept type to compare.
-
#hash ⇒ Object
Returns Fixnum:: A hash based on the super range.
-
#initialize(entry, index) ⇒ AcceptType
constructor
Parameters entry<String>:: The accept type pattern index<Fixnum>:: The index used for sorting accept types.
- #mime ⇒ Object
-
#super_range ⇒ Object
Returns String:: The primary media range for this accept type, i.e.
-
#synonyms ⇒ Object
Returns Array:: All Accept header values, such as “text/html”, that match this type.
-
#to_s ⇒ Object
Returns String:: The accept type as a string, i.e.
-
#to_sym ⇒ Object
Returns Symbol: The type as a symbol, e.g.
Constructor Details
#initialize(entry, index) ⇒ AcceptType
Parameters
- entry<String>
-
The accept type pattern
- index<Fixnum>
-
The index used for sorting accept types. A lower value indicates higher priority.
382 383 384 385 386 387 388 389 390 391 392 |
# File 'lib/merb-core/controller/mixins/responder.rb', line 382 def initialize(entry,index) @index = index entry =~ /\s*([^;\s]*)\s*(;\s*q=\s*(.*))?/ @media_range, quality = $1, $3 @type, @sub_type = @media_range.split(%r{/}) (quality ||= 0.0) if @media_range == "*/*" @quality = quality ? (quality.to_f * 100).to_i : 100 @quality *= (mime && mime[:default_quality] || 1) end |
Instance Attribute Details
#index ⇒ Object (readonly)
Returns the value of attribute index.
375 376 377 |
# File 'lib/merb-core/controller/mixins/responder.rb', line 375 def index @index end |
#media_range ⇒ Object (readonly)
Returns the value of attribute media_range.
375 376 377 |
# File 'lib/merb-core/controller/mixins/responder.rb', line 375 def media_range @media_range end |
#quality ⇒ Object (readonly)
Returns the value of attribute quality.
375 376 377 |
# File 'lib/merb-core/controller/mixins/responder.rb', line 375 def quality @quality end |
#sub_type ⇒ Object (readonly)
Returns the value of attribute sub_type.
375 376 377 |
# File 'lib/merb-core/controller/mixins/responder.rb', line 375 def sub_type @sub_type end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
375 376 377 |
# File 'lib/merb-core/controller/mixins/responder.rb', line 375 def type @type end |
Instance Method Details
#<=>(entry) ⇒ Object
Compares two accept types for sorting purposes.
Parameters
- entry<AcceptType>
-
The accept type to compare.
Returns
- Fixnum
-
-1, 0 or 1, depending on whether entry has a lower, equal or higher priority than the accept type being compared.
403 404 405 406 407 408 409 |
# File 'lib/merb-core/controller/mixins/responder.rb', line 403 def <=>(entry) if entry.quality == quality @index <=> entry.index else entry.quality <=> @quality end end |
#==(entry) ⇒ Object
An alias for eql?.
424 |
# File 'lib/merb-core/controller/mixins/responder.rb', line 424 def ==(entry); eql?(entry); end |
#eql?(entry) ⇒ Boolean
Parameters
- entry<AcceptType>
-
The accept type to compare.
Returns
- Boolean
-
True if the accept types are equal, i.e. if the synonyms for this accept type includes the entry media range.
419 420 421 |
# File 'lib/merb-core/controller/mixins/responder.rb', line 419 def eql?(entry) synonyms.include?(entry.media_range) end |
#hash ⇒ Object
Returns
- Fixnum
-
A hash based on the super range.
428 |
# File 'lib/merb-core/controller/mixins/responder.rb', line 428 def hash; super_range.hash; end |
#mime ⇒ Object
442 443 444 |
# File 'lib/merb-core/controller/mixins/responder.rb', line 442 def mime @mime ||= Merb.available_mime_types[Merb::ResponderMixin::MIMES[@media_range]] end |
#super_range ⇒ Object
Returns
- String
-
The primary media range for this accept type, i.e. either the first synonym or, if none exist, the media range.
450 451 452 |
# File 'lib/merb-core/controller/mixins/responder.rb', line 450 def super_range synonyms.first || @media_range end |
#synonyms ⇒ Object
Returns
- Array
-
All Accept header values, such as “text/html”, that match this type.
433 434 435 436 437 438 439 440 |
# File 'lib/merb-core/controller/mixins/responder.rb', line 433 def synonyms return @syns if @syns if _mime = mime @syns = _mime[:accepts] else @syns = [] end end |
#to_s ⇒ Object
Returns
- String
-
The accept type as a string, i.e. the media range.
463 464 465 |
# File 'lib/merb-core/controller/mixins/responder.rb', line 463 def to_s @media_range end |
#to_sym ⇒ Object
Returns
Symbol: The type as a symbol, e.g. :html.
456 457 458 459 |
# File 'lib/merb-core/controller/mixins/responder.rb', line 456 def to_sym Merb.available_mime_types.select{|k,v| v[:accepts] == synonyms || v[:accepts][0] == synonyms[0]}.flatten.first end |