class RequestInterceptor

Constants

VERSION

Attributes

applications[R]
transactions[R]

Public Class Methods

define(super_class = nil, &application_definition) click to toggle source
# File lib/request_interceptor.rb, line 45
def self.define(super_class = nil, &application_definition)
  Class.new(super_class || template, &application_definition)
end
new(applications) click to toggle source
# File lib/request_interceptor.rb, line 56
def initialize(applications)
  @applications = applications.map { |pattern, application| ApplicationWrapper.new(pattern, application) }
  @transactions = []
end
run(applications, &simulation) click to toggle source
# File lib/request_interceptor.rb, line 49
def self.run(applications, &simulation)
  new(applications).run(&simulation)
end
template() click to toggle source
# File lib/request_interceptor.rb, line 41
def self.template
  @template || Application
end
template=(template) click to toggle source
# File lib/request_interceptor.rb, line 31
def self.template=(template)
  @template =
    case template
    when Proc
      Class.new(Application, &template)
    else
      template
    end
end

Public Instance Methods

run(&simulation) click to toggle source
# File lib/request_interceptor.rb, line 61
def run(&simulation)
  transactions = []

  request_logging = ->(request, response) do
    next unless applications.any? { |application| application.intercepts?(request.uri) }
    transactions << Transaction.new(
      request: Transaction::Request.new(
        method: request.method,
        uri: URI(request.uri.to_s),
        headers: request.headers,
        body: request.body
      ),
      response: Transaction::Response.new(
        status_code: response.status,
        headers: response.headers,
        body: response.body
      )
    )
  end

  WebMockManager.new(applications, request_logging).run_simulation(&simulation)

  transactions
end