Sha256: 5d5d42d444c6ff71e25a991755937e557489e99b5649950685e29fd574466d98

Contents?: true

Size: 1.73 KB

Versions: 1

Compression:

Stored size: 1.73 KB

Contents

# frozen_string_literal: true

# FIXME: This is for the NotCallableEndpointError. It would be good if this
# could be require-able without having to bring in all the routing files
require "hanami/routing"

module Snowpack
  module Web
    class EndpointResolver
      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 Hanami::Routing::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

1 entries across 1 versions & 1 rubygems

Version Path
snowpack-1.0.0.alpha2 lib/snowpack/web/endpoint_resolver.rb