Class: Merb::AcceptType

Inherits:
Object show all
Defined in:
lib/merb-core/controller/mixins/responder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entry, index) ⇒ AcceptType

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters

entry<String>

The accept type pattern

index<Fixnum>

The index used for sorting accept types. A lower value indicates higher priority.



388
389
390
391
392
393
394
395
396
397
398
# File 'lib/merb-core/controller/mixins/responder.rb', line 388

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

#indexObject (readonly)

Returns the value of attribute index.



379
380
381
# File 'lib/merb-core/controller/mixins/responder.rb', line 379

def index
  @index
end

#media_rangeObject (readonly)

Returns the value of attribute media_range.



379
380
381
# File 'lib/merb-core/controller/mixins/responder.rb', line 379

def media_range
  @media_range
end

#qualityObject (readonly)

Returns the value of attribute quality.



379
380
381
# File 'lib/merb-core/controller/mixins/responder.rb', line 379

def quality
  @quality
end

#sub_typeObject (readonly)

Returns the value of attribute sub_type.



379
380
381
# File 'lib/merb-core/controller/mixins/responder.rb', line 379

def sub_type
  @sub_type
end

#typeObject (readonly)

Returns the value of attribute type.



379
380
381
# File 'lib/merb-core/controller/mixins/responder.rb', line 379

def type
  @type
end

Instance Method Details

#<=>(entry) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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.



411
412
413
414
415
416
417
# File 'lib/merb-core/controller/mixins/responder.rb', line 411

def <=>(entry)
  if entry.quality == quality
    @index <=> entry.index
  else
    entry.quality <=> @quality
  end
end

#==(entry) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

An alias for eql?.



436
# File 'lib/merb-core/controller/mixins/responder.rb', line 436

def ==(entry); eql?(entry); end

#eql?(entry) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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.

Returns:

  • (Boolean)


429
430
431
# File 'lib/merb-core/controller/mixins/responder.rb', line 429

def eql?(entry)
  synonyms.include?(entry.media_range)
end

#hashObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns

Fixnum

A hash based on the super range.



442
# File 'lib/merb-core/controller/mixins/responder.rb', line 442

def hash; super_range.hash; end

#mimeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



459
460
461
# File 'lib/merb-core/controller/mixins/responder.rb', line 459

def mime
  @mime ||= Merb.available_mime_types[Merb::ResponderMixin::MIMES[@media_range]]
end

#super_rangeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns

String

The primary media range for this accept type, i.e. either the first synonym or, if none exist, the media range.



469
470
471
# File 'lib/merb-core/controller/mixins/responder.rb', line 469

def super_range
  synonyms.first || @media_range
end

#synonymsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns

Array

All Accept header values, such as “text/html”, that match this type.



449
450
451
452
453
454
455
456
# File 'lib/merb-core/controller/mixins/responder.rb', line 449

def synonyms
  return @syns if @syns
  if _mime = mime
    @syns = _mime[:accepts]
  else
    @syns = []
  end
end

#to_sObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns

String

The accept type as a string, i.e. the media range.



486
487
488
# File 'lib/merb-core/controller/mixins/responder.rb', line 486

def to_s
  @media_range
end

#to_symObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns

Symbol: The type as a symbol, e.g. :html.



477
478
479
480
# File 'lib/merb-core/controller/mixins/responder.rb', line 477

def to_sym
  Merb.available_mime_types.select{|k,v| 
    v[:accepts] == synonyms || v[:accepts][0] == synonyms[0]}.flatten.first
end