class Webmachine::MediaType

Encapsulates a MIME media type, with logic for matching types.

Constants

MEDIA_TYPE_REGEX

Matches valid media types

PARAMS_REGEX

Matches sub-type parameters

Attributes

params[RW]

@return [Hash] any type parameters, e.g. charset

type[RW]

@return [String] the MIME media type

Public Class Methods

new(type, params = {}) click to toggle source

@param [String] type the main media type, e.g. application/json @param [Hash] params the media type parameters

# File lib/webmachine/media_type.rb, line 46
def initialize(type, params = {})
  @type, @params = type, params
end
parse(obj) click to toggle source

Creates a new MediaType by parsing an alternate representation. @param [MediaType, String, Array<String,Hash>] obj the raw type

to be parsed

@return [MediaType] the parsed media type @raise [ArgumentError] when the type could not be parsed

# 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 = raw_params.scan(PARAMS_REGEX).map { |m| [m[0], m[2].to_s] }.to_h
    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

Public Instance Methods

==(other) click to toggle source

@return [true,false] Are these two types strictly equal? @param other the other media type. @see MediaType.parse

# 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) click to toggle source

Detects whether this {MediaType} matches the other {MediaType}, taking into account wildcards. Sub-type parameters are treated strictly. @param [MediaType, String, Array<String,Hash>] other the other type @return [true,false] whether it is an acceptable match

# 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() click to toggle source

@return [String] The major type, e.g. “application”, “text”, “image”

# File lib/webmachine/media_type.rb, line 101
def major
  type.split(SLASH).first
end
match?(other) click to toggle source

Detects whether the {MediaType} is an acceptable match for the other {MediaType}, taking into account wildcards and satisfying all requested parameters, but allowing this type to have extra specificity. @param [MediaType, String, Array<String,Hash>] other the other type @return [true,false] whether it is an acceptable match

# 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?() click to toggle source

Detects whether the {MediaType} represents an open wildcard type, that is, “/” without any {#params}.

# File lib/webmachine/media_type.rb, line 52
def matches_all?
  @type == MATCHES_ALL && @params.empty?
end
minor() click to toggle source

@return [String] the minor or sub-type, e.g. “json”, “html”, “jpeg”

# File lib/webmachine/media_type.rb, line 106
def minor
  type.split(SLASH).last
end
params_match?(other) click to toggle source

Detects whether the passed sub-type parameters are all satisfied by this {MediaType}. The receiver is allowed to have other params than the ones specified, but all specified must be equal. @param [Hash] params the requested params @return [true,false] whether it is an acceptable match

# File lib/webmachine/media_type.rb, line 90
def params_match?(other)
  other.all? { |k, v| params[k] == v }
end
to_s() click to toggle source

Reconstitutes the type into a String @return [String] the type as a String

# File lib/webmachine/media_type.rb, line 96
def to_s
  [type, *params.map { |k, v| "#{k}=#{v}" }].join(';')
end
type_matches?(other) click to toggle source

@param [MediaType] other the other type @return [true,false] whether the main media type is acceptable,

ignoring params and taking into account wildcards
# 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