Class: Datadog::AppSec::Utils::HTTP::MediaType
- Inherits:
-
Object
- Object
- Datadog::AppSec::Utils::HTTP::MediaType
- Defined in:
- lib/datadog/appsec/utils/http/media_type.rb
Overview
Implementation of media type for content negotiation
See:
Defined Under Namespace
Classes: ParseError
Constant Summary collapse
- WILDCARD =
'*'.freeze
- TOKEN_RE =
/[-#$%&'*+.^_`|~A-Za-z0-9]+/.freeze
- PARAMETER_RE =
%r{ # rubocop:disable Style/RegexpLiteral (?: (?<parameter_name>#{TOKEN_RE}) = (?: (?<parameter_value>#{TOKEN_RE}) | "(?<parameter_value>[^"]+)" ) ) }ix.freeze
- MEDIA_TYPE_RE =
%r{ \A (?<type>#{TOKEN_RE})/(?<subtype>#{TOKEN_RE}) (?<parameters> (?: \s*;\s* #{PARAMETER_RE} )* ) \Z }ix.freeze
Instance Attribute Summary collapse
-
#parameters ⇒ Object
readonly
Returns the value of attribute parameters.
-
#subtype ⇒ Object
readonly
Returns the value of attribute subtype.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#initialize(media_type) ⇒ MediaType
constructor
A new instance of MediaType.
- #to_s ⇒ Object
Constructor Details
#initialize(media_type) ⇒ MediaType
Returns a new instance of MediaType.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/datadog/appsec/utils/http/media_type.rb', line 47 def initialize(media_type) media_type_match = MEDIA_TYPE_RE.match(media_type) raise ParseError, media_type.inspect if media_type_match.nil? @type = (media_type_match['type'] || WILDCARD).downcase @subtype = (media_type_match['subtype'] || WILDCARD).downcase @parameters = {} parameters = media_type_match['parameters'] return if parameters.nil? parameters.split(';').map(&:strip).each do |parameter| parameter_match = PARAMETER_RE.match(parameter) next if parameter_match.nil? parameter_name = parameter_match['parameter_name'] parameter_value = parameter_match['parameter_value'] next if parameter_name.nil? || parameter_value.nil? @parameters[parameter_name.downcase] = parameter_value.downcase end end |
Instance Attribute Details
#parameters ⇒ Object (readonly)
Returns the value of attribute parameters.
45 46 47 |
# File 'lib/datadog/appsec/utils/http/media_type.rb', line 45 def parameters @parameters end |
#subtype ⇒ Object (readonly)
Returns the value of attribute subtype.
45 46 47 |
# File 'lib/datadog/appsec/utils/http/media_type.rb', line 45 def subtype @subtype end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
45 46 47 |
# File 'lib/datadog/appsec/utils/http/media_type.rb', line 45 def type @type end |
Instance Method Details
#to_s ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/datadog/appsec/utils/http/media_type.rb', line 74 def to_s s = "#{@type}/#{@subtype}" s << ';' << @parameters.map { |k, v| "#{k}=#{v}" }.join(';') if @parameters.count > 0 s end |