Sha256: 19ef467cc0bda58158d9a3d882741bd45145abb506d1c0c984a52cc5ba2378ec

Contents?: true

Size: 1.76 KB

Versions: 5

Compression:

Stored size: 1.76 KB

Contents

# frozen_string_literal: true

module Snowpack
  module Web
    class EndpointResolver
      class NotCallableEndpointError < StandardError
        def initialize(endpoint)
          super("#{endpoint.inspect} isn't compatible with Rack. Please make sure it implements #call.")
        end
      end

      attr_reader :application
      attr_reader :container
      attr_reader :base_namespace

      def initialize(application:, container: application, namespace:)
        @application = application
        @container = container
        @base_namespace = namespace
      end

      def sliced(name)
        raise "Slices can only be mounted from top-level application" unless application.respond_to?(:slices)

        slice = application.slices[name]
        return unless slice

        self.class.new(
          application: application,
          container: slice,
          namespace: base_namespace,
        )
      end

      def call(name, namespace = nil, configuration = nil)
        endpoint =
          case name
          when String
            resolve_string_identifier(name, namespace, configuration)
          when Class
            name.respond_to?(:call) ? name : name.new
          else
            name
          end

        unless endpoint.respond_to?(:call)
          raise NotCallableEndpointError.new(endpoint)
        end

        endpoint
      end

      private

      def resolve_string_identifier(name, namespace, configuration)
        identifier = [base_namespace, namespace, name].compact.join(".")

        container[identifier].yield_self { |endpoint|
          if configuration && endpoint.class < Hanami::Action
            endpoint.with(configuration: configuration)
          else
            endpoint
          end
        }
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
snowpack-1.0.0.alpha7 lib/snowpack/web/endpoint_resolver.rb
snowpack-1.0.0.alpha6 lib/snowpack/web/endpoint_resolver.rb
snowpack-1.0.0.alpha5 lib/snowpack/web/endpoint_resolver.rb
snowpack-1.0.0.alpha4 lib/snowpack/web/endpoint_resolver.rb
snowpack-1.0.0.alpha3 lib/snowpack/web/endpoint_resolver.rb