Module: OpenapiFirst

Defined in:
lib/openapi_first.rb,
lib/openapi_first/test.rb,
lib/openapi_first/errors.rb,
lib/openapi_first/router.rb,
lib/openapi_first/schema.rb,
lib/openapi_first/builder.rb,
lib/openapi_first/failure.rb,
lib/openapi_first/request.rb,
lib/openapi_first/version.rb,
lib/openapi_first/response.rb,
lib/openapi_first/json_refs.rb,
lib/openapi_first/definition.rb,
lib/openapi_first/body_parser.rb,
lib/openapi_first/test/methods.rb,
lib/openapi_first/configuration.rb,
lib/openapi_first/error_response.rb,
lib/openapi_first/request_parser.rb,
lib/openapi_first/response_parser.rb,
lib/openapi_first/request_validator.rb,
lib/openapi_first/validated_request.rb,
lib/openapi_first/response_validator.rb,
lib/openapi_first/validated_response.rb,
lib/openapi_first/router/find_content.rb,
lib/openapi_first/router/find_response.rb,
lib/openapi_first/router/path_template.rb,
lib/openapi_first/error_responses/default.rb,
lib/openapi_first/error_responses/jsonapi.rb,
lib/openapi_first/schema/validation_error.rb,
lib/openapi_first/validators/request_body.rb,
lib/openapi_first/schema/validation_result.rb,
lib/openapi_first/validators/response_body.rb,
lib/openapi_first/validators/response_headers.rb,
lib/openapi_first/validators/request_parameters.rb,
lib/openapi_first/middlewares/request_validation.rb,
lib/openapi_first/middlewares/response_validation.rb

Overview

OpenapiFirst is a toolchain to build HTTP APIS based on OpenAPI API descriptions.

Defined Under Namespace

Modules: ErrorResponse, ErrorResponses, JsonRefs, Middlewares, Test, Validators Classes: Builder, Configuration, Definition, Error, Failure, FileNotFoundError, NotFoundError, ParseError, Request, RequestInvalidError, RequestParser, RequestValidator, Response, ResponseInvalidError, ResponseNotFoundError, ResponseParser, ResponseValidator, Router, Schema, ValidatedRequest, ValidatedResponse

Constant Summary collapse

REQUEST =

Key in rack to find instance of Request

'openapi.request'
FAILURE =
:openapi_first_validation_failure
VERSION =
'2.1.1'
ParsedResponse =
Data.define(:body, :headers)

Class Method Summary collapse

Class Method Details

.configurationConfiguration

Returns:



21
22
23
# File 'lib/openapi_first.rb', line 21

def self.configuration
  @configuration ||= Configuration.new
end

.configure {|Configuration| ... } ⇒ Configuration

Yields:

Returns:



27
28
29
# File 'lib/openapi_first.rb', line 27

def self.configure
  yield configuration
end

.find_error_response(name) ⇒ Class

Returns The error response class.

Parameters:

  • name (Symbol)

Returns:

  • (Class)

    The error response class



43
44
45
46
47
48
49
# File 'lib/openapi_first.rb', line 43

def self.find_error_response(name)
  ERROR_RESPONSES.fetch(name) do
    raise "Unknown error response: #{name}. " /
          'Register your error response class via `OpenapiFirst.register_error_response(name, klass)`. ' /
          "Registered error responses are: #{ERROR_RESPONSES.keys.join(', ')}."
  end
end

.load(filepath, only: nil) ⇒ Definition

Load and dereference an OpenAPI spec file

Returns:

Raises:



53
54
55
56
57
58
# File 'lib/openapi_first.rb', line 53

def self.load(filepath, only: nil, &)
  raise FileNotFoundError, "File not found: #{filepath}" unless File.exist?(filepath)

  resolved = Bundle.resolve(filepath)
  parse(resolved, only:, filepath:, &)
end

.parse(resolved, only: nil, filepath: nil) ⇒ Definition

Parse a dereferenced Hash

Returns:



62
63
64
65
# File 'lib/openapi_first.rb', line 62

def self.parse(resolved, only: nil, filepath: nil, &)
  resolved['paths'].filter!(&->(key, _) { only.call(key) }) if only
  Definition.new(resolved, filepath, &)
end

.register_error_response(name, klass) ⇒ Object

Register an error response class

Parameters:

  • name (Symbol)
  • klass (Class)

    A class that includes / implements OpenapiFirst::ErrorResponse



37
38
39
# File 'lib/openapi_first.rb', line 37

def self.register_error_response(name, klass)
  ERROR_RESPONSES[name.to_sym] = klass
end