Sha256: 85c8a51345a0c8fbab8af9830f0cab60b2037547039ee62377f6a9af608209e7

Contents?: true

Size: 1.74 KB

Versions: 1

Compression:

Stored size: 1.74 KB

Contents

module Cogy
  # {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 (https://github.com/skroutz/cogy-bundle) on behalf of the user.
  # You can think of it as the equivalent of the ActionPack's `Request` class.
  class Context
    # @return [Array] The Cog command arguments as passed by the user who
    #   invoked the command.
    #
    # @see https://cog-book.operable.io/#_arguments
    attr_reader :args

    # @return [Hash] The Cog command options as provided by the user who
    #   invoked the command
    #
    # @see https://cog-book.operable.io/#_options
    attr_reader :opts

    # @return [String] The chat handle of the user who invoked the command
    #
    # @see https://cog-book.operable.io/#_general_metadata
    attr_reader :handle

    # @return [Hash] The Cogy environment (ie. all environment variables in
    # the Relay executable that start with 'COGY_')
    #
    # @see https://github.com/skroutz/cogy-bundle/blob/master/commands/cogy
    attr_reader :env

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

    # Executes a {Command} in the context of {self}.
    #
    # @param [Command] cmd the {Command} to be executed
    #
    # @return [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.
    def run!(cmd)
      instance_eval(&cmd.handler)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cogy-0.1.1 lib/cogy/context.rb