Class: Grape::Util::MediaType
- Inherits:
-
Object
- Object
- Grape::Util::MediaType
- Defined in:
- lib/grape/util/media_type.rb
Constant Summary collapse
- VENDOR_VERSION_HEADER_REGEX =
based on the HTTP Accept header with the pattern: application/vnd.:vendor-:version+:format
/\Avnd\.(?<vendor>[a-z0-9.\-_!^]+?)(?:-(?<version>[a-z0-9*.]+))?(?:\+(?<format>[a-z0-9*\-.]+))?\z/.freeze
Instance Attribute Summary collapse
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#subtype ⇒ Object
readonly
Returns the value of attribute subtype.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#vendor ⇒ Object
readonly
Returns the value of attribute vendor.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Class Method Summary collapse
- .best_quality(header, available_media_types) ⇒ Object
- .match?(media_type) ⇒ Boolean
- .parse(media_type) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(type:, subtype:) ⇒ MediaType
constructor
A new instance of MediaType.
Constructor Details
#initialize(type:, subtype:) ⇒ MediaType
Returns a new instance of MediaType.
12 13 14 15 16 17 18 19 20 |
# 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 |
Instance Attribute Details
#format ⇒ Object (readonly)
Returns the value of attribute format.
6 7 8 |
# File 'lib/grape/util/media_type.rb', line 6 def format @format end |
#subtype ⇒ Object (readonly)
Returns the value of attribute subtype.
6 7 8 |
# File 'lib/grape/util/media_type.rb', line 6 def subtype @subtype end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
6 7 8 |
# File 'lib/grape/util/media_type.rb', line 6 def type @type end |
#vendor ⇒ Object (readonly)
Returns the value of attribute vendor.
6 7 8 |
# File 'lib/grape/util/media_type.rb', line 6 def vendor @vendor end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
6 7 8 |
# File 'lib/grape/util/media_type.rb', line 6 def version @version end |
Class Method Details
.best_quality(header, available_media_types) ⇒ Object
40 41 42 |
# 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) ⇒ Boolean
53 54 55 56 57 58 59 60 |
# 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 |
.parse(media_type) ⇒ Object
44 45 46 47 48 49 50 51 |
# 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 |
Instance Method Details
#==(other) ⇒ Object
22 23 24 |
# File 'lib/grape/util/media_type.rb', line 22 def ==(other) eql?(other) end |
#eql?(other) ⇒ Boolean
26 27 28 29 30 31 32 33 |
# 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 ⇒ Object
35 36 37 |
# File 'lib/grape/util/media_type.rb', line 35 def hash [self.class, type, subtype, vendor, version, format].hash end |