Class: Blazer::Ai::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/blazer/ai/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



6
7
8
# File 'lib/blazer/ai/middleware.rb', line 6

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/blazer/ai/middleware.rb', line 10

def call(env)
  # Handle AI generation endpoint
  if ai_generate_request?(env)
    return handle_ai_generate(env)
  end

  status, headers, response = @app.call(env)

  # Only inject into HTML responses for Blazer query pages
  if inject_button?(env, headers)
    body = extract_body(response)
    if body.include?("id=\"editor\"") || body.include?("id=\"editor-container\"")
      body = inject_ai_script(body)
      headers["Content-Length"] = body.bytesize.to_s
      response = [body]
    end
  end

  [status, headers, response]
end