Class: Slack::RPC::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/slack/rpc/command.rb

Overview

## Slack::RPC::Command A class that represents Slack-RPC style command

Instance Method Summary collapse

Constructor Details

#initialize(connection, command, sub_commands) ⇒ Command

### Slack::RPC::Commnad.new(conn, command, sub_commands) Creates a new instance of Slack::RPC::Command class



12
13
14
15
16
# File 'lib/slack/rpc/command.rb', line 12

def initialize(connection, command, sub_commands)
  @connection   = connection
  @command      = command
  @sub_commands = sub_commands
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

### Slack::RPC::Command.method_missing(name, *args, &block) Invoked by Ruby when obj is sent a message it cannot handle.



35
36
37
38
39
40
41
42
# File 'lib/slack/rpc/command.rb', line 35

def method_missing(name, *args, &block)
  if @sub_commands.include?(name.to_s) then
    params = (args.length == 1 && args[0].class == Hash) ? args[0] : {}
    call(name.to_s, params, &block)
  else
    super
  end
end

Instance Method Details

#call(sub_command, params, &block) ⇒ Object

### Slack::RPC::Command.invoke_command Invokes Slack RPC-Style command



21
22
23
# File 'lib/slack/rpc/command.rb', line 21

def call(sub_command, params, &block)
  @connection.call(@command, sub_command, params, &block)
end

#respond_to?(name) ⇒ Boolean

### Slack::RPC::Command.respond_to?(name) Returns true if obj responds to the given method.



28
29
30
# File 'lib/slack/rpc/command.rb', line 28

def respond_to?(name)
  @sub_commands.include?(name) || super
end