rslack 0.2.5 → 0.2.7

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: ed64057e1a6a7e9a5c3541d6ec477c551b480ce6
4
- data.tar.gz: 9b0f306e0a6a311e4c862b58b48c92eee20e7f09
3
+ metadata.gz: 8feb48a7730fc536e14a81b525a755af73c9d8fb
4
+ data.tar.gz: 35fb641427ea9320d0258224cf105966156013d5
5
5
  SHA512:
6
- metadata.gz: e3951ed8d9a74f734bdf9294009a24a88d8d1a4ed2c5e471ece1da4028623e6d93522f76bf820f0aee9ebbe92ddeefedf9d21d6119068577ae831225aaddd063
7
- data.tar.gz: e904de9637818adf05ad7cf9593d106a5631ba1e88dcea2e98b8687e59745ff3fd91f8306df114c5071859b0d9fbce36a323001e410e812739560ddf12d3972b
6
+ metadata.gz: 68a0a9e94ac80dcb4338e8b67c109b6e9c3c614f3c432361159b58bfb15f64564716778aa8cfb6f9daee07e04687b8d6267e2f38fe617eda2c2cf0b458300643
7
+ data.tar.gz: a45bd0b20d33d29a32345bb299decaccb0b0b282d02bb281f556e9356af19737fb8df35b06723d92e78b3706e4fb2da2b7a7ebd4716261644d28d9a20414b66a
@@ -11,8 +11,7 @@ module RSlack
11
11
  bot = build(args)
12
12
 
13
13
  Signal.trap(:INT) do
14
- bot.force_stop
15
- exit 0
14
+ bot.stop(force: true)
16
15
  end
17
16
 
18
17
  bot.start
@@ -17,13 +17,10 @@ module RSlack
17
17
  helptext: 'List cron schedules.',
18
18
  callback: 'list'
19
19
 
20
- class << self
21
- attr_accessor :timer
22
- end
23
-
24
20
  def initialize(bot)
25
- super(bot)
21
+ @timer = nil
26
22
  @channel = {}
23
+ super(bot)
27
24
  end
28
25
 
29
26
  def add(msg)
@@ -64,16 +61,16 @@ module RSlack
64
61
  parser = CronParser.new(msg.var.syntax)
65
62
  sleep = parser.next(Time.now) - Time.now
66
63
 
67
- CronAction.timer = EM::Timer.new(sleep) do
64
+ @timer = EM::Timer.new(sleep) do
68
65
  item = OpenStruct.new(msg.to_h.merge(text: msg.var.command))
69
66
  item.delete_field(:var)
70
67
  bot.react(item)
71
- CronAction.timer = start(msg)
68
+ start(msg)
72
69
  end
73
70
  end
74
71
 
75
72
  def stop
76
- CronAction.timer.cancel
73
+ @timer.cancel if @timer
77
74
  end
78
75
 
79
76
  private
@@ -1,3 +1,4 @@
1
+ require 'faye/websocket'
1
2
  require 'http'
2
3
  require 'json'
3
4
 
@@ -7,17 +8,20 @@ module RSlack
7
8
 
8
9
  BASEURL = "https://slack.com/api/"
9
10
 
10
- attr_accessor :token
11
+ attr_reader :url
12
+ attr_reader :token
11
13
 
12
- def initialize(token)
14
+ def initialize(url: BASEURL, token:)
15
+ @url = url
13
16
  @token = token
14
17
  end
15
18
 
16
- def rtm
17
- call('rtm.start')
19
+ def websocket
20
+ rtm = call('rtm.start')
21
+ Faye::WebSocket::Client.new(rtm['url'], {}, { ping: 60 })
18
22
  end
19
23
 
20
- def auth
24
+ def me
21
25
  call('auth.test')
22
26
  end
23
27
 
@@ -1,5 +1,4 @@
1
1
  require 'eventmachine'
2
- require 'faye/websocket'
3
2
  require 'rslack/api'
4
3
 
5
4
  require 'json'
@@ -15,12 +14,8 @@ module RSlack
15
14
  class << self
16
15
 
17
16
  def register(route)
18
- routes.push(route)
19
- end
20
-
21
- private
22
- def routes
23
17
  @routes ||= []
18
+ @routes.push(route)
24
19
  end
25
20
 
26
21
  end
@@ -34,26 +29,29 @@ module RSlack
34
29
  STORAGE_INTERVAL = 5
35
30
 
36
31
  def initialize(token, log: nil)
37
- @client = APIClient.new(token)
32
+ @client = APIClient.new(token: token)
38
33
  @logger = Logger.new(log || STDOUT)
39
34
  @memory = {}
35
+ @running = true
40
36
 
41
37
  @routes = self.class.instance_variable_get(:@routes)
38
+ @actions = @routes.reduce({}) { |a, e| a[e.action] ||= e.action.new(self); a }
39
+ @actions.keys.each { |e, _| @logger.info("#{self.class} loads #{e}.") }
42
40
  @storage = LevelDB::DB.new(File.join(Dir.pwd, "#{STORAGE_KEYNAME}.db"))
41
+ @botuser = @client.me
43
42
 
44
- @auth = @client.auth
45
- unless @auth['ok']
43
+ unless @botuser['ok']
46
44
  @logger.fatal("#{self.class} Failed to access slack web api.")
47
45
  exit 1
48
46
  end
49
47
  end
50
48
 
51
49
  def name
52
- "@#{@auth['user']}"
50
+ "@#{@botuser['user']}"
53
51
  end
54
52
 
55
53
  def linked_name
56
- "<@#{@auth['user_id']}>"
54
+ "<@#{@botuser['user_id']}>"
57
55
  end
58
56
 
59
57
  def reply(msg, **opts)
@@ -70,38 +68,41 @@ module RSlack
70
68
  count += 1
71
69
  message = msg.dup
72
70
  message.var = OpenStruct.new(matched.named_captures)
73
- logger.info("#{self.class} passed msg to: '#{route.action}'.")
71
+ logger.info("#{self.class} passed the msg to: '#{route.action}'.")
74
72
 
75
- th = Thread.start do
73
+ Thread.start do
76
74
  begin
77
- action = route.action.new(self)
75
+ action = @actions[route.action]
78
76
  action.instance_exec(message.freeze, &route.callback)
79
77
  rescue => e
80
- reply(msg, text: "_#{e}._")
78
+ reply(msg, text: "#{route.action} encountered an error: _#{e}._")
81
79
  end
82
80
  end
83
- th.abort_on_exception = true
84
81
  end
85
82
 
86
- reply(msg, text: 'I have no idea what to do...') if msg.prefix == linked_name && count.zero?
83
+ reply(msg, text: 'Sorry, I have no idea what to do...') if msg.prefix == linked_name && count.zero?
87
84
  end
88
85
 
89
86
  def start
90
87
  load_storage
91
88
  EM.run { wait_for_message }
89
+ logger.info("#{self.class} started.")
92
90
  end
93
91
 
94
- def stop
95
- EM.stop
96
- dump_storage
97
- end
92
+ def stop(force: false)
93
+ @running = !force
98
94
 
99
- def force_stop
100
95
  EM.stop
96
+ exit 0 unless @running
97
+
98
+ dump_storage
99
+ logger.info("#{self.class} stopped.")
101
100
  end
102
101
 
103
102
  def restart
104
103
  stop
104
+
105
+ sleep 5
105
106
  start
106
107
  end
107
108
 
@@ -109,22 +110,25 @@ module RSlack
109
110
  def wait_for_message
110
111
  EM.add_periodic_timer(STORAGE_INTERVAL) { dump_storage }
111
112
 
112
- url = @client.rtm['url']
113
- rtm = Faye::WebSocket::Client.new(url)
113
+ ws = @client.websocket
114
+
115
+ ws.on(:open) do |event|
116
+ logger.info("#{self.class} slack rtm api connection has opened.")
117
+ end
114
118
 
115
- rtm.on(:open) do |event|
116
- logger.info("#{self.class} slack realtime api connection has opened.")
119
+ ws.on(:close) do |event|
120
+ logger.info("#{self.class} slack rtm api connection has closed.")
121
+ restart if EM.reactor_running? && @running
117
122
  end
118
123
 
119
- rtm.on(:close) do |event|
120
- logger.info("#{self.class} slack realtime api connection has closed.")
121
- restart if EM.reactor_running?
124
+ ws.on(:error) do |event|
125
+ logger.info("#{self.class} slack rtm api error has occured: #{event.message}")
122
126
  end
123
127
 
124
- rtm.on(:message) do |event|
128
+ ws.on(:message) do |event|
125
129
  msg = OpenStruct.new(JSON.parse(event.data))
126
130
  if msg.type == 'message' && msg.subtype.nil?
127
- logger.info("#{self.class} recieved msg: '#{msg.text}'.")
131
+ logger.info("#{self.class} recieved a msg: '#{msg.text}'.")
128
132
 
129
133
  if msg.text =~ /^\s*(#{linked_name})\s*(.*)$/
130
134
  msg.prefix = $1.to_s
@@ -1,3 +1,3 @@
1
1
  module RSlack
2
- VERSION = "0.2.5"
2
+ VERSION = "0.2.7"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rslack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.2.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - haccht
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-05 00:00:00.000000000 Z
11
+ date: 2018-05-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler