convert AcceptList to a regular class

we never use this custom array outside the mime type `parse` method.  We
can reduce the interaction to just a regular array, so we should use
that instead (IOW, there was nothing special about AcceptList so we
should remove it).
This commit is contained in:
Aaron Patterson 2016-01-28 14:14:17 -08:00
parent c6d251f3a2
commit f8c9ef8401

@ -108,51 +108,50 @@ def <=>(item)
end
end
class AcceptList < Array #:nodoc:
def assort!
sort!
class AcceptList #:nodoc:
def self.sort!(list)
list.sort!
text_xml_idx = find_item_by_name self, 'text/xml'
app_xml_idx = find_item_by_name self, Mime[:xml].to_s
text_xml_idx = find_item_by_name list, 'text/xml'
app_xml_idx = find_item_by_name list, Mime[:xml].to_s
# Take care of the broken text/xml entry by renaming or deleting it
if text_xml_idx && app_xml_idx
app_xml = self[app_xml_idx]
text_xml = self[text_xml_idx]
app_xml = list[app_xml_idx]
text_xml = list[text_xml_idx]
app_xml.q = [text_xml.q, app_xml.q].max # set the q value to the max of the two
if app_xml_idx > text_xml_idx # make sure app_xml is ahead of text_xml in the list
self[app_xml_idx], self[text_xml_idx] = text_xml, app_xml
list[app_xml_idx], list[text_xml_idx] = text_xml, app_xml
app_xml_idx, text_xml_idx = text_xml_idx, app_xml_idx
end
delete_at(text_xml_idx) # delete text_xml from the list
list.delete_at(text_xml_idx) # delete text_xml from the list
elsif text_xml_idx
self[text_xml_idx].name = Mime[:xml].to_s
list[text_xml_idx].name = Mime[:xml].to_s
end
# Look for more specific XML-based types and sort them ahead of app/xml
if app_xml_idx
app_xml = self[app_xml_idx]
app_xml = list[app_xml_idx]
idx = app_xml_idx
while idx < length
type = self[idx]
while idx < list.length
type = list[idx]
break if type.q < app_xml.q
if type.name.ends_with? '+xml'
self[app_xml_idx], self[idx] = self[idx], app_xml
list[app_xml_idx], list[idx] = list[idx], app_xml
app_xml_idx = idx
end
idx += 1
end
end
map! { |i| Mime::Type.lookup(i.name) }.uniq!
to_a
list.map! { |i| Mime::Type.lookup(i.name) }.uniq!
list
end
private
def find_item_by_name(array, name)
def self.find_item_by_name(array, name)
array.index { |item| item.name == name }
end
end
@ -198,7 +197,7 @@ def parse(accept_header)
accept_header = accept_header.split(PARAMETER_SEPARATOR_REGEXP).first
parse_trailing_star(accept_header) || [Mime::Type.lookup(accept_header)].compact
else
list, index = AcceptList.new, 0
list, index = [], 0
accept_header.split(',').each do |header|
params, q = header.split(PARAMETER_SEPARATOR_REGEXP)
if params.present?
@ -212,7 +211,7 @@ def parse(accept_header)
end
end
end
list.assort!
AcceptList.sort! list
end
end