Sha256: 2db867c92233632a6d1bc8a0a2854d6731790286cb8fd631b101f9b4c4803e5a

Contents?: true

Size: 1.3 KB

Versions: 5

Compression:

Stored size: 1.3 KB

Contents

module Endpoints
  class <%= plural_class_name %> < Base
    namespace "<%= url_path %>" do
      before do
        content_type :json
      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(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(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

5 entries across 5 versions & 1 rubygems

Version Path
pliny-0.0.3 lib/pliny/templates/endpoint_scaffold.erb
pliny-0.0.1 lib/pliny/templates/endpoint_scaffold.erb
pliny-0.0.1.pre3 lib/pliny/templates/endpoint_scaffold.erb
pliny-0.0.1.pre2 lib/pliny/templates/endpoint_scaffold.erb
pliny-0.0.1.pre lib/pliny/templates/endpoint_scaffold.erb