Class: MediaType

Inherits:
Struct
  • Object
show all
Defined in:
lib/media_type.rb

Constant Summary collapse

VERSION =
"1.0.2"
MATCHER =
/\A([^\/]+)\/(?:([^\.\+;]+)\.)?([^\s\+;]+)(?:\+([^\s;]+))?\s*(?:;(.*))?\Z/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#parametersObject

Returns the value of attribute parameters

Returns:

  • (Object)

    the current value of parameters



1
2
3
# File 'lib/media_type.rb', line 1

def parameters
  @parameters
end

#subtypeObject

Returns the value of attribute subtype

Returns:

  • (Object)

    the current value of subtype



1
2
3
# File 'lib/media_type.rb', line 1

def subtype
  @subtype
end

#suffixObject

Returns the value of attribute suffix

Returns:

  • (Object)

    the current value of suffix



1
2
3
# File 'lib/media_type.rb', line 1

def suffix
  @suffix
end

#treeObject

Returns the value of attribute tree

Returns:

  • (Object)

    the current value of tree



1
2
3
# File 'lib/media_type.rb', line 1

def tree
  @tree
end

#typeObject

Returns the value of attribute type

Returns:

  • (Object)

    the current value of 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

#inspectObject



55
56
57
# File 'lib/media_type.rb', line 55

def inspect
  "#<#{self.class}:#{to_s}>"
end

#parsed_parametersObject



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_parametersObject



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_sObject



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