Class: Legion::API::Middleware::ApiVersion

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/api/middleware/api_version.rb

Constant Summary collapse

SKIP_PATHS =
%w[/api/health /api/ready /api/openapi.json /metrics].freeze

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ ApiVersion

Returns a new instance of ApiVersion.



11
12
13
# File 'lib/legion/api/middleware/api_version.rb', line 11

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/legion/api/middleware/api_version.rb', line 15

def call(env)
  path = env['PATH_INFO']

  if path.start_with?('/api/v1/')
    env['PATH_INFO'] = path.sub('/api/v1/', '/api/')
    env['HTTP_X_API_VERSION'] = '1'
    @app.call(env)
  elsif path.start_with?('/api/') && !skip_path?(path)
    status, headers, body = @app.call(env)
    headers['Deprecation'] = 'true'
    headers['Sunset'] = (Time.now + (180 * 86_400)).httpdate
    successor = path.sub('/api/', '/api/v1/')
    headers['Link'] = "<#{successor}>; rel=\"successor-version\""
    [status, headers, body]
  else
    @app.call(env)
  end
end