Class: Stitches::CallingServiceMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/stitches/calling_service_middleware.rb

Overview

Rack middleware that populates the api_client env var from the X-StitchFix-Calling-Service header when no other auth middleware has already set it. This allows existing code that reads api_client.name to work transparently after API keys are disabled.

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ CallingServiceMiddleware

Returns a new instance of CallingServiceMiddleware.



9
10
11
# File 'lib/stitches/calling_service_middleware.rb', line 9

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



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

def call(env)
  client_key = Stitches.configuration.env_var_to_hold_api_client

  unless env[client_key]
    header_name = Stitches.configuration.calling_service_header
    header_name = CallingServiceName::DEFAULT_CALLING_SERVICE_HEADER unless header_name.present?
    rack_key = "HTTP_#{header_name.upcase.tr('-', '_')}"

    if (name = env[rack_key]).present?
      env[client_key] = CallingServiceClient.new(name)
    end
  end

  @app.call(env)
end