Class: RoadForest::ContentHandling::MediaType
- Inherits:
-
Object
- Object
- RoadForest::ContentHandling::MediaType
- Defined in:
- lib/roadforest/content-handling/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*$/
- PARAMS_REGEX =
Matches sub-type parameters
/;\s*([^;=]+)(?:=([^;=\s]+))?/
Instance Attribute Summary collapse
-
#params ⇒ Hash
Any type parameters, e.g.
-
#quality ⇒ Object
readonly
Returns the value of attribute quality.
-
#type ⇒ String
The MIME media type.
Class Method Summary collapse
-
.parse(*args) ⇒ MediaType
Creates a new MediaType by parsing an alternate representation.
Instance Method Summary collapse
-
#==(other) ⇒ true, false
Are these two types strictly equal?.
- #accept_header ⇒ Object
- #content_type_header ⇒ Object (also: #to_s)
- #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 (also: #=~)
- #matches_all? ⇒ Boolean
-
#minor ⇒ String
The minor or sub-type, e.g.
- #params_for_header ⇒ Object
-
#params_match?(other) ⇒ true, false
Detects whether the passed sub-type parameters are all satisfied by this MediaType.
- #precedence_index ⇒ Object
-
#type_matches?(other) ⇒ true, false
Whether the main media type is acceptable, ignoring params and taking into account wildcards.
Constructor Details
#initialize(type, params = {}) ⇒ MediaType
Returns a new instance of MediaType.
48 49 50 51 |
# File 'lib/roadforest/content-handling/media-type.rb', line 48 def initialize(type, params={}) @type, @params = type, params @quality = (@params.delete('q') || "1.0").to_f end |
Instance Attribute Details
#params ⇒ Hash
Returns any type parameters, e.g. charset.
44 45 46 |
# File 'lib/roadforest/content-handling/media-type.rb', line 44 def params @params end |
#quality ⇒ Object (readonly)
Returns the value of attribute quality.
53 54 55 |
# File 'lib/roadforest/content-handling/media-type.rb', line 53 def quality @quality end |
#type ⇒ String
Returns the MIME media type.
41 42 43 |
# File 'lib/roadforest/content-handling/media-type.rb', line 41 def type @type end |
Class Method Details
.parse(*args) ⇒ MediaType
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 35 36 37 38 |
# File 'lib/roadforest/content-handling/media-type.rb', line 18 def self.parse(*args) if args.length == 1 obj = args.first else obj = args end if obj.is_a? MediaType obj elsif obj.is_a? String and !(match = MEDIA_TYPE_REGEX.match(obj)).nil? type, raw_params = *match[1,2] params = Hash[raw_params.scan(PARAMS_REGEX)] new(type, params) elsif Array === obj && String === obj[0] && Hash === obj[1] type = parse(obj[0]) type.params.merge!(obj[1]) type else raise ArgumentError, "Invalid media type #{obj.inspect}" end end |
Instance Method Details
#==(other) ⇒ true, false
Returns Are these two types strictly equal?.
64 65 66 67 |
# File 'lib/roadforest/content-handling/media-type.rb', line 64 def ==(other) other = self.class.parse(other) other.type == type && other.params == params end |
#accept_header ⇒ Object
106 107 108 |
# File 'lib/roadforest/content-handling/media-type.rb', line 106 def accept_header "#{type};q=#{quality}#{params_for_header}" end |
#content_type_header ⇒ Object Also known as: to_s
110 111 112 |
# File 'lib/roadforest/content-handling/media-type.rb', line 110 def content_type_header "#{type}#{params_for_header}" end |
#exact_match?(other) ⇒ true, false
Detects whether this RoadForest::ContentHandling::MediaType matches the other RoadForest::ContentHandling::MediaType, taking into account wildcards. Sub-type parameters are treated strictly.
74 75 76 77 |
# File 'lib/roadforest/content-handling/media-type.rb', line 74 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”.
116 117 118 |
# File 'lib/roadforest/content-handling/media-type.rb', line 116 def major @major ||= type.split("/").first end |
#match?(other) ⇒ true, false Also known as: =~
Detects whether the RoadForest::ContentHandling::MediaType is an acceptable match for the other RoadForest::ContentHandling::MediaType, taking into account wildcards and satisfying all requested parameters, but allowing this type to have extra specificity.
85 86 87 88 |
# File 'lib/roadforest/content-handling/media-type.rb', line 85 def match?(other) other = self.class.parse(other) type_matches?(other) && params_match?(other.params) end |
#matches_all? ⇒ Boolean
Detects whether the RoadForest::ContentHandling::MediaType represents an open wildcard type, that is, “/” without any #params.
57 58 59 |
# File 'lib/roadforest/content-handling/media-type.rb', line 57 def matches_all? @type == "*/*" && @params.empty? end |
#minor ⇒ String
Returns the minor or sub-type, e.g. “json”, “html”, “jpeg”.
121 122 123 |
# File 'lib/roadforest/content-handling/media-type.rb', line 121 def minor @minor ||= type.split("/").last end |
#params_for_header ⇒ Object
102 103 104 |
# File 'lib/roadforest/content-handling/media-type.rb', line 102 def params_for_header params.map {|k,v| ";#{k}#{v ? "=":""}#{v}" }.join("") end |
#params_match?(other) ⇒ true, false
Detects whether the passed sub-type parameters are all satisfied by this RoadForest::ContentHandling::MediaType. The receiver is allowed to have other params than the ones specified, but all specified must be equal.
96 97 98 99 100 |
# File 'lib/roadforest/content-handling/media-type.rb', line 96 def params_match?(other) other.all? do |k,v| params[k] == v end end |
#precedence_index ⇒ Object
125 126 127 128 129 130 131 |
# File 'lib/roadforest/content-handling/media-type.rb', line 125 def precedence_index [ @major == "*" ? 0 : 1, @minor == "*" ? 0 : 1, (@params.keys - %w{q}).length ] end |
#type_matches?(other) ⇒ true, false
Returns whether the main media type is acceptable, ignoring params and taking into account wildcards.
136 137 138 139 140 141 142 143 |
# File 'lib/roadforest/content-handling/media-type.rb', line 136 def type_matches?(other) other = self.class.parse(other) if ["*", "*/*", type].include?(other.type) true else other.major == major && other.minor == "*" end end |