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 collapse
- MEDIA_TYPE_REGEX =
Matches valid media types
/^\s*([^;\s]+)\s*((?:;\s*\S+\s*)*)\s*$/.freeze
- PARAMS_REGEX =
Matches sub-type parameters
/;\s*([^=]+)(=([^;=\s]*))?/.freeze
Instance Attribute Summary collapse
-
#params ⇒ Hash
Any type parameters, e.g.
-
#type ⇒ String
The MIME media type.
Class Method Summary collapse
-
.parse(obj) ⇒ MediaType
Creates a new MediaType by parsing an alternate representation.
Instance Method Summary collapse
-
#==(other) ⇒ true, false
Are these two types strictly equal?.
- #exact_match?(other) ⇒ true, false
-
#initialize(type, params = {}) ⇒ MediaType
constructor
A new instance of MediaType.
-
#major ⇒ String
The major type, e.g.
- #match?(other) ⇒ true, false
- #matches_all? ⇒ Boolean
-
#minor ⇒ String
The minor or sub-type, e.g.
-
#params_match?(other) ⇒ true, false
Detects whether the passed sub-type parameters are all satisfied by this MediaType.
-
#to_s ⇒ String
Reconstitutes the type into a String.
-
#type_matches?(other) ⇒ true, false
Whether the main media type is acceptable, ignoring params and taking into account wildcards.
Methods included from Translation
Constructor Details
#initialize(type, params = {}) ⇒ MediaType
Returns a new instance of MediaType.
46 47 48 |
# File 'lib/webmachine/media_type.rb', line 46 def initialize(type, params={}) @type, @params = type, params end |
Instance Attribute Details
#params ⇒ Hash
Returns any type parameters, e.g. charset.
42 43 44 |
# File 'lib/webmachine/media_type.rb', line 42 def params @params end |
#type ⇒ String
Returns the MIME media type.
39 40 41 |
# File 'lib/webmachine/media_type.rb', line 39 def type @type end |
Class Method Details
.parse(obj) ⇒ MediaType
Creates a new MediaType by parsing an alternate representation.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/webmachine/media_type.rb', line 20 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).map { |m| [m[0], m[2].to_s] }] 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
#==(other) ⇒ true, false
Returns Are these two types strictly equal?.
59 60 61 62 |
# File 'lib/webmachine/media_type.rb', line 59 def ==(other) other = self.class.parse(other) other.type == type && other.params == params end |
#exact_match?(other) ⇒ true, false
Detects whether this Webmachine::MediaType matches the other Webmachine::MediaType, taking into account wildcards. Sub-type parameters are treated strictly.
69 70 71 72 |
# File 'lib/webmachine/media_type.rb', line 69 def exact_match?(other) other = self.class.parse(other) type_matches?(other) && other.params == params end |
#major ⇒ String
Returns The major type, e.g. “application”, “text”, “image”.
101 102 103 |
# File 'lib/webmachine/media_type.rb', line 101 def major type.split(SLASH).first end |
#match?(other) ⇒ true, false
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.
80 81 82 83 |
# File 'lib/webmachine/media_type.rb', line 80 def match?(other) other = self.class.parse(other) type_matches?(other) && params_match?(other.params) end |
#matches_all? ⇒ Boolean
Detects whether the Webmachine::MediaType represents an open wildcard type, that is, “/” without any #params.
52 53 54 |
# File 'lib/webmachine/media_type.rb', line 52 def matches_all? @type == MATCHES_ALL && @params.empty? end |
#minor ⇒ String
Returns the minor or sub-type, e.g. “json”, “html”, “jpeg”.
106 107 108 |
# File 'lib/webmachine/media_type.rb', line 106 def minor type.split(SLASH).last end |
#params_match?(other) ⇒ true, false
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.
90 91 92 |
# File 'lib/webmachine/media_type.rb', line 90 def params_match?(other) other.all? {|k,v| params[k] == v } end |
#to_s ⇒ String
Reconstitutes the type into a String
96 97 98 |
# File 'lib/webmachine/media_type.rb', line 96 def to_s [type, *params.map {|k,v| "#{k}=#{v}" }].join(";") end |
#type_matches?(other) ⇒ true, false
Returns whether the main media type is acceptable, ignoring params and taking into account wildcards.
113 114 115 116 117 118 119 120 |
# File 'lib/webmachine/media_type.rb', line 113 def type_matches?(other) other = self.class.parse(other) if [Dispatcher::Route::MATCH_ALL_STR, MATCHES_ALL, type].include?(other.type) true else other.major == major && other.minor == Dispatcher::Route::MATCH_ALL_STR end end |