class Grape::Util::MediaType

Constants

VENDOR_VERSION_HEADER_REGEX

based on the HTTP Accept header with the pattern: application/vnd.:vendor-:version+:format

Attributes

format[R]
subtype[R]
type[R]
vendor[R]
version[R]

Public Class Methods

best_quality(header, available_media_types) click to toggle source
# File lib/grape/util/media_type.rb, line 40
def best_quality(header, available_media_types)
  parse(best_quality_media_type(header, available_media_types))
end
match?(media_type) click to toggle source
# File lib/grape/util/media_type.rb, line 53
def match?(media_type)
  return false if media_type.blank?

  subtype = media_type.split('/', 2).last
  return false if subtype.blank?

  VENDOR_VERSION_HEADER_REGEX.match?(subtype)
end
new(type:, subtype:) click to toggle source
# File lib/grape/util/media_type.rb, line 12
def initialize(type:, subtype:)
  @type = type
  @subtype = subtype
  VENDOR_VERSION_HEADER_REGEX.match(subtype) do |m|
    @vendor = m[:vendor]
    @version = m[:version]
    @format = m[:format]
  end
end
parse(media_type) click to toggle source
# File lib/grape/util/media_type.rb, line 44
def parse(media_type)
  return if media_type.blank?

  type, subtype = media_type.split('/', 2)
  return if type.blank? || subtype.blank?

  new(type: type, subtype: subtype)
end

Private Class Methods

best_quality_media_type(header, available_media_types) click to toggle source
# File lib/grape/util/media_type.rb, line 62
def best_quality_media_type(header, available_media_types)
  header.blank? ? available_media_types.first : Rack::Utils.best_q_match(header, available_media_types)
end

Public Instance Methods

==(other) click to toggle source
# File lib/grape/util/media_type.rb, line 22
def ==(other)
  eql?(other)
end
eql?(other) click to toggle source
# File lib/grape/util/media_type.rb, line 26
def eql?(other)
  self.class == other.class &&
    other.type == type &&
    other.subtype == subtype &&
    other.vendor == vendor &&
    other.version == version &&
    other.format == format
end
hash() click to toggle source
# File lib/grape/util/media_type.rb, line 35
def hash
  [self.class, type, subtype, vendor, version, format].hash
end