Sha256: a7b03c8f7c53f55988ff9d45d19da5ba9d9887c2914935d8274ff51fc94830e7

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 KB

Contents

require_dependency "cogy/application_controller"

module Cogy
  class CogyController < ApplicationController
    # GET <mount_path>/cmd/:cmd/:user
    #
    # The command endpoint is the one that the cogy executable (see
    # https://github.com/skroutz/cogy-bundle hits. It executes the requested
    # {Command} and responds back the result, which is then printed to the user
    # by the cogy executable.
    #
    # See https://github.com/skroutz/cogy-bundle.
    def command
      cmd = params[:cmd]
      args = request.query_parameters.select { |k,_| k =~ /\Acog_argv_/ }.values
      opts = request.query_parameters.select { |k,_| k =~ /\Acog_opt_/ }
        .transform_keys { |k| k.sub("cog_opt_", "") }
      cogy_env = request.query_parameters.select { |k,_| k =~ /\Acogy_/ }
      user = params[:user]

      begin
        if (command = Cogy.commands[cmd])
          context = Context.new(args, opts, user, cogy_env)
          render text: context.run!(command)
        else
          render status: 404, text: "The command '#{cmd}' does not exist."
        end
      rescue => e
        @user = user
        @cmd = cmd
        @exception = e
        respond_to do |format|
          format.any do
            render "/cogy/error.text.erb", content_type: "text/plain", status: 500
          end
        end
      end
    end

    # GET <mount_path>/inventory
    #
    # The inventory endpoint, is essentially the bundle config in YAML format,
    # which is installable by Cog. It is typically installed by the
    # `cogy:install` command (see https://github.com/skroutz/cogy-bundle).
    def inventory
      render text: Cogy.bundle_config.to_yaml, content_type: "application/x-yaml"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cogy-0.1.0 app/controllers/cogy/cogy_controller.rb