boty 0.0.13 → 0.0.14

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dc1c058cb4c902d4905be90809d77bfbbb6cdcfc
4
- data.tar.gz: bd3dfd1478dc9a5bffeb6298f9ea211c66316f2d
3
+ metadata.gz: 1456a7baa55bfe9bd3d7f66044b48ad900753686
4
+ data.tar.gz: 3bf63d30f6d2f77b20fe2ae7b13fbbed8283ad26
5
5
  SHA512:
6
- metadata.gz: 06b5a68da3320987f4841b0ac8581e359ee0c834a2481eb86335db5605377398959108ce50a8b2a4c18ec4dd47fdb7fc2fa62dc190484970ffb8fdb8db29edc0
7
- data.tar.gz: ee0bb00924a2a9ae56d33e829e4d2a85ac44b6db970ae6334cf01f0fc32d6747e30fe32759a0caa88c8a4972d9bb960fa998171b829ae2b266070acb02b16416
6
+ metadata.gz: 13a273517a75e7d28dfcf8784167ad6950843a97ef1392bf4c522910f4ad456dade641ec935d9a79018e88c1914eba02b2b9a58664c11956beba791275abcb32
7
+ data.tar.gz: 973e402fa4780ca293790850b58690ab51d62ee0d3540bc0026f1a35190b3ee1b7846609ab2086409a3df09a6da96aaaed5973432e1745e3baeeaea68c3fc90f
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- boty (0.0.13)
4
+ boty (0.0.14)
5
5
  eventmachine
6
6
  faye-websocket
7
7
  i18n
data/lib/boty.rb CHANGED
@@ -33,4 +33,6 @@ require "boty/slack"
33
33
  require "boty/session"
34
34
  require "boty/message"
35
35
  require "boty/action"
36
+ require "boty/script_loader"
37
+ require "boty/script_dsl"
36
38
  require "boty/bot"
data/lib/boty/action.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  module Boty
2
2
  class Action
3
+ include Boty::Logger
3
4
  attr_reader :regex, :desc, :action
4
5
 
5
6
  class Description
@@ -33,11 +34,15 @@ module Boty
33
34
  end
34
35
 
35
36
  def is_this?(regex, block)
36
- if block
37
- regex == self.regex && block == self.action
38
- else
39
- regex == self.regex
40
- end
37
+ same_regex = regex == self.regex
38
+ block ? same_regex && block == self.action : same_regex
39
+ end
40
+
41
+ def execute(bot, message)
42
+ ScriptDSL.new(bot).instance_exec message, &action if message.match! regex
43
+ rescue => e
44
+ logger.error e.message
45
+ raise e
41
46
  end
42
47
  end
43
48
  end
data/lib/boty/bot.rb CHANGED
@@ -8,7 +8,7 @@ module Boty
8
8
  @handlers ||= []
9
9
  @commands ||= []
10
10
  @events = {}
11
- load_default_scripts
11
+ ScriptLoader.new(self).load
12
12
  on :message, &method(:message_handler)
13
13
  end
14
14
 
@@ -92,18 +92,6 @@ module Boty
92
92
 
93
93
  private
94
94
 
95
- def scripts_to_load
96
- default_scripts_path = File.expand_path("../../../script", __FILE__)
97
- paths = Dir["script/**/*.rb"] + Dir["#{default_scripts_path}/**/*.rb"]
98
- paths.map { |file| File.expand_path file }.uniq
99
- end
100
-
101
- def load_default_scripts
102
- scripts_to_load.each do |file|
103
- instance_eval File.read(file), file, 1
104
- end
105
- end
106
-
107
95
  def event_type(data)
108
96
  type = data["type"].to_sym
109
97
  logger.debug { "bot specifc event[#{type}] arrived: #{data}" }
@@ -129,19 +117,15 @@ module Boty
129
117
  !!(/(<@#{id}|#{name}>)/ =~ data["text"])
130
118
  end
131
119
 
132
- def execute_command(command, message)
133
- @_message = message
134
- instance_exec @_message, &command.action
135
- @_message = nil
136
- end
137
-
138
120
  def message_handler(data)
139
121
  actions = has_valid_mention?(data) ? @commands : @handlers
140
-
141
- Array(actions).each do |command|
142
- next unless match = command.regex.match(data["text"])
143
- #command.execute(self, match) #=> or something like that...
144
- execute_command command, Message.new(data, match: match)
122
+ @_message = Message.new data
123
+ begin
124
+ Array(actions).each do |action|
125
+ action.execute self, @_message
126
+ end
127
+ ensure
128
+ @_message = nil
145
129
  end
146
130
  end
147
131
  end
data/lib/boty/message.rb CHANGED
@@ -12,6 +12,10 @@ module Boty
12
12
  @match = match
13
13
  end
14
14
 
15
+ def match!(regex)
16
+ @match = regex.match @text
17
+ end
18
+
15
19
  def from?(author)
16
20
  if author.respond_to? :id
17
21
  @user.id == author.id
@@ -0,0 +1,24 @@
1
+ require "forwardable"
2
+
3
+ module Boty
4
+ class ScriptDSL
5
+ attr_accessor :bot
6
+
7
+ INSTANCES = {}
8
+ private_constant :INSTANCES
9
+ class << self
10
+ alias original_constructor new
11
+ end
12
+
13
+ extend Forwardable
14
+ def_delegators :bot, :desc, :respond, :name, :say, :im, :brain, :know_how
15
+
16
+ def initialize(bot)
17
+ @bot = bot
18
+ end
19
+
20
+ def self.new(bot)
21
+ INSTANCES[bot] ||= original_constructor bot
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,21 @@
1
+ module Boty
2
+ class ScriptLoader
3
+ def initialize(bot)
4
+ @dsl = ScriptDSL.new bot
5
+ end
6
+
7
+ def load
8
+ scripts_to_load.each do |file|
9
+ @dsl.instance_eval File.read(file), file, 1
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ def scripts_to_load
16
+ default_scripts_path = File.expand_path("../../../script", __FILE__)
17
+ paths = Dir["script/**/*.rb"] + Dir["#{default_scripts_path}/**/*.rb"]
18
+ paths.map { |file| File.expand_path file }.uniq
19
+ end
20
+ end
21
+ end
data/lib/boty/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Boty
2
- VERSION = "0.0.13"
2
+ VERSION = "0.0.14"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: boty
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.13
4
+ version: 0.0.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ricardo Valeriano
@@ -191,10 +191,11 @@ files:
191
191
  - lib/boty/action.rb
192
192
  - lib/boty/bot.rb
193
193
  - lib/boty/cli.rb
194
- - lib/boty/dsl.rb
195
194
  - lib/boty/logger.rb
196
195
  - lib/boty/message.rb
197
196
  - lib/boty/rspec.rb
197
+ - lib/boty/script_dsl.rb
198
+ - lib/boty/script_loader.rb
198
199
  - lib/boty/session.rb
199
200
  - lib/boty/slack.rb
200
201
  - lib/boty/slack/channel.rb
data/lib/boty/dsl.rb DELETED
File without changes