Class: Billy::RequestHandler
- Inherits:
-
Object
- Object
- Billy::RequestHandler
- Extended by:
- Forwardable
- Includes:
- Handler
- Defined in:
- lib/billy/handlers/request_handler.rb
Instance Method Summary collapse
- #handle_request(method, url, headers, body) ⇒ Object
- #handlers ⇒ Object
- #handles_request?(method, url, headers, body) ⇒ Boolean
- #request_log ⇒ Object
- #reset ⇒ Object
- #reset_cache ⇒ Object
- #reset_stubs ⇒ Object
- #restore_cache ⇒ Object
- #stubs ⇒ Object
Instance Method Details
#handle_request(method, url, headers, body) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/billy/handlers/request_handler.rb', line 17 def handle_request(method, url, headers, body) request = request_log.record(method, url, headers, body) if Billy.config.before_handle_request method, url, headers, body = Billy.config.before_handle_request.call(method, url, headers, body) end # Process the handlers by order of importance [:stubs, :cache, :proxy].each do |key| if (response = handlers[key].handle_request(method, url, headers, body)) @request_log.complete(request, key) return response end end body_msg = Billy.config.cache_request_body_methods.include?(method) ? " with body '#{body}'" : '' request_log.complete(request, :error) { error: "Connection to #{url}#{body_msg} not cached and new http connections are disabled" } rescue => error { error: error. } end |
#handlers ⇒ Object
11 12 13 14 15 |
# File 'lib/billy/handlers/request_handler.rb', line 11 def handlers @handlers ||= { stubs: StubHandler.new, cache: CacheHandler.new, proxy: ProxyHandler.new } end |
#handles_request?(method, url, headers, body) ⇒ Boolean
39 40 41 42 43 |
# File 'lib/billy/handlers/request_handler.rb', line 39 def handles_request?(method, url, headers, body) [:stubs, :cache, :proxy].any? do |key| handlers[key].handles_request?(method, url, headers, body) end end |
#request_log ⇒ Object
45 46 47 |
# File 'lib/billy/handlers/request_handler.rb', line 45 def request_log @request_log ||= RequestLog.new end |
#reset ⇒ Object
53 54 55 56 |
# File 'lib/billy/handlers/request_handler.rb', line 53 def reset handlers.each_value(&:reset) request_log.reset end |
#reset_cache ⇒ Object
62 63 64 |
# File 'lib/billy/handlers/request_handler.rb', line 62 def reset_cache handlers[:cache].reset end |
#reset_stubs ⇒ Object
58 59 60 |
# File 'lib/billy/handlers/request_handler.rb', line 58 def reset_stubs stub_handler.reset end |
#restore_cache ⇒ Object
66 67 68 69 |
# File 'lib/billy/handlers/request_handler.rb', line 66 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 |
#stubs ⇒ Object
49 50 51 |
# File 'lib/billy/handlers/request_handler.rb', line 49 def stubs stub_handler.stubs end |