Class: Webmachine::MediaType
- Inherits:
-
Object
- Object
- Webmachine::MediaType
- Extended by:
- Translation
- Defined in:
- lib/webmachine/media_type.rb
Overview
Encapsulates a MIME media type, with logic for matching types.
Constant Summary
- MEDIA_TYPE_REGEX =
Matches valid media types
/^\s*([^;\s]+)\s*((?:;\s*\S+\s*)*)\s*$/- PARAMS_REGEX =
Matches sub-type parameters
/;\s*([^=]+)=([^;=\s]+)/
Instance Attribute Summary (collapse)
-
- (Hash) params
Any type parameters, e.g.
-
- (String) type
The MIME media type.
Class Method Summary (collapse)
-
+ (MediaType) parse(obj)
Creates a new MediaType by parsing an alternate representation.
Instance Method Summary (collapse)
-
- (true, false) ==(other)
Are these two types strictly equal?.
- - (true, false) exact_match?(other)
-
- (MediaType) initialize(type, params = {})
constructor
A new instance of MediaType.
-
- (String) major
The major type, e.g.
- - (true, false) match?(other)
- - (Boolean) matches_all?
-
- (String) minor
The minor or sub-type, e.g.
-
- (true, false) params_match?(other)
Detects whether the passed sub-type parameters are all satisfied by this MediaType.
-
- (String) to_s
Reconstitutes the type into a String.
-
- (true, false) type_matches?(other)
Whether the main media type is acceptable, ignoring params and taking into account wildcards.
Methods included from Translation
Constructor Details
- (MediaType) initialize(type, params = {})
A new instance of MediaType
44 45 46 |
# File 'lib/webmachine/media_type.rb', line 44 def initialize(type, params={}) @type, @params = type, params end |
Instance Attribute Details
- (Hash) params
Any type parameters, e.g. charset
40 41 42 |
# File 'lib/webmachine/media_type.rb', line 40 def params @params end |
- (String) type
The MIME media type
37 38 39 |
# File 'lib/webmachine/media_type.rb', line 37 def type @type end |
Class Method Details
+ (MediaType) parse(obj)
Creates a new MediaType by parsing an alternate representation.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/webmachine/media_type.rb', line 18 def self.parse(obj) case obj when MediaType obj when MEDIA_TYPE_REGEX type, raw_params = $1, $2 params = Hash[raw_params.scan(PARAMS_REGEX)] new(type, params) else unless Array === obj && String === obj[0] && Hash === obj[1] raise ArgumentError, t('invalid_media_type', :type => obj.inspect) end type = parse(obj[0]) type.params.merge!(obj[1]) type end end |
Instance Method Details
- (true, false) ==(other)
Are these two types strictly equal?
57 58 59 60 |
# File 'lib/webmachine/media_type.rb', line 57 def ==(other) other = self.class.parse(other) other.type == type && other.params == params end |
- (true, false) exact_match?(other)
Detects whether this Webmachine::MediaType matches the other Webmachine::MediaType, taking into account wildcards. Sub-type parameters are treated strictly.
67 68 69 70 |
# File 'lib/webmachine/media_type.rb', line 67 def exact_match?(other) other = self.class.parse(other) type_matches?(other) && other.params == params end |
- (String) major
The major type, e.g. “application”, “text”, “image”
99 100 101 |
# File 'lib/webmachine/media_type.rb', line 99 def major type.split("/").first end |
- (true, false) match?(other)
Detects whether the Webmachine::MediaType is an acceptable match for the other Webmachine::MediaType, taking into account wildcards and satisfying all requested parameters, but allowing this type to have extra specificity.
78 79 80 81 |
# File 'lib/webmachine/media_type.rb', line 78 def match?(other) other = self.class.parse(other) type_matches?(other) && params_match?(other.params) end |
- (Boolean) matches_all?
Detects whether the Webmachine::MediaType represents an open wildcard type, that is, “/” without any #params.
50 51 52 |
# File 'lib/webmachine/media_type.rb', line 50 def matches_all? @type == "*/*" && @params.empty? end |
- (String) minor
The minor or sub-type, e.g. “json”, “html”, “jpeg”
104 105 106 |
# File 'lib/webmachine/media_type.rb', line 104 def minor type.split("/").last end |
- (true, false) params_match?(other)
Detects whether the passed sub-type parameters are all satisfied by this Webmachine::MediaType. The receiver is allowed to have other params than the ones specified, but all specified must be equal.
88 89 90 |
# File 'lib/webmachine/media_type.rb', line 88 def params_match?(other) other.all? {|k,v| params[k] == v } end |
- (String) to_s
Reconstitutes the type into a String
94 95 96 |
# File 'lib/webmachine/media_type.rb', line 94 def to_s [type, *params.map {|k,v| "#{k}=#{v}" }].join(";") end |
- (true, false) type_matches?(other)
Whether the main media type is acceptable, ignoring params and taking into account wildcards
111 112 113 114 115 116 117 118 |
# File 'lib/webmachine/media_type.rb', line 111 def type_matches?(other) other = self.class.parse(other) if ["*", "*/*", type].include?(other.type) true else other.major == major && other.minor == "*" end end |