141: def select_best_encoding(available_encodings, accept_encoding)
142:
143:
144: expanded_accept_encoding =
145: accept_encoding.map { |m, q|
146: if m == "*"
147: (available_encodings - accept_encoding.map { |m2, _| m2 }).map { |m2| [m2, q] }
148: else
149: [[m, q]]
150: end
151: }.inject([]) { |mem, list|
152: mem + list
153: }
154:
155: encoding_candidates = expanded_accept_encoding.sort_by { |_, q| -q }.map { |m, _| m }
156:
157: unless encoding_candidates.include?("identity")
158: encoding_candidates.push("identity")
159: end
160:
161: expanded_accept_encoding.find_all { |m, q|
162: q == 0.0
163: }.each { |m, _|
164: encoding_candidates.delete(m)
165: }
166:
167: return (encoding_candidates & available_encodings)[0]
168: end