ellen 0.0.1 → 0.0.2

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: e25de66dca77cd59a37365477996c7323c2cdd83
4
- data.tar.gz: d6aae02262e034f3f603d4c23fa92666b718d357
3
+ metadata.gz: 1a7c6482b9bfde724af1ad6fc7ce974df7af1714
4
+ data.tar.gz: 77363d87ff9d8c0894f0b832c3ccb9221a6abee9
5
5
  SHA512:
6
- metadata.gz: 9696111504ef24468dbc0dac2c8a606842133d88e42ab7e6818cc022b8fa9e31424d6e1d02f40849ef19b32fdd00e8843284808de94ea994e6c46c183ffbeac2
7
- data.tar.gz: 518c304aa42d6f1d1eeacd5c4a4f0e01b61e6846b4dc835237f655cb904c2a3da88032fa33446459df2512fc4263740412366b880c7df4a3feba1234bf7d4d04
6
+ metadata.gz: 28c4776e263d80a48539d6bb0863b312ac9c78aaf14acc03378ee08ac97cd62e74436b9382ee29bc89570b640364942a5f70e73d8feb4e72d1976561e05e4619
7
+ data.tar.gz: 7766b5ec90898a46545db4a4a6a92cf650971f7fd79707848e407e98fcf49e7d20e7fd4202680539cd5157ddf04eb26447ad0d293bdfe42ac24e60950f4adb0a
data/CHANGELOG.md ADDED
@@ -0,0 +1,6 @@
1
+ ### 0.0.2
2
+ * Add Handler DSL Syntax
3
+ * Add --load option to load a ruby file before running
4
+
5
+ ### 0.0.1
6
+ * 1st release
data/lib/ellen.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require "active_support/core_ext/module/delegation"
1
2
  require "active_support/core_ext/string/inflections"
2
3
  require "bundler"
3
4
  require "mem"
@@ -28,6 +29,7 @@ module Ellen
28
29
  end
29
30
  end
30
31
 
32
+ require "ellen/action"
31
33
  require "ellen/adapter_builder"
32
34
  require "ellen/adapters/base"
33
35
  require "ellen/adapters/shell"
@@ -38,5 +40,6 @@ require "ellen/commands/run"
38
40
  require "ellen/handlers/base"
39
41
  require "ellen/handlers/help"
40
42
  require "ellen/logger"
43
+ require "ellen/message"
41
44
  require "ellen/robot"
42
45
  require "ellen/version"
@@ -0,0 +1,15 @@
1
+ module Ellen
2
+ class Action
3
+ attr_reader :pattern, :options, :block
4
+
5
+ def initialize(pattern, options = {}, &block)
6
+ @pattern = pattern
7
+ @options = options
8
+ @block = block
9
+ end
10
+
11
+ def call(handler, message)
12
+ handler.robot.instance_exec(message, &block) if message.match(pattern, options)
13
+ end
14
+ end
15
+ end
@@ -14,7 +14,6 @@ module Ellen
14
14
  class Base
15
15
  class << self
16
16
  def inherited(child)
17
- super
18
17
  Ellen.adapters[child.name.split("::").last.underscore] = child
19
18
  end
20
19
  end
@@ -25,6 +24,10 @@ module Ellen
25
24
  @robot = robot
26
25
  @options = options
27
26
  end
27
+
28
+ def say(body, options = {})
29
+ Ellen.logger.info("Not implemented #{self.class}##{__method__} was called")
30
+ end
28
31
  end
29
32
  end
30
33
  end
@@ -8,6 +8,10 @@ module Ellen
8
8
  listen
9
9
  end
10
10
 
11
+ def say(body, options = {})
12
+ puts body
13
+ end
14
+
11
15
  private
12
16
 
13
17
  def explain
@@ -28,17 +32,23 @@ module Ellen
28
32
 
29
33
  def listen
30
34
  loop { step }
35
+ rescue Interrupt
36
+ exit
31
37
  end
32
38
 
33
39
  def step
34
40
  prompt
35
- case message = read
41
+ case body = read
36
42
  when "exit", "quit"
37
43
  exit
38
44
  else
39
- robot.receive(message)
45
+ robot.receive(body: body, source: source, command: true)
40
46
  end
41
47
  end
48
+
49
+ def source
50
+ "shell user"
51
+ end
42
52
  end
43
53
  end
44
54
  end
@@ -20,8 +20,9 @@ module Ellen
20
20
 
21
21
  def options
22
22
  Slop.parse!(arguments, help: true) do
23
- on("g", "generate", "Generate a new chatterbot with ./ellen/ directory if specified.")
24
23
  on("a", "adapter=", "Take adapter name.")
24
+ on("g", "generate", "Generate a new chatterbot with ./ellen/ directory if specified.")
25
+ on("l", "load=", "Load a ruby file before running.")
25
26
  end.to_hash
26
27
  end
27
28
  memoize :options
@@ -12,10 +12,20 @@ module Ellen
12
12
  module Handlers
13
13
  class Base
14
14
  class << self
15
+ include Mem
16
+
15
17
  def inherited(child)
16
- super
17
18
  Ellen.handlers << child
18
19
  end
20
+
21
+ def on(pattern, options = {}, &block)
22
+ actions << Action.new(pattern, options, &block)
23
+ end
24
+
25
+ def actions
26
+ []
27
+ end
28
+ memoize :actions
19
29
  end
20
30
 
21
31
  attr_reader :robot
@@ -23,6 +33,12 @@ module Ellen
23
33
  def initialize(robot)
24
34
  @robot = robot
25
35
  end
36
+
37
+ def call(message)
38
+ self.class.actions.each do |action|
39
+ action.call(self, message)
40
+ end
41
+ end
26
42
  end
27
43
  end
28
44
  end
@@ -1,16 +1,8 @@
1
1
  module Ellen
2
2
  module Handlers
3
3
  class Help < Base
4
- def call(message)
5
- case message
6
- when /help\z/
7
- help
8
- end
9
- end
10
-
11
- # TODO
12
- def help
13
- puts "Not yet implemented"
4
+ on /help\z/, command: true do |message|
5
+ say "You'll be okay", destination: message.source
14
6
  end
15
7
  end
16
8
  end
@@ -0,0 +1,15 @@
1
+ module Ellen
2
+ class Message
3
+ attr_reader :body, :source, :command
4
+
5
+ def initialize(options)
6
+ @body = options[:body]
7
+ @source = options[:source]
8
+ @command = options[:command]
9
+ end
10
+
11
+ def match(pattern, options = {})
12
+ (!options[:command] || command) && pattern === body
13
+ end
14
+ end
15
+ end
data/lib/ellen/robot.rb CHANGED
@@ -2,6 +2,8 @@ module Ellen
2
2
  class Robot
3
3
  include Mem
4
4
 
5
+ delegate :say, to: :adapter
6
+
5
7
  attr_reader :options
6
8
 
7
9
  def initialize(options)
@@ -10,11 +12,15 @@ module Ellen
10
12
 
11
13
  def run
12
14
  bundle
15
+ setup
13
16
  adapt
14
17
  end
15
18
 
16
- def receive(message)
17
- handlers.each {|handler| handler.call(message) }
19
+ def receive(attributes)
20
+ message = Message.new(attributes)
21
+ handlers.each do |handler|
22
+ handler.call(message)
23
+ end
18
24
  end
19
25
 
20
26
  private
@@ -32,6 +38,10 @@ module Ellen
32
38
  Bundler.require
33
39
  end
34
40
 
41
+ def setup
42
+ load(options[:load]) if options[:load]
43
+ end
44
+
35
45
  def handlers
36
46
  Ellen.handlers.map {|handler_class| handler_class.new(self) }
37
47
  end
data/lib/ellen/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ellen
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ellen
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
@@ -117,6 +117,7 @@ extensions: []
117
117
  extra_rdoc_files: []
118
118
  files:
119
119
  - ".gitignore"
120
+ - CHANGELOG.md
120
121
  - Gemfile
121
122
  - LICENSE.txt
122
123
  - README.md
@@ -124,6 +125,7 @@ files:
124
125
  - bin/ellen
125
126
  - ellen.gemspec
126
127
  - lib/ellen.rb
128
+ - lib/ellen/action.rb
127
129
  - lib/ellen/adapter_builder.rb
128
130
  - lib/ellen/adapters/base.rb
129
131
  - lib/ellen/adapters/shell.rb
@@ -134,6 +136,7 @@ files:
134
136
  - lib/ellen/handlers/base.rb
135
137
  - lib/ellen/handlers/help.rb
136
138
  - lib/ellen/logger.rb
139
+ - lib/ellen/message.rb
137
140
  - lib/ellen/robot.rb
138
141
  - lib/ellen/version.rb
139
142
  - spec/ellen/commands/generate_spec.rb