Class: Mime::Type::AcceptList

Inherits:
Object
  • Object
show all
Defined in:
lib/action_dispatch/http/mime_type.rb

Overview

:nodoc:

Class Method Summary collapse

Class Method Details

.find_item_by_name(array, name) ⇒ Object



131
132
133
# File 'lib/action_dispatch/http/mime_type.rb', line 131

def self.find_item_by_name(array, name)
  array.index { |item| item.name == name }
end

.sort!(list) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/action_dispatch/http/mime_type.rb', line 89

def self.sort!(list)
  list.sort!

  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 = 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.
      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
    list.delete_at(text_xml_idx)  # Delete text_xml from the list.
  elsif text_xml_idx
    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 = list[app_xml_idx]
    idx = app_xml_idx

    while idx < list.length
      type = list[idx]
      break if type.q < app_xml.q

      if type.name.end_with? "+xml"
        list[app_xml_idx], list[idx] = list[idx], app_xml
        app_xml_idx = idx
      end
      idx += 1
    end
  end

  list.map! { |i| Mime::Type.lookup(i.name) }.uniq!
  list
end