match_types (OpenID::Yadis)

    # File lib/openid/yadis/accept.rb, line 80
        def self.match_types(accept_types, have_types)
          # Given the result of parsing an Accept: header, and the
          # available MIME types, return the acceptable types with their
          # quality markdowns.
          #
          # For example:
          #
          # >>> acceptable = parse_accept_header('text/html, text/plain; q=0.5')
          # >>> matchTypes(acceptable, ['text/plain', 'text/html', 'image/jpeg'])
          # [('text/html', 1.0), ('text/plain', 0.5)]
          #
          # Type signature: ([(str, str, float)], [str]) -> [(str, float)]
          if accept_types.nil? or accept_types == []
            # Accept all of them
            default = 1
          else
            default = 0
          end

          match_main = {}
          match_sub = {}
          accept_types.each { |main, sub, q|
            if main == '*'
              default = [default, q].max
              next
            elsif sub == '*'
              match_main[main] = [match_main.fetch(main, 0), q].max
            else
              match_sub[[main, sub]] = [match_sub.fetch([main, sub], 0), q].max
            end
          }

          accepted_list = []
          order_maintainer = 0
          have_types.each { |mtype|
            main, sub = mtype.split('/', 2)
            if match_sub.member?([main, sub])
              q = match_sub[[main, sub]]
            else
              q = match_main.fetch(main, default)
            end

            if q != 0
              accepted_list << [1 - q, order_maintainer, q, mtype]
              order_maintainer += 1
            end
          }

          accepted_list.sort!
          return accepted_list.collect { |_, _, q, mtype| [mtype, q] }
        end