Class: RoadForest::ContentHandling::MediaTypeList

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMediaTypeList

Creates a PriorityList.

See Also:

  • PriorityList::build


168
169
170
# File 'lib/roadforest/content-handling/media-type.rb', line 168

def initialize
  @list = []
end

Class Method Details

.build(list) ⇒ Object

Given an acceptance list, create a PriorityList from them.



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/roadforest/content-handling/media-type.rb', line 148

def self.build(list)
  return list if self === list

  case list
  when Array
  when String
    list = list.split(/\s*,\s*/)
  else
    raise "Cannot build a MediaTypeList from #{list.inspect}"
  end

  new.tap do |plist|
    list.each {|item| plist.add_header_val(item) }
  end
end

Instance Method Details

#accept_headerObject Also known as: to_s



172
173
174
# File 'lib/roadforest/content-handling/media-type.rb', line 172

def accept_header
  @list.map(&:accept_header).join(", ")
end

#add(type) ⇒ Object

Adds an acceptable item with the given priority to the list.

Parameters:

  • q (Float)

    the priority

  • choice (String)

    the acceptable item



207
208
209
210
# File 'lib/roadforest/content-handling/media-type.rb', line 207

def add(type)
  @list << type
  self
end

#add_header_val(type_string) ⇒ Object

Given a raw acceptable value from an acceptance header, parse and add it to the list.

Parameters:

  • c (String)

    the raw acceptable item

See Also:



216
217
218
219
220
# File 'lib/roadforest/content-handling/media-type.rb', line 216

def add_header_val(type_string)
  add(MediaType.parse(type_string))
rescue ArgumentError
  raise "Invalid media type"
end

#best_match_from(other) ⇒ Object

Given another MediaTypeList, find the media type that is the best match between them - generally, the idea is to match an Accept header with a local list of provided types



180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/roadforest/content-handling/media-type.rb', line 180

def best_match_from(other)
  other.max_by do |their_type|
    best_type = self.by_precedence.find do |our_type|
      their_type =~ our_type
    end
    if best_type.nil?
      0
    else
      best_type.quality * their_type.quality
    end
  end
end

#by_precedenceObject



198
199
200
201
202
# File 'lib/roadforest/content-handling/media-type.rb', line 198

def by_precedence
  self.sort do |left, right|
    right.precedence_index <=> left.precedence_index
  end.enum_for(:each)
end

#each {|q, v| ... } ⇒ Object

Iterates over the list in priority order, that is, taking into account the order in which items were added as well as their priorities.

Yields:

  • (q, v)

Yield Parameters:

  • q (Float)

    the acceptable item’s priority

  • v (String)

    the acceptable item



228
229
230
231
232
233
# File 'lib/roadforest/content-handling/media-type.rb', line 228

def each
  return enum_for(:each) unless block_given?
  @list.each do |item|
    yield item
  end
end

#matches?(other) ⇒ Boolean

Returns:

  • (Boolean)


193
194
195
196
# File 'lib/roadforest/content-handling/media-type.rb', line 193

def matches?(other)
  type = best_match_from(other)
  include?(type) && other.include?(type)
end