command_bot 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ce3490ea16754fc8c40d25e6a1c88ca15ecbb2457bb380b466c47797ad8d0af3
4
- data.tar.gz: 7aff185e761c5770f853cb78053dc57b81c9ecc388ad61e93642ee98e8f2121d
3
+ metadata.gz: c0967f34f9d65155da4467d04c8dbcaf5c1772d925a73f6892dab25e257e3440
4
+ data.tar.gz: 543fc95cc45e431bfaaf0a6c0ae93a40b8afa5239b622982c8a9f3ffd2f2e053
5
5
  SHA512:
6
- metadata.gz: b0ec894158f3d357ef9267c40549b5b8869184eda4b4beb7a30817ddd3440744108e0fa80d57a921ce493375dc050c40eed7f7e4916d808d8cdb5a77c2cc95a8
7
- data.tar.gz: 831ea48cfaf594e2375a651798a1a0f775c09bbcbb6854a4a383674e87f2a0296ad75a22c190717f87bd17abfbd4210eb065da94cb839002b133ab35d80043e4
6
+ metadata.gz: 74a2a881ef1ba216a3be8f0bb24b0b3eef659bb81b38258535db3b41af5f7fb034656d6bfbff0c56e616b75d740ee1d9bc9e1ff391bb22230745dc23c205cbc8
7
+ data.tar.gz: '08e77136146597ebe9d774a8d143c07c7ae0e318cd725c918ff9e10ff3a88215ee51c906c6db5f01f81d0b7cec1f0b3fb69f6cad85010fcf487d6f64a1eb5f6f'
@@ -1,49 +1,49 @@
1
- # frozen_string_literal: true
2
-
3
- $LOAD_PATH.unshift File.expand_path('../lib', __dir__)
4
- require 'command_bot'
5
-
6
- PingCommand = CommandBot::Command.new(
7
- name: 'ping',
8
- aliases: ['p'],
9
- data: {
10
- description: 'ping-pong',
11
- description_long: 'Ping-pong.'
12
- }
13
- ) do |_bot, _arguments, _options, *_other|
14
- 'pong'
15
- end
16
-
17
- HelpCommand = CommandBot::Command.new(
18
- name: 'help',
19
- aliases: ['h'],
20
- data: {
21
- description: 'show help message',
22
- description_long: 'Show help message for specified command. Shows list of ' \
23
- 'command if atgument is not specified.',
24
- another_info: 'you can pass any data here'
25
- }
26
- ) do |bot, arguments, _options, *_other|
27
- commands = if arguments.empty?
28
- bot.commands
29
- else
30
- bot.commands.select { |e| arguments.include? e.name }
31
- end
32
-
33
- help_line = if arguments.empty?
34
- proc do |e|
35
- "#{e.name} (also known as #{e.aliases.join(', ')}) " \
36
- "- #{e.data[:description]}"
37
- end
38
- else
39
- proc do |e|
40
- "#{e.name} (also known as #{e.aliases.join(', ')}) " \
41
- "- #{e.data[:description]}\n#{e.data[:description_long]}"
42
- end
43
- end
44
-
45
- <<~HELP
46
- Commands list:
47
- #{commands.map(&help_line).join("\n")}
48
- HELP
49
- end
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.unshift File.expand_path('../lib', __dir__)
4
+ require 'command_bot'
5
+
6
+ PingCommand = CommandBot::Command.new(
7
+ name: 'ping',
8
+ aliases: ['p'],
9
+ data: {
10
+ description: 'ping-pong',
11
+ description_long: 'Ping-pong.'
12
+ }
13
+ ) do |_bot, _arguments, _options, _data|
14
+ 'pong'
15
+ end
16
+
17
+ HelpCommand = CommandBot::Command.new(
18
+ name: 'help',
19
+ aliases: ['h'],
20
+ data: {
21
+ description: 'show help message',
22
+ description_long: 'Show help message for specified command. Shows list of ' \
23
+ 'command if atgument is not specified.',
24
+ another_info: 'you can pass any data here'
25
+ }
26
+ ) do |bot, arguments, _options, _data|
27
+ commands = if arguments.empty?
28
+ bot.commands
29
+ else
30
+ bot.commands.select { |e| arguments.include? e.name }
31
+ end
32
+
33
+ help_line = if arguments.empty?
34
+ proc do |e|
35
+ "#{e.name} (also known as #{e.aliases.join(', ')}) " \
36
+ "- #{e.data[:description]}"
37
+ end
38
+ else
39
+ proc do |e|
40
+ "#{e.name} (also known as #{e.aliases.join(', ')}) " \
41
+ "- #{e.data[:description]}\n#{e.data[:description_long]}"
42
+ end
43
+ end
44
+
45
+ <<~HELP
46
+ Commands list:
47
+ #{commands.map(&help_line).join("\n")}
48
+ HELP
49
+ end
@@ -1,117 +1,120 @@
1
- # frozen_string_literal: true
2
-
3
- require 'logger'
4
-
5
- module CommandBot
6
- # Main class.
7
- class Bot
8
- # Initialize new bot.
9
- # @paran identifier [CommandIdentifier] defaults to {CommandIdentifier.default}.
10
- # @param logger [Logger]
11
- # @param log_level [Logger::Severity]
12
- # @param command_not_found [Proc] process to execute when command can't be
13
- # identified. Accepts single argument of {CommandCall}. Defaults to returning +nil+.
14
- def initialize(identifier: nil, logger: nil, log_level: Logger::INFO, command_not_found: nil)
15
- @identifier = identifier || CommandIdentifier.default
16
- @command_not_found = command_not_found || proc { |_command_call| nil }
17
-
18
- @commands = []
19
-
20
- log_formatter = proc do |severity, datetime, progname, msg|
21
- "[#{datetime}] #{severity} - #{progname || object_id}:\t #{msg}\n"
22
- end
23
- @logger = logger || Logger.new(STDOUT, formatter: log_formatter)
24
- @logger.level = log_level
25
- end
26
-
27
- # @return [Array<Command>]
28
- attr_reader :commands
29
-
30
- # @param name [String]
31
- # @return [Command, nil]
32
- def find_command(name)
33
- logger.debug "Searching for command `#{name}`"
34
- commands.find { |c| c.name_matches?(name) }
35
- end
36
-
37
- # Add new commands.
38
- # @param commands [Command]
39
- def add_commands(*commands)
40
- logger.debug "Adding #{commands.size} new commands"
41
- all_aliases_array = all_aliases
42
- commands.each { |c| add_command_unless_alias_is_in_array(c, all_aliases_array) }
43
- end
44
-
45
- # Remove commands.
46
- # @param commands [Command, String]
47
- def remove_commands(*commands)
48
- logger.debug "Removing #{commands.size} commands"
49
- command_names = commands.map { |c| c.is_a?(String) ? c : c.name }
50
- @commands.reject! { |ec| ec.name_matches?(*command_names) }
51
- end
52
-
53
- # Handle message.
54
- # @param text [String] message text.
55
- # @param data [Hash] additional data which should be passed to handler procedure.
56
- # @return [void, nil] result depends on handler of command.
57
- def handle(text, data = {})
58
- logger.debug "Handling text: `#{text}` with additional data: #{data}"
59
- command_call = identify_command_call(text, data)
60
- return nil if command_call.nil? # Not a command call, so not handling.
61
-
62
- handle_command_call(command_call)
63
- end
64
-
65
- # Hadnle {CommandCall}.
66
- # @param command_call [CommandCall]
67
- # @return [void, nil] result depends on handler of command.
68
- def handle_command_call(command_call)
69
- logger.debug "Hadnling command call: #{command_call}"
70
- if command_call.command
71
- execute_command_call(command_call)
72
- else
73
- command_not_found(command_call)
74
- end
75
- end
76
-
77
- private
78
-
79
- # @return [Logger]
80
- attr_reader :logger
81
-
82
- # @return [Array<String>]
83
- def all_aliases
84
- re = @commands.map(&:all_aliases)
85
- re.flatten!
86
- re
87
- end
88
-
89
- # @note modifies +all_aliases_array+
90
- def add_command_unless_alias_is_in_array(command, all_aliases_array)
91
- command_aliases = command.all_aliases
92
- intersection = all_aliases_array & command_aliases
93
- if intersection.empty?
94
- @commands << command
95
- all_aliases_array.concat(command_aliases)
96
- else
97
- logger.warn "Command #{command.name} will not be added due to name intersection!"
98
- end
99
- end
100
-
101
- # @return [CommandCall, nil]
102
- def identify_command_call(text, data)
103
- @identifier.call(self, text, data)
104
- end
105
-
106
- # @return [void]
107
- def execute_command_call(command_call)
108
- command_call.execute
109
- # NOTE: it is possible to execute it from here, but IHMO command_call should
110
- # be able to execute itself.
111
- end
112
-
113
- def command_not_found(command_call)
114
- @command_not_found.call(command_call)
115
- end
116
- end
117
- end
1
+ # frozen_string_literal: true
2
+
3
+ require 'logger'
4
+
5
+ module CommandBot
6
+ # Main class.
7
+ class Bot
8
+ # Initialize new bot.
9
+ # @paran identifier [CommandIdentifier] defaults to {CommandIdentifier.default}.
10
+ # @param logger [Logger]
11
+ # @param log_level [Logger::Severity]
12
+ # @param command_not_found [Proc] process to execute when command can't be
13
+ # identified. Accepts single argument of {CommandCall}. Defaults to returning +nil+.
14
+ def initialize(identifier: nil, logger: nil, log_level: Logger::INFO, command_not_found: nil)
15
+ @identifier = identifier || CommandIdentifier.default
16
+ @command_not_found = command_not_found || proc { |_command_call| nil }
17
+
18
+ @commands = []
19
+
20
+ log_formatter = proc do |severity, datetime, progname, msg|
21
+ "[#{datetime}] #{severity} - #{progname || object_id}:\t #{msg}\n"
22
+ end
23
+ @logger = logger || Logger.new(STDOUT, formatter: log_formatter)
24
+ @logger.level = log_level
25
+ end
26
+
27
+ # @return [Array<Command>]
28
+ attr_reader :commands
29
+
30
+ # @return [CommandIdentifier]
31
+ attr_reader :identifier
32
+
33
+ # @param name [String]
34
+ # @return [Command, nil]
35
+ def find_command(name)
36
+ logger.debug "Searching for command `#{name}`"
37
+ commands.find { |c| c.name_matches?(name) }
38
+ end
39
+
40
+ # Add new commands.
41
+ # @param commands [Command]
42
+ def add_commands(*commands)
43
+ logger.debug "Adding #{commands.size} new commands"
44
+ all_aliases_array = all_aliases
45
+ commands.each { |c| add_command_unless_alias_is_in_array(c, all_aliases_array) }
46
+ end
47
+
48
+ # Remove commands.
49
+ # @param commands [Command, String]
50
+ def remove_commands(*commands)
51
+ logger.debug "Removing #{commands.size} commands"
52
+ command_names = commands.map { |c| c.is_a?(String) ? c : c.name }
53
+ @commands.reject! { |ec| ec.name_matches?(*command_names) }
54
+ end
55
+
56
+ # Handle message.
57
+ # @param text [String] message text.
58
+ # @param data [Hash] additional data which should be passed to handler procedure.
59
+ # @return [void, nil] result depends on handler of command.
60
+ def handle(text, data = {})
61
+ logger.debug "Handling text: `#{text}` with additional data: #{data}"
62
+ command_call = identify_command_call(text, data)
63
+ return nil if command_call.nil? # Not a command call, so not handling.
64
+
65
+ handle_command_call(command_call)
66
+ end
67
+
68
+ # Hadnle {CommandCall}.
69
+ # @param command_call [CommandCall]
70
+ # @return [void, nil] result depends on handler of command.
71
+ def handle_command_call(command_call)
72
+ logger.debug "Hadnling command call: #{command_call}"
73
+ if command_call.command
74
+ execute_command_call(command_call)
75
+ else
76
+ command_not_found(command_call)
77
+ end
78
+ end
79
+
80
+ private
81
+
82
+ # @return [Logger]
83
+ attr_reader :logger
84
+
85
+ # @return [Array<String>]
86
+ def all_aliases
87
+ re = @commands.map(&:all_aliases)
88
+ re.flatten!
89
+ re
90
+ end
91
+
92
+ # @note modifies +all_aliases_array+
93
+ def add_command_unless_alias_is_in_array(command, all_aliases_array)
94
+ command_aliases = command.all_aliases
95
+ intersection = all_aliases_array & command_aliases
96
+ if intersection.empty?
97
+ @commands << command
98
+ all_aliases_array.concat(command_aliases)
99
+ else
100
+ logger.warn "Command #{command.name} will not be added due to name intersection!"
101
+ end
102
+ end
103
+
104
+ # @return [CommandCall, nil]
105
+ def identify_command_call(text, data)
106
+ @identifier.call(self, text, data)
107
+ end
108
+
109
+ # @return [void]
110
+ def execute_command_call(command_call)
111
+ command_call.execute
112
+ # NOTE: it is possible to execute it from here, but IHMO command_call should
113
+ # be able to execute itself.
114
+ end
115
+
116
+ def command_not_found(command_call)
117
+ @command_not_found.call(command_call)
118
+ end
119
+ end
120
+ end
@@ -1,6 +1,6 @@
1
- # frozen_string_literal: true
2
-
3
- module CommandBot
4
- # Library version.
5
- VERSION = '1.0.0'
6
- end
1
+ # frozen_string_literal: true
2
+
3
+ module CommandBot
4
+ # Library version.
5
+ VERSION = '1.0.1'
6
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: command_bot
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fizvlad
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-06-14 00:00:00.000000000 Z
11
+ date: 2020-06-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: