pug-bot 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pug'
4
+
5
+ describe Pug::Configuration do
6
+ describe 'initialize' do
7
+ it 'should default to terminal if type is nil' do
8
+ config = Pug::Configuration.new
9
+ expect(config.type).to eq(Pug::Configuration::TERMINAL)
10
+ end
11
+ end
12
+
13
+ describe 'validate' do
14
+ before(:each) { @config = Pug::Configuration.new }
15
+
16
+ it 'should raise an error if type is not valid' do
17
+ @config.type = 100
18
+ expect { @config.validate }.to raise_error(RuntimeError)
19
+ end
20
+
21
+ it 'should raise an error if telegram type and missing token' do
22
+ @config.type = Pug::Configuration::TELEGRAM
23
+ @config.chat_id = '123'
24
+ expect { @config.validate }.to raise_error(RuntimeError)
25
+ end
26
+
27
+ it 'should raise an error if telegram type and missing chat_id' do
28
+ @config.type = Pug::Configuration::TELEGRAM
29
+ @config.token = 'TOKEN'
30
+ expect { @config.validate }.to raise_error(RuntimeError)
31
+ end
32
+
33
+ it 'should not raise an error for a valid telegram config' do
34
+ @config.type = Pug::Configuration::TERMINAL
35
+ @config.chat_id = '123'
36
+ @config.token = 'TOKEN'
37
+ @config.actions = []
38
+ expect { @config.validate }.to_not raise_error
39
+ end
40
+
41
+ it 'should raise an error if actions are nil' do
42
+ @config.type = Pug::Configuration::TERMINAL
43
+ expect { @config.validate }.to raise_error(RuntimeError)
44
+ end
45
+
46
+ it 'should not raise an error for a valid terminal config' do
47
+ @config.type = Pug::Configuration::TERMINAL
48
+ @config.actions = []
49
+ expect { @config.validate }.to_not raise_error
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pug'
4
+ require_relative '../spec_helpers/mock_action'
5
+
6
+ describe Pug::KeywordHandler do
7
+ describe 'keyword?' do
8
+ it 'returns false for non-keyword' do
9
+ handler = Pug::KeywordHandler.new([])
10
+ expect(handler.keyword?('ALEX')).to be false
11
+ end
12
+
13
+ it 'returns true for help keyword' do
14
+ handler = Pug::KeywordHandler.new([])
15
+ expect(handler.keyword?('help')).to be true
16
+ end
17
+
18
+ it 'returns true for list keyword' do
19
+ handler = Pug::KeywordHandler.new([])
20
+ expect(handler.keyword?('list')).to be true
21
+ end
22
+ end
23
+
24
+ describe 'run_command_for_keyword' do
25
+ before(:each) do
26
+ action0 = MockAction.new('Test0', false, '', 'About0')
27
+ action1 = MockAction.new('Test1', false, '')
28
+ action2 = MockAction.new('Test2', false, '', '')
29
+ actions = [action0, action1, action2]
30
+ @handler = Pug::KeywordHandler.new(actions)
31
+ end
32
+
33
+ it 'returns nil if text is not a keyword' do
34
+ expect(@handler.run_command_for_keyword('Alex')).to be nil
35
+ end
36
+
37
+ it 'returns list of keywords if text is help' do
38
+ expected = Pug::Strings.help('list')
39
+ expect(@handler.run_command_for_keyword('help')).to eq(expected)
40
+ end
41
+
42
+ it 'returns list of actions if text is list' do
43
+ expected = "0: Test0 # About0\n1: Test1\n2: Test2"
44
+ expect(@handler.run_command_for_keyword('list')).to eq(expected)
45
+ end
46
+
47
+ it 'returns if there are no actions if text is list' do
48
+ handler = Pug::KeywordHandler.new([])
49
+ expected = Pug::Strings.no_actions
50
+ expect(handler.run_command_for_keyword('list')).to eq(expected)
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pug'
4
+ require_relative '../spec_helpers/mock_action'
5
+
6
+ describe Pug::MessageHandler do
7
+ describe 'when there are no actions' do
8
+ before(:each) do
9
+ @handler = Pug::MessageHandler.default([])
10
+ end
11
+
12
+ it 'should return a message about lack of actions' do
13
+ result = @handler.handle('something')
14
+ expect(result.type).to eq(Pug::Types::Result::ERROR)
15
+ expect(result.error).to eq(Pug::Strings.no_actions)
16
+ end
17
+ end
18
+
19
+ describe 'when there are some actions' do
20
+ before(:each) do
21
+ action = MockAction.new('Cmd', false, 'Yo')
22
+ @handler = Pug::MessageHandler.default([action])
23
+ end
24
+
25
+ it 'should return unknown input result for an empty message' do
26
+ result = @handler.handle('')
27
+ expect(result.type).to eq(Pug::Types::Result::ERROR)
28
+ expect(result.error).to eq(Pug::Strings.unknown_input)
29
+ end
30
+
31
+ it 'should return unknown input result for an invalid action message' do
32
+ result = @handler.handle('-1: An invalid action')
33
+ expect(result.type).to eq(Pug::Types::Result::ERROR)
34
+ expect(result.error).to eq(Pug::Strings.unknown_input)
35
+ end
36
+
37
+ it 'should return unable to start commmand for invalid index' do
38
+ result = @handler.handle('10: Some out of bounds action')
39
+ expect(result.type).to eq(Pug::Types::Result::ERROR)
40
+ expect(result.error).to eq(Pug::Strings.unable_to_start_action(10))
41
+ end
42
+ end
43
+
44
+ describe 'when running a action that does not require input' do
45
+ before(:each) do
46
+ action = MockAction.new('Cmd', false, 'Done!')
47
+ @handler = Pug::MessageHandler.default([action])
48
+ end
49
+
50
+ it 'should return the success output' do
51
+ result = @handler.handle('0')
52
+ expect(result.type).to eq(Pug::Types::Result::SUCCESS)
53
+ expect(result.value.action_name).to eq('Cmd')
54
+ expect(result.value.value).to eq('Done!')
55
+ end
56
+ end
57
+
58
+ describe 'when running a action that requires input' do
59
+ before(:each) do
60
+ action = MockAction.new('Cmd', true, 'Done!')
61
+ @handler = Pug::MessageHandler.default([action])
62
+ end
63
+
64
+ it 'should return info result informing that input is needed' do
65
+ result = @handler.handle('0')
66
+ expect(result.type).to eq(Pug::Types::Result::INFO)
67
+ expect(result.value).to eq(Pug::Strings.enter_inputs('Cmd'))
68
+ end
69
+
70
+ it 'should return a success result when input has been received' do
71
+ @handler.handle('0')
72
+ result = @handler.handle('Some input now')
73
+ expect(result.type).to eq(Pug::Types::Result::SUCCESS)
74
+ expect(result.value.action_name).to eq('Cmd')
75
+ expect(result.value.value).to eq('Done!')
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pug/number_parser'
4
+
5
+ describe Pug::NumberParser do
6
+ describe 'starts_with_numeric_text?' do
7
+ it 'returns false if the text does not start with a number' do
8
+ parser = Pug::NumberParser.new
9
+ expect(parser.starts_with_numeric_text?('alex')).to be false
10
+ end
11
+
12
+ it 'returns true if the text is 0' do
13
+ parser = Pug::NumberParser.new
14
+ expect(parser.starts_with_numeric_text?('0')).to be true
15
+ end
16
+
17
+ it 'returns true if the text is just a number' do
18
+ parser = Pug::NumberParser.new
19
+ expect(parser.starts_with_numeric_text?('10')).to be true
20
+ end
21
+
22
+ it 'returns true if text starts with number' do
23
+ parser = Pug::NumberParser.new
24
+ expect(parser.starts_with_numeric_text?('1. Alex')).to be true
25
+ end
26
+ end
27
+
28
+ describe 'number_from_text' do
29
+ it 'returns nil if the text does not contain a number' do
30
+ parser = Pug::NumberParser.new
31
+ expect(parser.number_from_text('Alex')).to be nil
32
+ end
33
+
34
+ it 'returns nil if the text does not start with a number' do
35
+ parser = Pug::NumberParser.new
36
+ expect(parser.number_from_text('Alex 10')).to be nil
37
+ end
38
+
39
+ it 'returns the number if the text does start with a number' do
40
+ parser = Pug::NumberParser.new
41
+ expect(parser.number_from_text('10. Alex')).to eq(10)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pug'
4
+
5
+ describe Pug::Types::Result do
6
+ describe 'success' do
7
+ it 'should return a success result' do
8
+ success = Pug::Types::Result.success('Hello')
9
+ expect(success.type).to eq(Pug::Types::Result::SUCCESS)
10
+ expect(success.value).to eq('Hello')
11
+ expect(success.error).to be nil
12
+ end
13
+ end
14
+
15
+ describe 'info' do
16
+ it 'should return an info result' do
17
+ info = Pug::Types::Result.info('Info')
18
+ expect(info.type).to eq(Pug::Types::Result::INFO)
19
+ expect(info.value).to eq('Info')
20
+ expect(info.error).to be nil
21
+ end
22
+ end
23
+
24
+ describe 'error' do
25
+ it 'should return an error result' do
26
+ error = Pug::Types::Result.error('Oops')
27
+ expect(error.type).to eq(Pug::Types::Result::ERROR)
28
+ expect(error.value).to be nil
29
+ expect(error.error).to eq('Oops')
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pug'
4
+
5
+ class MockAction < Pug::Interfaces::Action
6
+ attr_accessor :mock_name,
7
+ :mock_requires_input,
8
+ :mock_output,
9
+ :mock_description
10
+
11
+ def initialize(
12
+ name = nil,
13
+ requires_input = nil,
14
+ output = nil,
15
+ description = nil
16
+ )
17
+ @mock_name = name || ''
18
+ @mock_requires_input = requires_input || false
19
+ @mock_output = output || ''
20
+ @mock_description = description || ''
21
+ end
22
+
23
+ # Action Interface
24
+ def name
25
+ @mock_name
26
+ end
27
+
28
+ def description
29
+ @mock_description
30
+ end
31
+
32
+ def requires_input?
33
+ @mock_requires_input
34
+ end
35
+
36
+ def execute(___)
37
+ @mock_output
38
+ end
39
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pug'
4
+
5
+ class MockClient < Pug::Interfaces::Client
6
+ attr_accessor :message_queue, :sent_messages
7
+
8
+ def initialize
9
+ @sent_messages = []
10
+ @message_queue = []
11
+ end
12
+
13
+ # Overrides
14
+ def listen
15
+ queue = @message_queue
16
+ yield queue.shift until queue.empty?
17
+ end
18
+
19
+ def send_message(message)
20
+ @sent_messages.push(message)
21
+ end
22
+
23
+ # Helpers
24
+ def enqueue_message(message)
25
+ @message_queue.push(message)
26
+ end
27
+
28
+ def last_sent_message
29
+ return nil if @sent_messages.empty?
30
+ @sent_messages.pop
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,169 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pug-bot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alex Figueroa
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-06-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: telegram-bot-ruby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.8.6
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.8.6
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: 0.8.6
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 0.8.6
33
+ - !ruby/object:Gem::Dependency
34
+ name: rspec
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: 3.7.0
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 3.7.0
43
+ type: :development
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: 3.7.0
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 3.7.0
53
+ - !ruby/object:Gem::Dependency
54
+ name: rubocop
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: 0.53.0
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 0.53.0
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: 0.53.0
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 0.53.0
73
+ - !ruby/object:Gem::Dependency
74
+ name: yard
75
+ requirement: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - "~>"
78
+ - !ruby/object:Gem::Version
79
+ version: 0.9.2
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 0.9.2
83
+ type: :development
84
+ prerelease: false
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.9.2
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: 0.9.2
93
+ description:
94
+ email: alexjfigueroa [ at ] gmail [ dot ] com
95
+ executables: []
96
+ extensions: []
97
+ extra_rdoc_files: []
98
+ files:
99
+ - Gemfile
100
+ - LICENSE
101
+ - README.md
102
+ - Rakefile
103
+ - lib/pug.rb
104
+ - lib/pug/action/controller.rb
105
+ - lib/pug/action/enumerator.rb
106
+ - lib/pug/action/input.rb
107
+ - lib/pug/action/output.rb
108
+ - lib/pug/bot.rb
109
+ - lib/pug/clients/factory.rb
110
+ - lib/pug/configuration.rb
111
+ - lib/pug/interfaces/action.rb
112
+ - lib/pug/interfaces/client.rb
113
+ - lib/pug/keyword_handler.rb
114
+ - lib/pug/message_handler.rb
115
+ - lib/pug/number_parser.rb
116
+ - lib/pug/results.rb
117
+ - lib/pug/strings.rb
118
+ - lib/pug/telegram_client.rb
119
+ - lib/pug/terminal_client.rb
120
+ - lib/pug/types/result.rb
121
+ - lib/pug/version.rb
122
+ - pug-bot.gemspec
123
+ - spec/lib/pug/action/controller_spec.rb
124
+ - spec/lib/pug/action/enumerator_spec.rb
125
+ - spec/lib/pug/bot_spec.rb
126
+ - spec/lib/pug/clients/factory_spec.rb
127
+ - spec/lib/pug/configuration_spec.rb
128
+ - spec/lib/pug/keyword_handler_spec.rb
129
+ - spec/lib/pug/message_handler_spec.rb
130
+ - spec/lib/pug/number_parser_spec.rb
131
+ - spec/lib/pug/types/result_spec.rb
132
+ - spec/lib/spec_helpers/mock_action.rb
133
+ - spec/lib/spec_helpers/mock_client.rb
134
+ homepage: https://github.com/ajfigueroa/pug-bot
135
+ licenses:
136
+ - MIT
137
+ metadata: {}
138
+ post_install_message:
139
+ rdoc_options: []
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '2.3'
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ requirements: []
153
+ rubyforge_project:
154
+ rubygems_version: 2.6.14
155
+ signing_key:
156
+ specification_version: 4
157
+ summary: An automation framework for repetitive dev tasks
158
+ test_files:
159
+ - spec/lib/spec_helpers/mock_action.rb
160
+ - spec/lib/spec_helpers/mock_client.rb
161
+ - spec/lib/pug/clients/factory_spec.rb
162
+ - spec/lib/pug/types/result_spec.rb
163
+ - spec/lib/pug/number_parser_spec.rb
164
+ - spec/lib/pug/configuration_spec.rb
165
+ - spec/lib/pug/action/enumerator_spec.rb
166
+ - spec/lib/pug/action/controller_spec.rb
167
+ - spec/lib/pug/message_handler_spec.rb
168
+ - spec/lib/pug/bot_spec.rb
169
+ - spec/lib/pug/keyword_handler_spec.rb