Sha256: 6bae340581f5b651f97866790dbf4168605683504e73622b2a7f46e84b7cd2ef

Contents?: true

Size: 1.76 KB

Versions: 6

Compression:

Stored size: 1.76 KB

Contents

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)
    end

    def registered_uri(method, uri)
      uri = normalize_uri(uri)
      registered = registered_uri?(method, uri)
      if registered && uri_map[uri].has_key?(method)
        uri_map[uri][method]
      elsif registered
        uri_map[uri][:any]
      else
        nil
      end
    end

    def response_for(method, uri, &block)
      responses = registered_uri(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

    private

    def normalize_uri(uri)
      case uri
      when URI then uri
      else
        uri = 'http://' + uri unless uri.match('^https?://')
        parsed_uri = URI.parse(uri)
        parsed_uri.query = sort_query_params(parsed_uri.query)
        parsed_uri
      end
    end

    def sort_query_params(query)
      if query.nil? || query.empty?
        nil
      else
        query.split('&').sort.join('&')
      end
    end

  end
end

Version data entries

6 entries across 6 versions & 4 rubygems

Version Path
MarkMenard-fakeweb-1.1.2.6 lib/fake_web/registry.rb
chrisk-fakeweb-1.1.2.7 lib/fake_web/registry.rb
chrisk-fakeweb-1.2.0 lib/fake_web/registry.rb
mbleigh-fakeweb-1.1.3.8 lib/fake_web/registry.rb
mbleigh-fakeweb-1.1.3.9 lib/fake_web/registry.rb
fakeweb-1.2.0 lib/fake_web/registry.rb