ellen 0.1.0 → 0.1.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
  SHA1:
3
- metadata.gz: 2de2c293649270b33753969b8e1b504eb63ca821
4
- data.tar.gz: 2f0cbbadad308168ca053235d503c86dfca3a7f7
3
+ metadata.gz: 96e3c30b37c53dec2a1f1b781a9bbb8d21dbcb4b
4
+ data.tar.gz: ae06dedaad859870eb47f91bc121ce94bbf5e040
5
5
  SHA512:
6
- metadata.gz: 652d8b2f4d97172ef2d72c2877528eeca0f0e4a8cd0b10c7a18aa8f7b3cdbe769fa58033f3f4f748a6e44335173d8d6d639e629836954ccfed5868dd3425d0d4
7
- data.tar.gz: 1b04c2c135b32f78fa9a7765718477345d5fd0b6d4b72fefb454253597b0e64c635f0a2e42bca14ead4a9128f189f7905accd6916b29897a6a29132d923c9b74
6
+ metadata.gz: d6daf3ce130b28062a9f998b5a930f1699210f913d0786f93116dcd8594d48462a7cfd0ea754647232de3b0dda202439c19037229e0b45bffa35b8c9667511fe
7
+ data.tar.gz: cba65d6007a7d5fb66d970ce99914ecf93a9ebaea6254c8414a6e511ec2605dbdee15ea49c71083b0dcaa37e3e32909a7ecbbdbe445622de9a96de7a286f4adf
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ ## 0.1.1
2
+ * Improve Brain interface
3
+
1
4
  ## 0.1.0
2
5
  * Deprecate --adapter option
3
6
 
data/lib/ellen.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require "active_support/concern"
1
2
  require "active_support/core_ext/module/delegation"
2
3
  require "active_support/core_ext/string/inflections"
3
4
  require "bundler"
@@ -18,20 +19,22 @@ module Ellen
18
19
  exit(1)
19
20
  end
20
21
 
21
- def adapters
22
- {}
23
- end
24
- memoize :adapters
25
-
26
22
  def handlers
27
23
  []
28
24
  end
29
25
  memoize :handlers
26
+
27
+ def actions
28
+ handlers.map(&:actions).flatten.sort_by(&:all?)
29
+ end
30
30
  end
31
31
  end
32
32
 
33
33
  require "ellen/action"
34
34
  require "ellen/adapter_builder"
35
+ require "ellen/env"
36
+ require "ellen/env/missing_required_key_error"
37
+ require "ellen/env/validatable"
35
38
  require "ellen/adapters/base"
36
39
  require "ellen/adapters/shell"
37
40
  require "ellen/brains/base"
@@ -40,7 +43,6 @@ require "ellen/command_builder"
40
43
  require "ellen/commands/base"
41
44
  require "ellen/commands/generate"
42
45
  require "ellen/commands/run"
43
- require "ellen/env"
44
46
  require "ellen/handlers/base"
45
47
  require "ellen/handlers/help"
46
48
  require "ellen/handlers/ping"
@@ -12,39 +12,24 @@
12
12
  module Ellen
13
13
  module Adapters
14
14
  class Base
15
+ include Env::Validatable
16
+
15
17
  class << self
16
18
  def inherited(child_class)
17
19
  Ellen::AdapterBuilder.adapter_classes << child_class
18
20
  end
19
-
20
- def env(key, description, options = {})
21
- envs << Env.new(key, description, options)
22
- end
23
-
24
- def envs
25
- @envs ||= []
26
- end
27
-
28
- def usage
29
- envs.map(&:to_usage).join("\n")
30
- end
31
21
  end
32
22
 
33
23
  attr_reader :robot
34
24
 
35
25
  def initialize(robot)
36
26
  @robot = robot
27
+ validate
37
28
  end
38
29
 
39
30
  def say(body, options = {})
40
31
  Ellen.logger.info("Not implemented #{self.class}##{__method__} was called")
41
32
  end
42
-
43
- def validate
44
- self.class.envs.each(&:validate)
45
- rescue Env::MissingRequiredKeyError => exception
46
- Ellen.die("#{exception}\n#{self.class.usage}")
47
- end
48
33
  end
49
34
  end
50
35
  end
@@ -3,51 +3,54 @@ require "readline"
3
3
  module Ellen
4
4
  module Adapters
5
5
  class Shell < Base
6
+ PROMPT = "> "
7
+
8
+ SOURCE = "shell"
9
+
10
+ USAGE = "Type `exit` or `quit` to end the session."
11
+
12
+ attr_accessor :stopped
13
+
6
14
  def run
7
15
  explain
8
16
  listen
9
17
  end
10
18
 
11
19
  def say(body, options = {})
12
- puts body
20
+ Ellen.logger.info(body)
13
21
  end
14
22
 
15
23
  private
16
24
 
17
25
  def explain
18
- puts "Type `exit` or `quit` to end the session."
19
- end
20
-
21
- def prompt
22
- @prompt ||= "> "
23
- end
24
-
25
- def prompt=(str)
26
- @prompt = str
26
+ Ellen.logger.info(USAGE)
27
27
  end
28
28
 
29
29
  def read
30
- Readline.readline(prompt, true)
30
+ Readline.readline(PROMPT, true)
31
31
  end
32
32
 
33
33
  def listen
34
- loop { step }
34
+ step until stopped?
35
35
  rescue Interrupt
36
- exit
36
+ stop
37
37
  end
38
38
 
39
39
  def step
40
- prompt
41
40
  case body = read
42
41
  when "exit", "quit"
43
- exit
42
+ stop
44
43
  else
45
- robot.receive(body: body, source: source, command: true)
44
+ robot.receive(body: body, source: SOURCE)
46
45
  end
47
46
  end
48
47
 
49
- def source
50
- "shell user"
48
+ def stopped?
49
+ !!stopped
50
+ end
51
+
52
+ def stop
53
+ self.stopped = true
51
54
  end
52
55
  end
53
56
  end
@@ -1,6 +1,8 @@
1
1
  module Ellen
2
2
  module Brains
3
3
  class Base
4
+ include Env::Validatable
5
+
4
6
  class << self
5
7
  def inherited(child)
6
8
  brain_classes << child
@@ -17,8 +19,8 @@ module Ellen
17
19
  end
18
20
  end
19
21
 
20
- def data
21
- @data ||= {}
22
+ def initialize
23
+ validate
22
24
  end
23
25
  end
24
26
  end
@@ -1,7 +1,8 @@
1
1
  module Ellen
2
2
  module Brains
3
3
  class Null < Base
4
- def save
4
+ def data
5
+ @data ||= {}
5
6
  end
6
7
  end
7
8
  end
data/lib/ellen/env.rb CHANGED
@@ -29,8 +29,5 @@ module Ellen
29
29
  def error
30
30
  raise MissingRequiredKeyError, %<ENV["#{key}"] is required but missing>
31
31
  end
32
-
33
- class MissingRequiredKeyError < StandardError
34
- end
35
32
  end
36
33
  end
@@ -0,0 +1,6 @@
1
+ module Ellen
2
+ class Env
3
+ class MissingRequiredKeyError < StandardError
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,27 @@
1
+ module Ellen
2
+ class Env
3
+ module Validatable
4
+ extend ActiveSupport::Concern
5
+
6
+ module ClassMethods
7
+ def envs
8
+ @envs ||= []
9
+ end
10
+
11
+ def env(key, description, options = {})
12
+ envs << Env.new(key, description, options)
13
+ end
14
+
15
+ def usage
16
+ envs.map(&:to_usage).join("\n")
17
+ end
18
+ end
19
+
20
+ def validate
21
+ self.class.envs.each(&:validate)
22
+ rescue MissingRequiredKeyError => exception
23
+ Ellen.die("#{exception}\n#{self.class.usage}")
24
+ end
25
+ end
26
+ end
27
+ end
@@ -4,12 +4,9 @@ module Ellen
4
4
  on /help\z/i, name: "help", description: "Show this help message"
5
5
 
6
6
  def help(message)
7
- lines = Ellen.handlers.map(&:actions).flatten.sort_by(&:all?).map do |action|
8
- prefix = "@#{robot.name} " unless action.all?
9
- line = "%-30s - #{action.description}" % "#{prefix}#{action.pattern.inspect}"
10
- line
11
- end
12
- robot.say lines.join("\n")
7
+ robot.say Ellen.actions.map {|action|
8
+ "%-30s - #{action.description}" % "#{robot.name unless action.all?} #{action.pattern.inspect}"
9
+ }.join("\n")
13
10
  end
14
11
  end
15
12
  end
data/lib/ellen/robot.rb CHANGED
@@ -8,7 +8,7 @@ module Ellen
8
8
 
9
9
  attr_reader :options
10
10
 
11
- def initialize(options)
11
+ def initialize(options = {})
12
12
  @options = options
13
13
  end
14
14
 
@@ -16,7 +16,7 @@ module Ellen
16
16
  dotenv
17
17
  bundle
18
18
  setup
19
- validate
19
+ remember
20
20
  adapt
21
21
  end
22
22
 
@@ -64,8 +64,8 @@ module Ellen
64
64
  end
65
65
  memoize :handlers
66
66
 
67
- def validate
68
- adapter.validate
67
+ def remember
68
+ brain
69
69
  end
70
70
  end
71
71
  end
data/lib/ellen/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ellen
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -6,11 +6,7 @@ describe Ellen::AdapterBuilder do
6
6
  end
7
7
 
8
8
  let(:robot) do
9
- Ellen::Robot.new(options)
10
- end
11
-
12
- let(:options) do
13
- {}
9
+ Ellen::Robot.new
14
10
  end
15
11
 
16
12
  describe "#build" do
@@ -0,0 +1,56 @@
1
+ require "spec_helper"
2
+
3
+ describe Ellen::Adapters::Shell do
4
+ before do
5
+ Ellen.logger.stub(:info)
6
+ end
7
+
8
+ let(:adapter) do
9
+ described_class.new(robot)
10
+ end
11
+
12
+ let(:robot) do
13
+ Ellen::Robot.new
14
+ end
15
+
16
+ describe "#run" do
17
+ context "with `exit`" do
18
+ it "stops" do
19
+ Readline.stub(readline: "exit")
20
+ adapter.should_receive(:stop).and_call_original
21
+ adapter.run
22
+ end
23
+ end
24
+
25
+ context "with `quit`" do
26
+ it "stops" do
27
+ Readline.stub(readline: "exit")
28
+ adapter.should_receive(:stop).and_call_original
29
+ adapter.run
30
+ end
31
+ end
32
+
33
+ context "with Inturrupt from console" do
34
+ it "stops" do
35
+ Readline.stub(:readline).and_raise(Interrupt)
36
+ adapter.should_receive(:stop).and_call_original
37
+ adapter.run
38
+ end
39
+ end
40
+
41
+ context "without `exit` nor `quit`" do
42
+ it "passes given message to robot" do
43
+ Readline.stub(:readline).and_return("a", "exit")
44
+ robot.should_receive(:receive).with(body: "a", source: described_class::SOURCE)
45
+ adapter.run
46
+ end
47
+ end
48
+ end
49
+
50
+ describe "#say" do
51
+ it "shows given message body on stdout" do
52
+ Ellen.logger.should_receive(:info).with("a")
53
+ adapter.say("a")
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,14 @@
1
+ require "spec_helper"
2
+
3
+ describe Ellen::Handlers::Help do
4
+ let(:robot) do
5
+ Ellen::Robot.new
6
+ end
7
+
8
+ describe "#help" do
9
+ it "responds to `@ellen help` and says each handler's usage" do
10
+ robot.should_receive(:say).with(%r<ellen /help\\z/i +- Show this help message>)
11
+ robot.receive(body: "@ellen help")
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,20 @@
1
+ require "spec_helper"
2
+
3
+ describe Ellen::Handlers::Ping do
4
+ let(:robot) do
5
+ Ellen::Robot.new
6
+ end
7
+
8
+ describe "#ping" do
9
+ {
10
+ "@ellen ping" => "pong",
11
+ "@ellen Ping" => "Pong",
12
+ "@ellen PING" => "PONG",
13
+ }.each do |input, output|
14
+ it "responds to `#{input}` and says `#{output}`" do
15
+ robot.should_receive(:say).with(output)
16
+ robot.receive(body: input)
17
+ end
18
+ end
19
+ end
20
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ellen
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-21 00:00:00.000000000 Z
11
+ date: 2014-04-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -180,6 +180,8 @@ files:
180
180
  - lib/ellen/commands/generate.rb
181
181
  - lib/ellen/commands/run.rb
182
182
  - lib/ellen/env.rb
183
+ - lib/ellen/env/missing_required_key_error.rb
184
+ - lib/ellen/env/validatable.rb
183
185
  - lib/ellen/handlers/base.rb
184
186
  - lib/ellen/handlers/help.rb
185
187
  - lib/ellen/handlers/ping.rb
@@ -188,8 +190,11 @@ files:
188
190
  - lib/ellen/robot.rb
189
191
  - lib/ellen/version.rb
190
192
  - spec/ellen/adapter_builder_spec.rb
193
+ - spec/ellen/adapters/shell_spec.rb
191
194
  - spec/ellen/commands/generate_spec.rb
192
195
  - spec/ellen/commands/run_spec.rb
196
+ - spec/ellen/handlers/help_spec.rb
197
+ - spec/ellen/handlers/ping_spec.rb
193
198
  - spec/ellen/robot_spec.rb
194
199
  - spec/spec_helper.rb
195
200
  - templates/Gemfile
@@ -219,7 +224,11 @@ specification_version: 4
219
224
  summary: A chatterbot framework, inspired by Hubot
220
225
  test_files:
221
226
  - spec/ellen/adapter_builder_spec.rb
227
+ - spec/ellen/adapters/shell_spec.rb
222
228
  - spec/ellen/commands/generate_spec.rb
223
229
  - spec/ellen/commands/run_spec.rb
230
+ - spec/ellen/handlers/help_spec.rb
231
+ - spec/ellen/handlers/ping_spec.rb
224
232
  - spec/ellen/robot_spec.rb
225
233
  - spec/spec_helper.rb
234
+ has_rdoc: