Class: RailsContractSync::Runtime::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_contract_sync/runtime/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, store:, route_resolver:, enabled: true) ⇒ Middleware

Returns a new instance of Middleware.



6
7
8
9
10
11
# File 'lib/rails_contract_sync/runtime/middleware.rb', line 6

def initialize(app, store:, route_resolver:, enabled: true)
  @app = app
  @store = store
  @route_resolver = route_resolver
  @enabled = enabled
end

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rails_contract_sync/runtime/middleware.rb', line 13

def call(env)
  status, headers, response = @app.call(env)
  return [status, headers, response] unless enabled?

  content_type = headers["Content-Type"] || headers["content-type"]
  return [status, headers, response] unless content_type&.include?("application/json")

  # Buffer the body so both the recorder and the downstream server can read it.
  parts = []
  response.each { |part| parts << part }
  response.close if response.respond_to?(:close)

  record(env, status, headers, content_type, parts)
  [status, headers, parts]
end