-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create Grape::Util::MediaType Use Rack::Util functions
- Loading branch information
1 parent
e37831c
commit 33d8024
Showing
6 changed files
with
96 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# frozen_string_literal: true | ||
|
||
module Grape | ||
module Util | ||
class MediaType | ||
attr_reader :type, :subtype, :vendor, :version, :format | ||
|
||
VENDOR_VERSION_HEADER_REGEX = /\Avnd\.(?<vendor>[a-z0-9.\-_!^]+?)(?:-(?<version>[a-z0-9*.]+))?(?:\+(?<format>[a-z0-9*\-.]+))?\z/.freeze | ||
|
||
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 | ||
|
||
class << self | ||
|
||
def q_values(header) | ||
Rack::Utils.q_values(header) | ||
end | ||
|
||
def from_best_quality_media_type(header, available_media_types) | ||
parse(best_quality_media_type(header, available_media_types)) | ||
end | ||
|
||
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 | ||
|
||
def match?(media_type) | ||
return if media_type.blank? | ||
|
||
subtype = media_type.split('/', 2).last | ||
return if subtype.blank? | ||
|
||
VENDOR_VERSION_HEADER_REGEX.match?(subtype) | ||
end | ||
|
||
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 | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters