Class: Rack::Request::AcceptableMediaTypes::MediaType

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/rack/acceptable.rb

Constant Summary collapse

ANY =
"*"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(media_type) ⇒ MediaType

Returns a new instance of MediaType.



100
101
102
# File 'lib/rack/acceptable.rb', line 100

def initialize(media_type)
  @media_type = media_type
end

Instance Attribute Details

#media_typeObject

Returns the value of attribute media_type.



96
97
98
# File 'lib/rack/acceptable.rb', line 96

def media_type
  @media_type
end

#qualityObject

qvalue = ( “0” [ “.” 0*3DIGIT ] ) | ( “1” [ “.” 0*3(“0”) ] )



114
115
116
# File 'lib/rack/acceptable.rb', line 114

def quality
  @quality
end

#rangeObject

media-range = ( “/” | ( type “/” “*” ) | ( type “/” subtype ) ) *( “;” parameter )



108
109
110
# File 'lib/rack/acceptable.rb', line 108

def range
  @range
end

Instance Method Details

#<=>(other) ⇒ Object



118
119
120
# File 'lib/rack/acceptable.rb', line 118

def <=>(other)
  other.quality <=> quality
end

#==(other_media_type) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/rack/acceptable.rb', line 122

def ==(other_media_type)
  return false unless other_media_type.is_a?(String) || other_media_type.is_a?(MediaType)
  other_media_type = MediaType.new(other_media_type) unless other_media_type.is_a?(MediaType)

  if type == ANY
    true
  elsif type == other_media_type.type
    subtype == ANY || subtype == other_media_type.subtype
  else 
    false
  end
end

#inspectObject



135
136
137
# File 'lib/rack/acceptable.rb', line 135

def inspect
  %Q{#<#{self.class}:0x%1x #{to_s}>} % object_id
end

#subtypeObject



156
157
158
# File 'lib/rack/acceptable.rb', line 156

def subtype
  @subtype ||= range.split('/').last
end

#to_sObject



139
140
141
# File 'lib/rack/acceptable.rb', line 139

def to_s
  @media_type
end

#typeObject



152
153
154
# File 'lib/rack/acceptable.rb', line 152

def type
  @type ||= range.split('/').first
end

#valid?Boolean

“A weight is normalized to a real number in the range 0 through 1, where 0 is the minimum and 1 the maximum value. If a parameter has a quality value of 0, then content with this parameter is ‘not acceptable’ for the client.”

Returns:

  • (Boolean)


148
149
150
# File 'lib/rack/acceptable.rb', line 148

def valid?
  quality > 0 && quality <= 1
end