Class: Cogy::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/cogy/context.rb,
lib/cogy/invocation.rb

Overview

Context represents a particular invocation request of a Command performed by a user. It holds state like the command arguments, options etc. In other words, it provides the context in which a Command should be invoked.

A Context essentially is an HTTP request performed by the ‘cogy:cogy` command (github.com/skroutz/cogy-bundle) on behalf of the user. You can think of it as the equivalent of the ActionPack’s ‘Request` class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, opts, user, env) ⇒ Context

Returns a new instance of Context.



34
35
36
37
38
39
# File 'lib/cogy/context.rb', line 34

def initialize(args, opts, handle, env)
  @args = args
  @opts = opts
  @handle = handle
  @env = env
end

Instance Attribute Details

#argsObject (readonly)

The Cog command arguments as provided by the user who invoked the command.

See cog-book.operable.io/#_arguments



15
16
17
# File 'lib/cogy/context.rb', line 15

def args
  @args
end

#envObject (readonly)

The Cogy environment (ie. all environment variables in the Relay executable that start with ‘COGY_’)



32
33
34
# File 'lib/cogy/context.rb', line 32

def env
  @env
end

#handleString (readonly)

Returns The chat handle of the user who invoked the command.

Returns:

  • (String)

    The chat handle of the user who invoked the command

See Also:



26
27
28
# File 'lib/cogy/context.rb', line 26

def handle
  @handle
end

#optsObject (readonly)

The Cog command options as provided by the user who invoked the command

See cog-book.operable.io/#_options



21
22
23
# File 'lib/cogy/context.rb', line 21

def opts
  @opts
end

#userObject (readonly)

The chat handle of the user who invoked the command

See cog-book.operable.io/#_general_metadata



24
25
26
# File 'lib/cogy/invocation.rb', line 24

def user
  @user
end

Instance Method Details

#run!(cmd) ⇒ String

Executes a Cogy::Command in the context of self.

Parameters:

Returns:

  • (String)

    the result of the command. This is what will get printed back to the user that invoked the command and is effectively the return value of the command body.



48
49
50
# File 'lib/cogy/context.rb', line 48

def run!(cmd)
  instance_eval(&cmd.handler)
end