Sha256: 94b2c7426a5904bcbd78ab41f828fb8984fb2ee3c94caf93c29e68f7b4b9a862

Contents?: true

Size: 1.75 KB

Versions: 8

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: true

class Roda
  module Endpoints
    # Generic HTTP endpoint abstraction.
    class Endpoint
      # Accessing data inside of endpoint.
      module Verbs
        # @param attributes [{Symbol=>Object}]
        def initialize(**attributes)
          @verbs = verbs_to_implement(**attributes)
          super(**attributes)
        end

        # @param only [<Symbol>]
        # @param except [<Symbol>]
        def verbs_to_implement(only: implemented_verbs, except: [], **_kwargs)
          only = Array(only).flatten
          except = Array(except).flatten
          if ((unknown_only = only - implemented_verbs) +
              (unknown_except = except - implemented_verbs)).any?
            params = { only: unknown_only, except: unknown_except }
            raise ArgumentError, "unknown verbs in params: #{params}"
          end
          only - except
        end

        # @return [<Symbol>]
        def implemented_verbs
          self.class.verbs.to_a
        end

        # @return [<Symbol>]
        attr_reader :verbs

        # @param [#to_s] verb
        # @param [Hash] kwargs
        # @param [Proc] block
        def verb(verb, **kwargs, &block)
          key = "operations.#{ns}.#{verb}"
          block ||= container[key]
          singleton_class.verb(verb, **kwargs, &block)
        end

        def prepare_verbs!
          return if @verbs_prepared
          verbs.each do |verb|
            key = "operations.#{ns}.#{verb}"
            next if container.key?(key)
            endpoint = self
            operation = method(verb)
            container.register key do |*args|
              endpoint.instance_exec(*args, &operation)
            end
          end
          @verbs_prepared = true
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
roda-endpoints-0.3.6 lib/roda/endpoints/endpoint/verbs.rb
roda-endpoints-0.3.5 lib/roda/endpoints/endpoint/verbs.rb
roda-endpoints-0.3.4 lib/roda/endpoints/endpoint/verbs.rb
roda-endpoints-0.3.3 lib/roda/endpoints/endpoint/verbs.rb
roda-endpoints-0.3.2 lib/roda/endpoints/endpoint/verbs.rb
roda-endpoints-0.3.1 lib/roda/endpoints/endpoint/verbs.rb
roda-endpoints-0.3.0 lib/roda/endpoints/endpoint/verbs.rb
roda-endpoints-0.2.0 lib/roda/endpoints/endpoint/verbs.rb