<commit_before>module FakeWeb
  class Registry #:nodoc:
    include Singleton

    attr_accessor :uri_map

    def initialize
      clean_registry
    end

    def clean_registry
      self.uri_map = Hash.new do |hash, key|
        hash[key] = Hash.new(&hash.default_proc)
      end
    end

    def register_uri(method, uri, options)
      uri_map[normalize_uri(uri)][method] = [*[options]].flatten.collect do |option|
        FakeWeb::Responder.new(method, uri, option, option[:times])
      end
    end

    def registered_uri?(method, uri)
      normalized_uri = normalize_uri(uri)
      uri_map[normalized_uri].has_key?(method) || uri_map[normalized_uri].has_key?(:any) ||
      uri_map_matches?(method, uri) || uri_map_matches?(:any, uri)
    end

    def responses_for(method, uri)
      uri = normalize_uri(uri)
      return nil unless registered_uri?(method, uri)

      if uri_map[uri].has_key?(method)
        uri_map[uri][method]
      elsif uri_map_matches?(method, uri)
        uri_map_matches(method, uri)
      elsif uri_map[uri].has_key?(:any)
        uri_map[uri][:any]
      elsif uri_map_matches(:any, uri)
        uri_map_matches(:any, uri)
      end
    end

    def response_for(method, uri, &block)
      responses = responses_for(method, uri)
      return nil if responses.nil?

      next_response = responses.last
      responses.each do |response|
        if response.times and response.times > 0
          response.times -= 1
          next_response = response
          break
        end
      end

      next_response.response(&block)
    end

    def uri_map_matches?(method, uri)<commit_msg>Refactoring: remove some duplication in the URI registry code<commit_after>module FakeWeb
  class Registry #:nodoc:
    include Singleton

    attr_accessor :uri_map

    def initialize
      clean_registry
    end

    def clean_registry
      self.uri_map = Hash.new do |hash, key|
        hash[key] = Hash.new(&hash.default_proc)
      end
    end

    def register_uri(method, uri, options)
      uri_map[normalize_uri(uri)][method] = [*[options]].flatten.collect do |option|
        FakeWeb::Responder.new(method, uri, option, option[:times])
      end
    end

    def registered_uri?(method, uri)
      normalized_uri = normalize_uri(uri)
      !responses_for(method, uri).nil?
    end

    def responses_for(method, uri)
      uri = normalize_uri(uri)

      if uri_map[uri].has_key?(method)
        uri_map[uri][method]
      elsif uri_map_matches?(method, uri)
        uri_map_matches(method, uri)
      elsif uri_map[uri].has_key?(:any)
        uri_map[uri][:any]
      elsif uri_map_matches(:any, uri)
        uri_map_matches(:any, uri)
      else
        nil
      end
    end

    def response_for(method, uri, &block)
      responses = responses_for(method, uri)
      return nil if responses.nil?

      next_response = responses.last
      responses.each do |response|
        if response.times and response.times > 0
          response.times -= 1
          next_response = response
          break
        end
      end

      next_response.response(&block)
    end

    def uri_map_matches?(method, uri)
{'fraction_non_alphanumeric': 0.09694850603941513, 'fraction_numerical': 0.0012714558169103624, 'mean_word_length': 2.617241379310345, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2232075', 'n_tokens_mistral': 1013, 'n_tokens_neox': 989, 'n_words': 222}