Class: FakeWeb::Registry

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/fake_web/registry.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



7
8
9
# File 'lib/fake_web/registry.rb', line 7

def initialize
  clean_registry
end

Instance Attribute Details

#uri_mapObject

Returns the value of attribute uri_map.



5
6
7
# File 'lib/fake_web/registry.rb', line 5

def uri_map
  @uri_map
end

Instance Method Details

#clean_registryObject



11
12
13
14
15
# File 'lib/fake_web/registry.rb', line 11

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

#register_uri(method, uri, options) ⇒ Object



17
18
19
20
21
# File 'lib/fake_web/registry.rb', line 17

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

#registered_uri(method, uri) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fake_web/registry.rb', line 34

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

#registered_uri?(method, uri) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
# File 'lib/fake_web/registry.rb', line 23

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

#response_for(method, uri, &block) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fake_web/registry.rb', line 46

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

#unregister_uri(method, uri) ⇒ Object



28
29
30
31
32
# File 'lib/fake_web/registry.rb', line 28

def unregister_uri(method, uri)
  normalized_uri = normalize_uri(uri)
  return !!uri_map.delete(normalized_uri) if method == :any
  !!uri_map[normalized_uri].delete(method)
end