Class: PromptScrub::Middleware

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/promptscrub/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, config: PromptScrub.configuration) ⇒ Middleware

Returns a new instance of Middleware.



7
8
9
10
# File 'lib/promptscrub/middleware.rb', line 7

def initialize(app, config: PromptScrub.configuration)
  super(app)
  @config = config
end

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/promptscrub/middleware.rb', line 12

def call(env)
  vault      = Vault.new
  redactor   = Redactor.new(vault, @config.detectors)
  rehydrator = Rehydrator.new(vault)

  env[:body] = scrub_body(env[:body], redactor) if @config.scrub_request

  @app.call(env).on_complete do |response_env|
    if @config.scrub_response && !vault.empty? && response_env[:body].is_a?(String)
      response_env[:body] = rehydrator.rehydrate(response_env[:body])
    end
  end
end