Method: RoadForest::ContentHandling::MediaType.parse

Defined in:
lib/roadforest/content-handling/media-type.rb

.parse(*args) ⇒ MediaType

Creates a new MediaType by parsing an alternate representation.

Parameters:

  • obj (MediaType, String, Array<String,Hash>)

    the raw type to be parsed

Returns:

Raises:

  • (ArgumentError)

    when the type could not be parsed



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/roadforest/content-handling/media-type.rb', line 18

def self.parse(*args)
  if args.length == 1
    obj = args.first
  else
    obj = args
  end

  if obj.is_a? MediaType
    obj
  elsif obj.is_a? String and !(match = MEDIA_TYPE_REGEX.match(obj)).nil?
    type, raw_params = *match[1,2]
    params = Hash[raw_params.scan(PARAMS_REGEX)]
    new(type, params)
  elsif Array === obj && String === obj[0] && Hash === obj[1]
    type = parse(obj[0])
    type.params.merge!(obj[1])
    type
  else
    raise ArgumentError, "Invalid media type #{obj.inspect}"
  end
end