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



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

def handle_request(method, url, headers, body)
  request = request_log.record(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))
      @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.message }
end

#handlersObject



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

Returns:

  • (Boolean)


35
36
37
38
39
# File 'lib/billy/handlers/request_handler.rb', line 35

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

#request_logObject



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

def request_log
  @request_log ||= RequestLog.new
end

#resetObject



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

def reset
  handlers.each_value(&:reset)
  request_log.reset
end

#reset_cacheObject



58
59
60
# File 'lib/billy/handlers/request_handler.rb', line 58

def reset_cache
  handlers[:cache].reset
end

#reset_stubsObject



54
55
56
# File 'lib/billy/handlers/request_handler.rb', line 54

def reset_stubs
  stub_handler.reset
end

#restore_cacheObject



62
63
64
65
# File 'lib/billy/handlers/request_handler.rb', line 62

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

#stubsObject



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

def stubs
  stub_handler.stubs
end