Class: Cogy::CogyController

Inherits:
ApplicationController show all
Defined in:
app/controllers/cogy/cogy_controller.rb

Overview

This is the entry point to the host application.

All Cogy-command invocations of the users end up being served by this controller (#command).

Instance Method Summary collapse

Instance Method Details

#commandObject

POST /<mount_path>/cmd/:cmd

Executes the requested Cogy::Command and returns the result.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/controllers/cogy/cogy_controller.rb', line 12

def command
  cmd = params[:cmd]
  args = params.select { |k, _| k.start_with?("COG_ARGV_") }.to_unsafe_h
               .sort_by { |k, _| k.match(/\d+\z/)[0] }.to_h.values
  opts = params.select { |k, _| k.start_with?("COG_OPT_") }
               .transform_keys { |k| k.sub("COG_OPT_", "").downcase }
  user = params["COG_CHAT_HANDLE"]
  cog_env = request.request_parameters

  begin
    if (command = Cogy.commands[cmd])
      result = Context.new(command, args, opts, user, cog_env).invoke
      if result.is_a?(Hash)
        result = "COG_TEMPLATE: #{command.template || command.name}\n" \
                 "JSON\n" \
                 "#{result.to_json}"
      end
      render plain: result
    else
      render status: 404, plain: "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

#inventoryObject

GET /<mount_path>/inventory

Returns the bundle config in YAML format, which is installable by Cog. It is typically hit by ‘cogy:install` (github.com/skroutz/cogy-bundle).



49
50
51
# File 'app/controllers/cogy/cogy_controller.rb', line 49

def inventory
  render body: Cogy.bundle_config.to_yaml, content_type: "application/x-yaml"
end