Class: RoadForest::ContentHandling::MediaType

Inherits:
Object
  • Object
show all
Defined in:
lib/roadforest/content-handling/media-type.rb

Overview

Encapsulates a MIME media type, with logic for matching types.

Constant Summary collapse

MEDIA_TYPE_REGEX =

Matches valid media types

/^\s*([^;\s]+)\s*((?:;\s*\S+\s*)*)\s*$/
PARAMS_REGEX =

Matches sub-type parameters

/;\s*([^;=]+)(?:=([^;=\s]+))?/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, params = {}) ⇒ MediaType

Returns a new instance of MediaType.

Parameters:

  • type (String)

    the main media type, e.g. application/json

  • params (Hash) (defaults to: {})

    the media type parameters



48
49
50
51
# File 'lib/roadforest/content-handling/media-type.rb', line 48

def initialize(type, params={})
  @type, @params = type, params
  @quality = (@params.delete('q') || "1.0").to_f
end

Instance Attribute Details

#paramsHash

Returns any type parameters, e.g. charset.

Returns:

  • (Hash)

    any type parameters, e.g. charset



44
45
46
# File 'lib/roadforest/content-handling/media-type.rb', line 44

def params
  @params
end

#qualityObject (readonly)

Returns the value of attribute quality.



53
54
55
# File 'lib/roadforest/content-handling/media-type.rb', line 53

def quality
  @quality
end

#typeString

Returns the MIME media type.

Returns:

  • (String)

    the MIME media type



41
42
43
# File 'lib/roadforest/content-handling/media-type.rb', line 41

def type
  @type
end

Class Method Details

.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

Instance Method Details

#==(other) ⇒ true, false

Returns Are these two types strictly equal?.

Parameters:

  • other

    the other media type.

Returns:

  • (true, false)

    Are these two types strictly equal?

See Also:



64
65
66
67
# File 'lib/roadforest/content-handling/media-type.rb', line 64

def ==(other)
  other = self.class.parse(other)
  other.type == type && other.params == params
end

#accept_headerObject



106
107
108
# File 'lib/roadforest/content-handling/media-type.rb', line 106

def accept_header
  "#{type};q=#{quality}#{params_for_header}"
end

#content_type_headerObject Also known as: to_s



110
111
112
# File 'lib/roadforest/content-handling/media-type.rb', line 110

def content_type_header
  "#{type}#{params_for_header}"
end

#exact_match?(other) ⇒ true, false

Detects whether this RoadForest::ContentHandling::MediaType matches the other RoadForest::ContentHandling::MediaType, taking into account wildcards. Sub-type parameters are treated strictly.

Parameters:

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

    the other type

Returns:

  • (true, false)

    whether it is an acceptable match



74
75
76
77
# File 'lib/roadforest/content-handling/media-type.rb', line 74

def exact_match?(other)
  other = self.class.parse(other)
  type_matches?(other) && other.params == params
end

#majorString

Returns The major type, e.g. “application”, “text”, “image”.

Returns:

  • (String)

    The major type, e.g. “application”, “text”, “image”



116
117
118
# File 'lib/roadforest/content-handling/media-type.rb', line 116

def major
  @major ||= type.split("/").first
end

#match?(other) ⇒ true, false Also known as: =~

Detects whether the RoadForest::ContentHandling::MediaType is an acceptable match for the other RoadForest::ContentHandling::MediaType, taking into account wildcards and satisfying all requested parameters, but allowing this type to have extra specificity.

Parameters:

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

    the other type

Returns:

  • (true, false)

    whether it is an acceptable match



85
86
87
88
# File 'lib/roadforest/content-handling/media-type.rb', line 85

def match?(other)
  other = self.class.parse(other)
  type_matches?(other) && params_match?(other.params)
end

#matches_all?Boolean

Detects whether the RoadForest::ContentHandling::MediaType represents an open wildcard type, that is, “/” without any #params.

Returns:

  • (Boolean)


57
58
59
# File 'lib/roadforest/content-handling/media-type.rb', line 57

def matches_all?
  @type == "*/*" && @params.empty?
end

#minorString

Returns the minor or sub-type, e.g. “json”, “html”, “jpeg”.

Returns:

  • (String)

    the minor or sub-type, e.g. “json”, “html”, “jpeg”



121
122
123
# File 'lib/roadforest/content-handling/media-type.rb', line 121

def minor
  @minor ||= type.split("/").last
end

#params_for_headerObject



102
103
104
# File 'lib/roadforest/content-handling/media-type.rb', line 102

def params_for_header
  params.map {|k,v| ";#{k}#{v ? "=":""}#{v}" }.join("")
end

#params_match?(other) ⇒ true, false

Detects whether the passed sub-type parameters are all satisfied by this RoadForest::ContentHandling::MediaType. The receiver is allowed to have other params than the ones specified, but all specified must be equal.

Parameters:

  • params (Hash)

    the requested params

Returns:

  • (true, false)

    whether it is an acceptable match



96
97
98
99
100
# File 'lib/roadforest/content-handling/media-type.rb', line 96

def params_match?(other)
  other.all? do |k,v|
    params[k] == v
  end
end

#precedence_indexObject



125
126
127
128
129
130
131
# File 'lib/roadforest/content-handling/media-type.rb', line 125

def precedence_index
  [
    @major == "*" ? 0 : 1,
    @minor == "*" ? 0 : 1,
    (@params.keys - %w{q}).length
  ]
end

#type_matches?(other) ⇒ true, false

Returns whether the main media type is acceptable, ignoring params and taking into account wildcards.

Parameters:

Returns:

  • (true, false)

    whether the main media type is acceptable, ignoring params and taking into account wildcards



136
137
138
139
140
141
142
143
# File 'lib/roadforest/content-handling/media-type.rb', line 136

def type_matches?(other)
  other = self.class.parse(other)
  if ["*", "*/*", type].include?(other.type)
    true
  else
    other.major == major && other.minor == "*"
  end
end