Class: Billy::RequestHandler

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Handler
Defined in:
lib/billy/handlers/request_handler.rb

Instance Method Summary collapse

Instance Method Details

#handle_request(method, url, headers, body) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/billy/handlers/request_handler.rb', line 16

def handle_request(method, url, headers, body)
  # Process the handlers by order of importance
  [:stubs, :cache, :proxy].each do |key|
    if (response = handlers[key].handle_request(method, url, headers, body))
      return response
    end
  end

  body_msg = Billy.config.cache_request_body_methods.include?(method) ? " with body '#{body}'" : ''
  { error: "Connection to #{url}#{body_msg} not cached and new http connections are disabled" }
rescue => error
  { error: error.message }
end

#handlersObject



10
11
12
13
14
# File 'lib/billy/handlers/request_handler.rb', line 10

def handlers
  @handlers ||= { stubs: StubHandler.new,
                  cache: CacheHandler.new,
                  proxy: ProxyHandler.new }
end

#handles_request?(method, url, headers, body) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
# File 'lib/billy/handlers/request_handler.rb', line 30

def handles_request?(method, url, headers, body)
  [:stubs, :cache, :proxy].each do |key|
    return true if handlers[key].handles_request?(method, url, headers, body)
  end

  false
end

#resetObject



38
39
40
# File 'lib/billy/handlers/request_handler.rb', line 38

def reset
  handlers.each_value(&:reset)
end

#reset_cacheObject



46
47
48
# File 'lib/billy/handlers/request_handler.rb', line 46

def reset_cache
  handlers[:cache].reset
end

#reset_stubsObject



42
43
44
# File 'lib/billy/handlers/request_handler.rb', line 42

def reset_stubs
  handlers[:stubs].reset
end

#restore_cacheObject



50
51
52
53
# File 'lib/billy/handlers/request_handler.rb', line 50

def restore_cache
  warn '[DEPRECATION] `restore_cache` is deprecated as cache files are dynamically checked. Use `reset_cache` if you just want to clear the cache.'
  reset_cache
end