Sha256: b5ce670752acadb4743efc7b260a0be83b658149450ba9c3fede5b10f62a822b

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

module Scorpio
  module OpenAPI
    # OperationsScope acts as an Enumerable of the Operations for an openapi_document,
    # and offers subscripting by operationId.
    class OperationsScope
      include JSI::Memoize

      # @param openapi_document [Scorpio::OpenAPI::Document]
      def initialize(openapi_document)
        @openapi_document = openapi_document
      end
      attr_reader :openapi_document

      # @yield [Scorpio::OpenAPI::Operation]
      def each
        openapi_document.paths.each do |path, path_item|
          path_item.each do |http_method, operation|
            if operation.is_a?(Scorpio::OpenAPI::Operation)
              yield operation
            end
          end
        end
      end
      include Enumerable

      # @param operationId
      # @return [Scorpio::OpenAPI::Operation] the operation with the given operationId
      # @raise [::KeyError] if the given operationId does not exist
      def [](operationId)
        jsi_memoize(:[], operationId) do |operationId_|
          detect { |operation| operation.operationId == operationId_ }.tap do |op|
            unless op
              raise(::KeyError, "operationId not found: #{operationId_.inspect}")
            end
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
scorpio-0.4.6 lib/scorpio/openapi/operations_scope.rb