Class: EndPointBlank::Commands::VersionFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/end_point_blank/commands/version_finder.rb

Overview

Finds the API version from an incoming request by checking multiple sources:

  • Custom version finder configured in Configuration.instance.version_finder
  • Accept header (e.g., application/vnd.api.v1+json)
  • X-Api-Version header (e.g., v1)
  • Content-Type header (e.g., application/vnd.api.v1+json)
  • Query parameter 'version' (e.g., ?version=v1)
  • URL path segment (e.g., /v1/resource)
  • Controller versioning configuration (via Versioned concern) Returns the version number as a string (e.g., "1") or nil if no version is found.

Instance Method Summary collapse

Instance Method Details

#find(request) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/end_point_blank/commands/version_finder.rb', line 16

def find(request)
  return Configuration.instance.version_finder.call(request) if Configuration.instance.version_finder

  return version if respond_to?(:version)

  # Logic to determine version from request
  # This could involve checking headers, query parameters, etc.
  headers["Accept"]&.match(%r{application/vnd\.\w+\.v(\d+)}) do |m|
    return m[1]
  end

  headers["X-Api-Version"]&.match(/v(\d+)/) do |m|
    return m[1]
  end

  headers["Content-Type"]&.match(%r{application/vnd\.\w+\.v(\d+)}) do |m|
    return m[1]
  end

  request.params["version"]&.match(/v(\d+)/) do |m|
    return m[1]
  end

  request.path.match(%r{/v(\d+)/}) do |m|
    return m[1]
  end

  # Try to find version from controller versioning configuration
  version_from_controller(request)
end

#headersObject



13
14
15
# File 'lib/end_point_blank/commands/version_finder.rb', line 13

def headers
  @headers ||= ::EndPointBlank::Rack::Headers.extract
end