Class: RequestInterceptor

Inherits:
Object
  • Object
show all
Defined in:
lib/request_interceptor.rb,
lib/request_interceptor/version.rb

Defined Under Namespace

Modules: Matchers, WebMockPatches Classes: Application, ApplicationWrapper, Transaction, WebMockManager

Constant Summary collapse

VERSION =
"1.0.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(applications) ⇒ RequestInterceptor

Returns a new instance of RequestInterceptor.



56
57
58
59
# File 'lib/request_interceptor.rb', line 56

def initialize(applications)
  @applications = applications.map { |pattern, application| ApplicationWrapper.new(pattern, application) }
  @transactions = []
end

Instance Attribute Details

#applicationsObject (readonly)

Returns the value of attribute applications.



53
54
55
# File 'lib/request_interceptor.rb', line 53

def applications
  @applications
end

#transactionsObject (readonly)

Returns the value of attribute transactions.



54
55
56
# File 'lib/request_interceptor.rb', line 54

def transactions
  @transactions
end

Class Method Details

.define(super_class = nil, &application_definition) ⇒ Object



45
46
47
# File 'lib/request_interceptor.rb', line 45

def self.define(super_class = nil, &application_definition)
  Class.new(super_class || template, &application_definition)
end

.run(applications, &simulation) ⇒ Object



49
50
51
# File 'lib/request_interceptor.rb', line 49

def self.run(applications, &simulation)
  new(applications).run(&simulation)
end

.templateObject



41
42
43
# File 'lib/request_interceptor.rb', line 41

def self.template
  @template || Application
end

.template=(template) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/request_interceptor.rb', line 31

def self.template=(template)
  @template =
    case template
    when Proc
      Class.new(Application, &template)
    else
      template
    end
end

Instance Method Details

#run(&simulation) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# 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