ruboty-hubot 1.0.0 → 3.0.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9f0e5c0842257c3dbb56ac1413afea7297326dcb
4
- data.tar.gz: 9b073aca8dcb346ecf6888547e93ea4c34271b98
3
+ metadata.gz: 08178c671464d11a66e37e77af0ffac63f17378b
4
+ data.tar.gz: 39e08ee9c98106aa374b15f104118a08e7978407
5
5
  SHA512:
6
- metadata.gz: cdad93387e226601a964264aee0016bc0291a1fe02f40e77fae7eee057f47b1c364f7763b7ce2257b0922283de9dd56f357288b0aac832047d7b2b249f017b27
7
- data.tar.gz: 2bdf4a1ff82fdd79e2f4e21d3952e2b528495204dfb6ef300ad78311141adf5c980e664ff625d732d4674b21fe617e6074df595704c2093f8b4c6aa0f5637222
6
+ metadata.gz: 7d2270185a67e19933c21d843dc7ca3c74f507b5e3cb5ff19863c6bbb501e7c98e244570a67f7cef12880b1302ed12ae837033c6ae1d8a04ea7cd0bf6eba1b72
7
+ data.tar.gz: 31c25946be925da58e76ece1cbe5639f916b5d30d2417020ee77f5e6ab8831104b5e11cb68b306acc011eaaf623f19587921314a500f17b348db5f74b21b5cc6
data/README.md CHANGED
@@ -35,9 +35,9 @@ module.exports = function(robot) {
35
35
  ## TODO
36
36
 
37
37
  - [x] support `.js` scripts
38
- - [ ] support `.coffee` scripts
38
+ - [x] support `.coffee` scripts
39
39
  - [x] support robot.respond
40
- - [ ] support robot.hear
40
+ - [x] support robot.hear
41
41
  - [ ] support more complex methods (e.x. robot.http)
42
42
  - [ ] support `external-scripts.json`
43
43
  - [ ] support `hubot-scripts.json`
@@ -1,8 +1,13 @@
1
1
  module Ruboty
2
2
  module Handlers
3
3
  class Hubot < Base
4
+ on /.*/, name: 'hear', description: 'respond by hubot scripts', all: true
4
5
  on /.*/, name: 'respond', description: 'respond by hubot scripts'
5
6
 
7
+ def hear(message)
8
+ Ruboty::Hubot::Actions::Hear.new(message).call
9
+ end
10
+
6
11
  def respond(message)
7
12
  Ruboty::Hubot::Actions::Respond.new(message).call
8
13
  end
data/lib/ruboty/hubot.rb CHANGED
@@ -2,6 +2,7 @@ require "ruboty/hubot/version"
2
2
  require "ruboty/hubot/robot"
3
3
  require "ruboty/hubot/script"
4
4
  require "ruboty/handlers/hubot"
5
+ require "ruboty/hubot/actions/hear"
5
6
  require "ruboty/hubot/actions/respond"
6
7
 
7
8
  module Ruboty
@@ -0,0 +1,13 @@
1
+ module Ruboty
2
+ module Hubot
3
+ module Actions
4
+ class Hear < Ruboty::Actions::Base
5
+ def call
6
+ Robot.instance.receive_all(message.body) do |res|
7
+ message.reply(res)
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -2,26 +2,9 @@ module Ruboty
2
2
  module Hubot
3
3
  module Actions
4
4
  class Respond < Ruboty::Actions::Base
5
- class << self
6
- include Mem
7
-
8
- def scripts
9
- Dir.glob(File.join(Dir.pwd, 'scripts/*')).map do |path|
10
- Script.new(path)
11
- end
12
- end
13
-
14
- def robot
15
- robot = Robot.new
16
- scripts.each {|script| robot.load_script script }
17
- robot
18
- end
19
- memoize :robot
20
- end
21
-
22
5
  def call
23
- self.class.robot.receive(message.body) do |res|
24
- message.reply("@#{message.from_name} #{res}")
6
+ Robot.instance.receive_mention(message.body) do |res|
7
+ message.reply(res)
25
8
  end
26
9
  end
27
10
  end
@@ -1,20 +1,20 @@
1
1
  require 'execjs'
2
+ require 'singleton'
2
3
 
3
4
  module Ruboty
4
5
  module Hubot
5
6
  class Robot
6
7
  include Mem
8
+ include Singleton
7
9
 
8
- def initialize
9
- @scripts = []
10
- end
11
-
12
- def load_script(script)
13
- @scripts << script
10
+ def receive_all(text)
11
+ if res_text = robot_context.call("ruboty.receiveAll", text)
12
+ yield res_text if block_given?
13
+ end
14
14
  end
15
15
 
16
- def receive(text)
17
- if res_text = robot_context.call("ruboty.receive", text)
16
+ def receive_mention(text)
17
+ if res_text = robot_context.call("ruboty.receiveMention", text)
18
18
  yield res_text if block_given?
19
19
  end
20
20
  end
@@ -28,47 +28,62 @@ module Ruboty
28
28
 
29
29
  def robot_js
30
30
  @robot_js = <<ROBOT_JS
31
- var module = {};
31
+ var ruboty = {
32
+ // mention
33
+ respondHandlers: [],
32
34
 
33
- #{@scripts.join(";\n")}
35
+ respond: function (regexp, callback) {
36
+ ruboty.respondHandlers.push({
37
+ regexp: regexp,
38
+ callback: callback
39
+ });
40
+ },
34
41
 
35
- var ruboty = {
36
- handlers: [],
37
- respond: function (regexp, callback) {
38
- ruboty.handlers.push({
39
- response: function (text) {
40
- var match;
41
- if (match = text.match(regexp)) {
42
- var res = {
43
- match: match,
44
- sentText: null,
45
- send: function (text) { return res.sentText = text; }
46
- };
47
- return res;
48
- }
49
- },
50
- callback: callback
51
- });
52
- },
42
+ receiveMention: function (text) {
43
+ return ruboty.receive(text, ruboty.respondHandlers);
44
+ },
53
45
 
54
- // all
55
- hear: undefined,
46
+ // all
47
+ hearHandlers: [],
56
48
 
57
- // mention
58
- receive: function (text) {
59
- var i, len, res;
60
- for (i = 0, len = ruboty.handlers.length; i < len; i++) {
61
- if (res = ruboty.handlers[i].response(text)) {
62
- return ruboty.handlers[i].callback(res);
63
- }
64
- }
65
- return undefined;
66
- }
49
+ hear: function (regexp, callback) {
50
+ ruboty.hearHandlers.push({
51
+ regexp: regexp,
52
+ callback: callback
53
+ });
54
+ },
55
+
56
+ receiveAll: function (text) {
57
+ return ruboty.receive(text, ruboty.hearHandlers);
58
+ },
59
+
60
+ receive: function (text, handlers) {
61
+ var i, len, match, message;
62
+ for (i = 0, len = handlers.length; i < len; i++) {
63
+ if (match = text.match(handlers[i].regexp)) {
64
+ message = {
65
+ match: match,
66
+ sentText: null,
67
+ send: function (text) { return message.sentText = text; }
68
+ };
69
+ handlers[i].callback(message);
70
+ return message.sentText;
71
+ }
72
+ }
73
+ return undefined;
74
+ }
67
75
  };
76
+ var module = {};
77
+ #{scripts.map {|s| "#{s}\nif (module.exports) { module.exports(ruboty); module.exports = undefined; }\n" }.join("\n")}
68
78
 
69
- module.exports(ruboty);
70
79
  ROBOT_JS
71
80
  end
81
+
82
+ def scripts
83
+ Dir.glob(File.join(Dir.pwd, 'scripts/*')).map do |path|
84
+ Script.new(path)
85
+ end
86
+ end
72
87
  end
73
88
  end
74
89
  end
@@ -1,9 +1,36 @@
1
+ require 'mem'
2
+ require 'execjs'
3
+ require "open-uri"
4
+
1
5
  module Ruboty
2
6
  module Hubot
3
7
  class Script
4
- # TODO: compile .coffee
8
+ class << self
9
+ include Mem
10
+
11
+ def compile(source)
12
+ coffee.call("CoffeeScript.compile", source, bare: true)
13
+ end
14
+
15
+ private
16
+
17
+ def coffee
18
+ ExecJS.compile(coffee_source)
19
+ end
20
+ memoize :coffee
21
+
22
+ def coffee_source
23
+ open("http://coffeescript.org/extras/coffee-script.js").read
24
+ end
25
+ memoize :coffee_source
26
+ end
27
+
5
28
  def initialize(source_path)
6
- @source = File.read(source_path)
29
+ if File.extname(source_path).downcase == '.coffee'
30
+ @source = self.class.compile(File.read(source_path))
31
+ else
32
+ @source = File.read(source_path)
33
+ end
7
34
  end
8
35
 
9
36
  def to_s
@@ -1,5 +1,5 @@
1
1
  module Ruboty
2
2
  module Hubot
3
- VERSION = "1.0.0"
3
+ VERSION = "3.0.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruboty-hubot
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - block_given?
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-11 00:00:00.000000000 Z
11
+ date: 2014-10-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruboty
@@ -108,6 +108,7 @@ files:
108
108
  - Rakefile
109
109
  - lib/ruboty/handlers/hubot.rb
110
110
  - lib/ruboty/hubot.rb
111
+ - lib/ruboty/hubot/actions/hear.rb
111
112
  - lib/ruboty/hubot/actions/respond.rb
112
113
  - lib/ruboty/hubot/robot.rb
113
114
  - lib/ruboty/hubot/script.rb
@@ -138,3 +139,4 @@ signing_key:
138
139
  specification_version: 4
139
140
  summary: ruboty plugin for using hubot script in ruboty.
140
141
  test_files: []
142
+ has_rdoc: