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