Class: Steppe::ContentType
- Inherits:
-
Object
- Object
- Steppe::ContentType
- Defined in:
- lib/steppe/content_type.rb
Constant Summary collapse
- TOKEN =
/[!#$%&'*+\-.^_`|~0-9A-Z]+/i- MIME_TYPE =
/(?<type>#{TOKEN})\/(?<subtype>#{TOKEN})/- QUOTED_STRING =
/"(?:\\.|[^"\\])*"/- PARAMETER =
/\s*;\s*(?<key>#{TOKEN})=(?<value>#{TOKEN}|#{QUOTED_STRING})/
Instance Attribute Summary collapse
-
#media_type ⇒ Object
readonly
Returns the value of attribute media_type.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#subtype ⇒ Object
readonly
Returns the value of attribute subtype.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #hash ⇒ Object
-
#initialize(type, subtype, params) ⇒ ContentType
constructor
A new instance of ContentType.
- #qualified? ⇒ Boolean
- #quality ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(type, subtype, params) ⇒ ContentType
Returns a new instance of ContentType.
53 54 55 56 57 58 59 |
# File 'lib/steppe/content_type.rb', line 53 def initialize(type, subtype, params) @type = type.downcase @subtype = subtype.downcase @params = params @media_type = "#{type}/#{subtype}" freeze end |
Instance Attribute Details
#media_type ⇒ Object (readonly)
Returns the value of attribute media_type.
51 52 53 |
# File 'lib/steppe/content_type.rb', line 51 def media_type @media_type end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
51 52 53 |
# File 'lib/steppe/content_type.rb', line 51 def params @params end |
#subtype ⇒ Object (readonly)
Returns the value of attribute subtype.
51 52 53 |
# File 'lib/steppe/content_type.rb', line 51 def subtype @subtype end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
51 52 53 |
# File 'lib/steppe/content_type.rb', line 51 def type @type end |
Class Method Details
.parse(str) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/steppe/content_type.rb', line 12 def self.parse(str) return str if str.is_a?(ContentType) if str.is_a?(Symbol) str = Rack::Mime.mime_type(".#{str}") end m = MIME_TYPE.match(str) or raise ArgumentError, "invalid content type: #{str.inspect}" params = {} str.scan(PARAMETER) do key, value = Regexp.last_match.values_at(:key, :value) value = value[1..-2] if value&.start_with?('"') # unquote params[key.downcase] = value end new(m[:type], m[:subtype], params) end |
.parse_accept(header) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/steppe/content_type.rb', line 32 def self.parse_accept(header) header.split(/\s*,\s*/).map do |entry| # Match the MIME type part m = MIME_TYPE.match(entry) next unless m params = {} # Iterate over all parameters entry.scan(PARAMETER) do |match| key, value = Regexp.last_match.values_at(:key, :value) # Remove quotes if quoted value = value[1..-2] if value&.start_with?('"') params[key.downcase] = value end new(m[:type], m[:subtype], params) end.compact.sort_by { |ct| -ct.quality } end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
68 69 70 71 72 73 |
# File 'lib/steppe/content_type.rb', line 68 def ==(other) other.is_a?(ContentType) && type == other.type && subtype == other.subtype && params == other.params end |
#hash ⇒ Object
76 |
# File 'lib/steppe/content_type.rb', line 76 def hash = [type, subtype, params].hash |
#qualified? ⇒ Boolean
61 |
# File 'lib/steppe/content_type.rb', line 61 def qualified? = !(type == '*' && subtype == '*') |
#quality ⇒ Object
78 |
# File 'lib/steppe/content_type.rb', line 78 def quality = params.fetch('q', 1.0).to_f |
#to_s ⇒ Object
63 64 65 66 |
# File 'lib/steppe/content_type.rb', line 63 def to_s param_str = params.map { |k, v| "#{k}=#{v}" }.join('; ') [ media_type, param_str ].reject(&:empty?).join('; ') end |