class Rack::ServiceApiVersioning::ServiceComponentDescriber

Middleware that, given a “service name” parameter, will read descriptive data from a supplied repository regarding the named service, format that data as a JSON string, assign it to the `COMPONENT_DESCRIPTION` environment variable, and pass the updated environment along to the next link in the Rack call chain (app or more middleware). If there is no data for the named service in the repository, this middleware will halt execution with a 404 (Not Found).

Also bear in mind that both usual parameters (`repository` and `service_name`) are supplied *by the AVIDA,* which should know what those “ought” to be.

Middlware to query and encode data on a named service, making it available to later entries in the middleware chain via an environment variable.

Constants

DEFAULT_ENV_KEYS

Attributes

app[R]
env_keys[R]
repository[R]
service_name[R]

Public Class Methods

new(app, repository:, service_name:, env_keys: DEFAULT_ENV_KEYS) click to toggle source
# File lib/rack/service_api_versioning/service_component_describer.rb, line 26
def initialize(app, repository:, service_name:,
               env_keys: DEFAULT_ENV_KEYS)
  @app = app
  @env_keys = env_keys
  @repository = repository
  @service_name = service_name
  self
end

Public Instance Methods

call(env) click to toggle source
# File lib/rack/service_api_versioning/service_component_describer.rb, line 35
def call(env)
  env = update_env(env)
  verify_datum_set(env) { |app_env| app.call app_env }
end

Private Instance Methods

first_record() click to toggle source
# File lib/rack/service_api_versioning/service_component_describer.rb, line 44
def first_record
  repository.find(name: service_name).first
end
result_key() click to toggle source
# File lib/rack/service_api_versioning/service_component_describer.rb, line 48
def result_key
  env_keys[:result]
end
update_env(env) click to toggle source
# File lib/rack/service_api_versioning/service_component_describer.rb, line 52
def update_env(env)
  datum = first_record
  env[result_key] = JSON.dump(datum) if datum
  env
end
verify_datum_set(env) { |env| ... } click to toggle source
# File lib/rack/service_api_versioning/service_component_describer.rb, line 58
def verify_datum_set(env)
  verify_found(env) || yield(env)
end
verify_found(env) click to toggle source
# File lib/rack/service_api_versioning/service_component_describer.rb, line 62
def verify_found(env)
  return nil if env[result_key]
  ReportServiceNotFound.call(service_name)
end