Class: MediaType
- Inherits:
-
Struct
- Object
- Struct
- MediaType
- Defined in:
- lib/media_type.rb
Constant Summary collapse
- VERSION =
"1.0.2"- MATCHER =
/\A([^\/]+)\/(?:([^\.\+;]+)\.)?([^\s\+;]+)(?:\+([^\s;]+))?\s*(?:;(.*))?\Z/
Instance Attribute Summary collapse
-
#parameters ⇒ Object
Returns the value of attribute parameters.
-
#subtype ⇒ Object
Returns the value of attribute subtype.
-
#suffix ⇒ Object
Returns the value of attribute suffix.
-
#tree ⇒ Object
Returns the value of attribute tree.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
- .encode_params(params) ⇒ Object
- .parse(string, parse_parameters: true) ⇒ Object
- .parse_params(string) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(*args) ⇒ MediaType
constructor
A new instance of MediaType.
- #inspect ⇒ Object
- #parsed_parameters ⇒ Object
- #string_parameters ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(*args) ⇒ MediaType
Returns a new instance of MediaType.
5 6 7 8 9 10 11 12 |
# File 'lib/media_type.rb', line 5 def initialize(*args) if args.length == 1 && (hash = args.first).is_a?(Hash) symbolized_hash = hash.map { |k, v| [k.to_sym, v] }.to_h super(*symbolized_hash.values_at(*self.class.members)) else super end end |
Instance Attribute Details
#parameters ⇒ Object
Returns the value of attribute parameters
1 2 3 |
# File 'lib/media_type.rb', line 1 def parameters @parameters end |
#subtype ⇒ Object
Returns the value of attribute subtype
1 2 3 |
# File 'lib/media_type.rb', line 1 def subtype @subtype end |
#suffix ⇒ Object
Returns the value of attribute suffix
1 2 3 |
# File 'lib/media_type.rb', line 1 def suffix @suffix end |
#tree ⇒ Object
Returns the value of attribute tree
1 2 3 |
# File 'lib/media_type.rb', line 1 def tree @tree end |
#type ⇒ Object
Returns the value of attribute type
1 2 3 |
# File 'lib/media_type.rb', line 1 def type @type end |
Class Method Details
.encode_params(params) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/media_type.rb', line 39 def self.encode_params(params) params.map do |key, value| value = %("#{value.gsub(?", '\"')}") if value[/["\s]/] "#{key}=#{value}" end.join("; ") end |
.parse(string, parse_parameters: true) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/media_type.rb', line 14 def self.parse(string, parse_parameters: true) components = string.match(MATCHER)[1..-1] if parse_parameters components[-1] = parse_params(components[-1]) end new(*components) end |
.parse_params(string) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/media_type.rb', line 22 def self.parse_params(string) return nil if string.nil? components = string.split(/(?<!\\)"/) # seperate quoted strings components.map!.with_index do |element, i| if i.even? # not inside a quoted string element.split(?;). map(&:strip). reject(&:empty?). map { |s| s.split(?=).map(&:strip) } # seperate keys and values else # inside a quoted string element.gsub(/\\(.)/, '\1') # remove escapes end end components.flatten! # now 1D array of strings, alternating key/value components.each_slice(2).to_h end |
Instance Method Details
#==(other) ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/media_type.rb', line 59 def ==(other) return false unless other.kind_of? MediaType [self, other].map do |type| hash = type.to_h hash[:parameters] &&= type.parsed_parameters hash end.inject(:==) end |
#inspect ⇒ Object
55 56 57 |
# File 'lib/media_type.rb', line 55 def inspect "#<#{self.class}:#{to_s}>" end |
#parsed_parameters ⇒ Object
72 73 74 |
# File 'lib/media_type.rb', line 72 def parsed_parameters parameters.is_a?(Hash) ? parameters : self.class.parse_params(parameters) end |
#string_parameters ⇒ Object
68 69 70 |
# File 'lib/media_type.rb', line 68 def string_parameters parameters.is_a?(Hash) ? self.class.encode_params(parameters) : parameters end |
#to_s ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/media_type.rb', line 46 def to_s string = "#{type}/" string << "#{tree}." if tree string << subtype string << "+#{suffix}" if suffix string << "; #{string_parameters.gsub(/\A\s+/, "")}" if parameters string end |