Class: Jsapi::Media::Range

Inherits:
Object
  • Object
show all
Includes:
Comparable, TypeAndSubtype
Defined in:
lib/jsapi/media/range.rb

Overview

Represents a media range.

Constant Summary collapse

ALL =

Range for all types ("*/*").

Range.new('*', '*')
APPLICATION_JSON =

Range for JSON type ("application/json").

Range.new('application', 'json')

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TypeAndSubtype

#==, #hash, included, #initialize, #inspect, #to_s

Class Method Details

.from(value) ⇒ Object

Transforms value to an instance of this class.

Raises an ArgumentError when value could not be transformed.

Raises:

  • (ArgumentError)


22
23
24
25
26
27
# File 'lib/jsapi/media/range.rb', line 22

def from(value)
  media_range = try_from(value)
  return media_range unless media_range.nil?

  raise ArgumentError, "invalid media range: #{value.inspect}"
end

.reduce(media_ranges) ⇒ Object

Reduces the given collection of media ranges by removing media ranges that are already covered by another media range.



31
32
33
34
35
36
37
38
39
# File 'lib/jsapi/media/range.rb', line 31

def reduce(media_ranges)
  media_ranges.each_with_object([]) do |media_range, memo|
    media_range = from(media_range)
    if memo.none? { |other| other.cover?(media_range) }
      memo.delete_if { |other| media_range.cover?(other) }
      memo << media_range
    end
  end.sort
end

.try_from(value) ⇒ Object

Tries to transform value to an instance of this class.

Returns nil if value could not be transformed.



44
45
46
47
48
49
# File 'lib/jsapi/media/range.rb', line 44

def try_from(value)
  return value if value.is_a?(Range)

  type_and_subtype = pattern.match(value.to_s)&.captures
  new(*type_and_subtype) if type_and_subtype&.count == 2
end

Instance Method Details

#<=>(other) ⇒ Object

Compares it with other by priority.



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/jsapi/media/range.rb', line 62

def <=>(other)
  return unless other.is_a?(self.class)

  result = priority <=> other.priority
  return result unless result.zero?

  result = type <=> other.type
  return result unless result.zero?

  subtype <=> other.subtype
end

#cover?(other) ⇒ Boolean

Returns true if it covers other.

Returns:

  • (Boolean)


75
76
77
78
79
80
81
82
# File 'lib/jsapi/media/range.rb', line 75

def cover?(other)
  return if other.nil?

  other = Range.from(other)

  (type == '*' || type == other.type) &&
    (subtype == '*' || subtype == other.subtype)
end

#match?(media_type) ⇒ Boolean Also known as: =~

Returns true if the given media type matches the media range.

Returns:

  • (Boolean)


85
86
87
88
89
90
91
92
# File 'lib/jsapi/media/range.rb', line 85

def match?(media_type)
  return if media_type.nil?

  media_type = Type.from(media_type)

  (type == '*' || type == media_type.type) &&
    (subtype == '*' || subtype == media_type.subtype)
end

#priorityObject

Returns the level of priority of the media range.



97
98
99
# File 'lib/jsapi/media/range.rb', line 97

def priority
  @priority ||= (type == '*' ? 2 : 0) + (subtype == '*' ? 1 : 0) + 1
end