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

Parameters

entry<String>

The accept type pattern

index<Fixnum>

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



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

def initialize(entry,index)
  @index = index
  @media_range, quality = entry.split(/;\s*q=/).map{|a| a.strip }
  @type, @sub_type = @media_range.split(/\//)
  quality ||= 0.0 if @media_range == '*/*'
  @quality = ((quality || 1.0).to_f * 100).to_i
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



384
385
386
# File 'lib/merb-core/controller/mixins/responder.rb', line 384

def index
  @index
end

#media_rangeObject (readonly)

Returns the value of attribute media_range.



384
385
386
# File 'lib/merb-core/controller/mixins/responder.rb', line 384

def media_range
  @media_range
end

#qualityObject (readonly)

Returns the value of attribute quality.



384
385
386
# File 'lib/merb-core/controller/mixins/responder.rb', line 384

def quality
  @quality
end

#sub_typeObject (readonly)

Returns the value of attribute sub_type.



384
385
386
# File 'lib/merb-core/controller/mixins/responder.rb', line 384

def sub_type
  @sub_type
end

#typeObject (readonly)

Returns the value of attribute type.



384
385
386
# File 'lib/merb-core/controller/mixins/responder.rb', line 384

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.



408
409
410
411
412
# File 'lib/merb-core/controller/mixins/responder.rb', line 408

def <=>(entry)
  c = entry.quality <=> quality
  c = index <=> entry.index if c == 0
  c
end

#==(entry) ⇒ Object

An alias for eql?.



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

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.

Returns:

  • (Boolean)


421
422
423
# File 'lib/merb-core/controller/mixins/responder.rb', line 421

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

#hashObject

Returns

Fixnum

A hash based on the super range.



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

def hash; super_range.hash; end

#super_rangeObject

Returns

String

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



445
446
447
# File 'lib/merb-core/controller/mixins/responder.rb', line 445

def super_range
  synonyms.first || @media_range
end

#synonymsObject

Returns

Array

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



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

def synonyms
  @syns ||= Merb.available_mime_types.values.map do |e| 
    e[:request_headers] if e[:request_headers].include?(@media_range)
  end.compact.flatten
end

#to_sObject

Returns

String

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



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

def to_s
  @media_range
end

#to_symObject

Returns

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



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

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