Sha256: 30f82b796371a50002b0ffcb9221aa4d61098f66431f5d35bb8176b0eb1335af

Contents?: true

Size: 1.37 KB

Versions: 2

Compression:

Stored size: 1.37 KB

Contents

module VersionCake
  class VersionContextService

    def initialize(config)
      @versioned_resources = config.versioned_resources
      @default_version = config.default_version
      @strategies = config.extraction_strategies
    end

    def create_context_from_request(raw_request)
      return unless resource = find_resource(raw_request.path)

      request = VersionCake::VersionedRequest.new(
                    raw_request,
                    @strategies,
                    @default_version
                )
      request.execute

      result = if request.failed
        :version_invalid
      else
        check_version(resource, request.version)
      end

      VersionCake::VersionContext.new(request.version, resource, result)
    end

    def create_context(uri, version)
      return unless resource = find_resource(uri)

      result = check_version(resource, version)

      VersionCake::VersionContext.new(version, resource, result)
    end

    def create_context_from_context(context, version)
      result = check_version(context.resource, version)

      VersionCake::VersionContext.new(version, context.resource, result)
    end

    private

    def check_version(resource, version)
      VersionCake::VersionChecker.new(version, resource).execute
    end

    def find_resource(uri)
      @versioned_resources.find { |resource| resource.uri.match uri }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
versioncake-4.1.1 lib/versioncake/version_context_service.rb
versioncake-4.1.0 lib/versioncake/version_context_service.rb