module Roda::RodaPlugins::Endpoints::RequestMethods

`Roda::RodaRequest` instant extensions.

Public Instance Methods

collection(name, item: { on: :id }, type: Roda::Endpoints::Endpoint::Collection, on: name.to_s, parent: root_endpoint, **kwargs) { |endpoint| ... } click to toggle source

Implements collection endpoint using given args

@param name [Symbol] @param item [{Symbol=>Object}] @param kwargs [{Symbol=>Object}] @param type [Class(Roda::Endpoints::Endpoint::Collection)] @param on [String, Symbol] @param (see Endpoint::Collection#initialize) @see Endpoint::Collection.defaults @yieldparam endpoint [Collection] @yieldreturn [#to_json] @return [Endpoint]

@example

r.collection :articles, item: { only: %i(get delete) }

# is equivalent to

r.on 'articles', item: { only: %i(get delete) } do
  articles = Endpoint.new(
    name: :articles,
    container: container,
  )

  r.last_modified articles.last_modified

  r.get do
    articles.call(:get, r.params)
  end

  r.post do
    articles.call(:post, r.params)
  end

  r.child :id, only: %i(get delete)
end
# File lib/roda/plugins/endpoints.rb, line 96
def collection(name,
               item: { on: :id },
               type: Roda::Endpoints::Endpoint::Collection,
               on: name.to_s,
               parent: root_endpoint,
               **kwargs)
  endpoint name: name,
           item: item,
           type: type,
           on: on,
           parent: parent,
           **kwargs do |endpoint|
    yield endpoint if block_given?
  end
end
item(on: :id, type: Roda::Endpoints::Endpoint::Item, **kwargs) { |endpoint| ... } click to toggle source

@param on [Symbol] @param kwargs [Hash]

@example

r.collection :articles do |articles|
  r.item :id
end

# is equivalent to

r.collection :articles do |articles|
  r.on :id do |id|

    def article
      @article ||= articles.repository.fetch(id)
    end

    r.get do
      article
    end
  end
end
# File lib/roda/plugins/endpoints.rb, line 136
def item(on: :id,
         type: Roda::Endpoints::Endpoint::Item,
         **kwargs)
  unless current_endpoint.is_a?(Roda::Endpoints::Endpoint::Collection)
    raise ArgumentError,
          "#{self.class}#item called not within a collection endpoint"
  end
  # @route /{collection.name}/{id}
  endpoint(
    on: on,
    name: current_endpoint.item_name,
    type: type,
    **kwargs
  ) do |endpoint|
    yield endpoint if block_given?
  end
end
singleton(name, *args, entity: args.first, type: Roda::Endpoints::Endpoint::Singleton, **kwargs) { |endpoint| ... } click to toggle source

@overload singleton(name, entity, type:, **kwargs) @overload singleton(name:, entity:, type:, **kwargs)

# File lib/roda/plugins/endpoints.rb, line 156
def singleton(name, *args,
              entity: args.first,
              type: Roda::Endpoints::Endpoint::Singleton,
              **kwargs)
  endpoint(
    name: name,
    entity: entity,
    type: type,
    on: name.to_s,
    **kwargs
  ) do |endpoint|
    yield endpoint if block_given?
  end
end

Private Instance Methods

current_endpoint() click to toggle source

@return [Endpoint]

# File lib/roda/plugins/endpoints.rb, line 225
def current_endpoint
  endpoints.last
end
endpoint(name:, type:, container: roda_class.opts[:endpoints][:container], parent: current_endpoint, on: name, **kwargs) { |endpoint| ... } click to toggle source

@param [Class(Roda::Endpoints::Endpoint::Singleton)] type @param [Dry::Container::Mixin, register, resolve, merge] container @param [Roda::Endpoints::Endpoint] parent @param [Hash] kwargs

# File lib/roda/plugins/endpoints.rb, line 177
def endpoint(name:,
             type:,
             container: roda_class.opts[:endpoints][:container],
             parent: current_endpoint,
             on: name,
             **kwargs)
  parent ||= root_endpoint
  on on do |*captures|
    with_current_endpoint parent.child(
      name: name,
      type: type,
      container: container,
      parent: parent,
      on: on,
      captures: captures,
      **kwargs
    ) do |endpoint|
      yield endpoint if block_given?
      instance_exec(self, endpoint, &endpoint.route)
    end
  end
end
endpoints() click to toggle source

@return [<Endpoint>]

# File lib/roda/plugins/endpoints.rb, line 230
def endpoints
  @endpoints ||= [root_endpoint]
end
match_transaction(verb) click to toggle source

@param [Symbol] verb

# File lib/roda/plugins/endpoints.rb, line 201
def match_transaction(verb)
  resolve current_endpoint.transactions.key_for(verb) do |transaction|
    transaction.call(params) do |m|
      statuses = current_endpoint.class.statuses[verb]

      m.success do |result|
        response.status = statuses[:success]
        result
      end

      m.failure do |result|
        if result.is_a?(Array) && result.size == 2
          response.status, value = result
          value
        else
          response.status = statuses[:failure]
          result
        end
      end
    end
  end
end
root_endpoint() click to toggle source
# File lib/roda/plugins/endpoints.rb, line 234
def root_endpoint
  @root_endpoint ||= Roda::Endpoints::Endpoint.new(
    name: :root,
    ns: nil,
    container: roda_class.opts[:endpoints][:container]
  )
end
with_current_endpoint(endpoint) { |endpoint| ... } click to toggle source
# File lib/roda/plugins/endpoints.rb, line 242
def with_current_endpoint(endpoint)
  endpoints.push endpoint
  yield endpoint
  endpoints.pop
end