Sha256: 82c0a467b8186fdfd88b9df5253bfe6bff0a936ab7f7f9e9630f840af786dc04
Contents?: true
Size: 1.95 KB
Versions: 1
Compression:
Stored size: 1.95 KB
Contents
# frozen_string_literal: true require 'roda/endpoints/endpoint' require 'roda/endpoints/endpoint/data' require 'roda/endpoints/endpoint/caching' require 'inflecto' require 'rom/sql' class Roda module Endpoints class Endpoint # HTTP endpoint representing a collection of items of the same type. class Collection < Endpoint include Data prepend Caching self.attributes += %i(item) self.defaults = defaults.merge( type: :collection, last_modified: :last_modified ) # @return [{Symbol=>Object}] attr_reader :item # @return [Time] def last_modified @last_modified ? repository.public_send(@last_modified) : super end def child(**params) with( type: Item, name: resource_name, **params ) end # @return [Symbol] def resource_name Inflecto.singularize(name).to_sym end route do |r, endpoint| # r.collection :articles do |articles| r.last_modified endpoint.last_modified if endpoint.last_modified endpoint.verbs.each do |verb| # @route #{verb} /:name r.public_send(verb, transaction: verb) r.child **endpoint.item if endpoint.item # child by: :id end end verb :get do |params| Right(repository.list(**params)) end verb :post do |params| params = params[resource_name] || {} Right(repository.create(**params)) end # @route GET /{collection.name} transaction :get do |endpoint| step :retrieve, with: endpoint.operation_for(:get) end # @route POST /{collection.name} transaction :post do |endpoint| step :validate, with: endpoint.validation_for(:post) step :persist, with: endpoint.operation_for(:post) end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
roda-endpoints-0.1.0 | lib/roda/endpoints/endpoint/collection.rb |