Class: CommandBot::CommandIdentifier

Inherits:
Object
  • Object
show all
Defined in:
lib/command_bot/command_identifier.rb

Overview

Stores command call information.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|bot, text, data| ... } ⇒ CommandIdentifier

Create new identifier.

Yield Parameters:

  • bot (Bot)
  • text (String)
  • data (Hash)

Yield Returns:



11
12
13
# File 'lib/command_bot/command_identifier.rb', line 11

def initialize(&identifier)
  @identifier = identifier
end

Class Method Details

._get_options(words, prefix, separator) ⇒ Object

Utility method to get options hash from words array (it will be modified).

Parameters:

  • words (Array<String>)
  • prefix (String)

    prefix of option key.

  • separator (String)

    separator between option and value.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/command_bot/command_identifier.rb', line 70

def self._get_options(words, prefix, separator)
  options = {}
  while words.first&.start_with?(prefix)
    str = words.shift
    str.downcase!
    str.delete_prefix! prefix

    # Need to check for splitter in key
    key, *, value = str.partition(separator)

    # Following line allows writing '!command --key = val' with words=['--key', '=', 'val']
    *, value = words.shift(2) if value.empty? && words.first == separator

    options[key] = value || ''
  end
  options
end

.default(prefix: '', disallow_whitespace: false, o_prefix: '--', o_separator: '=') ⇒ CommandIdentifier

Parameters:

  • prefix (String) (defaults to: '')

    command prefix.

  • disallow_whitespace (Boolean) (defaults to: false)

    whether to disallow whitespace between prefix and command name.

  • o_prefix (String) (defaults to: '--')

    prefix of option key.

  • o_separator (String) (defaults to: '=')

    separator between option and value.

Returns:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/command_bot/command_identifier.rb', line 29

def self.default(prefix: '', disallow_whitespace: false, o_prefix: '--', o_separator: '=') # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  new do |bot, text_original, data|
    command_call = CommandCall.new(bot, text_original, data)
    text = text_original.dup

    # Not a command if got prefix and text do not start with it
    next nil unless prefix.empty? || text.delete_prefix!(prefix)
    # Check for whitespace
    next nil if disallow_whitespace && text.lstrip!

    words = text.split
    next nil if words.empty? # Just a prefix

    command_name = words.shift
    command_name&.downcase! # Usually commands should not depend on case, so downcasing

    options = _get_options(words, o_prefix, o_separator)

    command_call.command = bot.find_command(command_name)
    command_call.arguments = words # Everything left is considered to be arguments
    command_call.options = options
    command_call
  end
end

.primitiveCommandIdentifier

Returns primitive optionless identifier.

Returns:



55
56
57
58
59
60
61
62
63
64
# File 'lib/command_bot/command_identifier.rb', line 55

def self.primitive
  new do |bot, text, data|
    command_call = CommandCall.new(bot, text, data)
    words = text.split
    command_call.command = bot.find_command(words.shift || '')
    command_call.arguments = words
    command_call.options = {}
    command_call
  end
end

Instance Method Details

#call(bot, text, data) ⇒ CommandCall?

Call identifier process.

Parameters:

  • text (String)
  • data (Hash)

Returns:



19
20
21
# File 'lib/command_bot/command_identifier.rb', line 19

def call(bot, text, data)
  @identifier.call(bot, text, data)
end