Sha256: bf6eb6f78dac97f7aa668c55360110fbb6a8b19ab4b6549208a17accf35afac4

Contents?: true

Size: 1.33 KB

Versions: 2

Compression:

Stored size: 1.33 KB

Contents

module Endpoints
  class <%= plural_class_name %> < Base
    namespace "<%= url_path %>" do
      before do
        content_type :json, charset: 'utf-8'
      end

      get do
        MultiJson.encode <%= singular_class_name %>.all.map { |x| serialize(x) }
      end

      post do
        # warning: not safe
        <%= field_name %> = <%= singular_class_name %>.new(body_params)
        <%= field_name %>.save
        status 201
        MultiJson.encode serialize(<%= field_name %>)
      end

      get "/:id" do |id|
        <%= field_name %> = <%= singular_class_name %>.first(uuid: id) || halt(404)
        MultiJson.encode serialize(<%= field_name %>)
      end

      patch "/:id" do |id|
        <%= field_name %> = <%= singular_class_name %>.first(uuid: id) || halt(404)
        # warning: not safe
        #<%= field_name %>.update(body_params)
        MultiJson.encode serialize(<%= field_name %>)
      end

      delete "/:id" do |id|
        <%= field_name %> = <%= singular_class_name %>.first(uuid: id) || halt(404)
        <%= field_name %>.destroy
        MultiJson.encode serialize(<%= field_name %>)
      end

      private

      def serialize(data)
        {
          created_at: data.created_at.try(:iso8601),
          id:         data.uuid,
          updated_at: data.updated_at.try(:iso8601),
        }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pliny-0.1.0 lib/pliny/templates/endpoint_scaffold.erb
pliny-0.0.4 lib/pliny/templates/endpoint_scaffold.erb