Class: Mime::Type::AcceptList

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

Overview

:nodoc:

Class Method Summary collapse

Class Method Details

.find_item_by_name(array, name) ⇒ Object


156
157
158
# File 'actionpack/lib/action_dispatch/http/mime_type.rb', line 156

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

.sort!(list) ⇒ Object


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'actionpack/lib/action_dispatch/http/mime_type.rb', line 114

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.ends_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