Module: Rack::Acceptable::Media
- Included in:
- Request
- Defined in:
- lib/rack/acceptable/mixin/media.rb
Instance Method Summary collapse
-
#accept_media?(thing) ⇒ Boolean
Checks if the MIME-Type passed acceptable.
-
#acceptable_media ⇒ Object
Returns An Array with completely parsed MIME-Types (incl. qvalues and accept-extensions; see Rack::Acceptable::MIMETypes).
-
#best_media_for(thing) ⇒ Object
Returns the best match for the MIME-Type or pattern (like “text/*” etc) passed or
nil. - #negotiate_media(*things) ⇒ Object (also: #preferred_media_from)
Instance Method Details
#accept_media?(thing) ⇒ Boolean
Checks if the MIME-Type passed acceptable.
27 28 29 30 31 32 |
# File 'lib/rack/acceptable/mixin/media.rb', line 27 def accept_media?(thing) qvalue = MIMETypes.weigh_mime_type(thing, acceptable_media).first qvalue > 0 rescue false end |
#acceptable_media ⇒ Object
Returns
An Array with completely parsed MIME-Types (incl. qvalues and accept-extensions; see Rack::Acceptable::MIMETypes). Default qvalue is 1.0.
Raises
- ArgumentError
-
Syntax of the The Accept request-header is bad. For example, one of Media-Ranges is not in a RFC ‘Media-Range’ pattern (type or subtype is invalid, or there’s something like “*/foo”) or, at last, one of MIME-Types has malformed qvalue.
19 20 21 22 23 24 |
# File 'lib/rack/acceptable/mixin/media.rb', line 19 def acceptable_media @_acceptable_media ||= begin header = env[Const::ENV_HTTP_ACCEPT].to_s header.split(Utils::COMMA_SPLITTER).map! { |entry| MIMETypes.parse_mime_type(entry) } end end |
#best_media_for(thing) ⇒ Object
Returns the best match for the MIME-Type or pattern (like “text/*” etc) passed or nil.
36 37 38 39 40 41 42 43 |
# File 'lib/rack/acceptable/mixin/media.rb', line 36 def best_media_for(thing) weight = MIMETypes.weigh_mime_type(thing, acceptable_media) if weight.first > 0 acceptable_media.at(-weight.last) else nil end end |
#negotiate_media(*things) ⇒ Object Also known as: preferred_media_from
45 46 47 48 |
# File 'lib/rack/acceptable/mixin/media.rb', line 45 def negotiate_media(*things) flag = (things.last == true || things.last == false) ? things.pop : false MIMETypes.detect_best_mime_type(things, acceptable_media, flag) end |