lib/slack/real_time/client.rb in slack-ruby-client-0.6.0 vs lib/slack/real_time/client.rb in slack-ruby-client-0.7.0
- old
+ new
@@ -18,18 +18,21 @@
attr_accessor :web_client
attr_accessor :store
attr_accessor :url
attr_accessor(*Config::ATTRIBUTES)
+ protected :logger, :logger=
+ protected :store_class, :store_class=
+
def initialize(options = {})
@callbacks = Hash.new { |h, k| h[k] = [] }
Slack::RealTime::Config::ATTRIBUTES.each do |key|
- send("#{key}=", options[key] || Slack::RealTime.config.send(key))
+ send("#{key}=", options.key?(key) ? options[key] : Slack::RealTime.config.send(key))
end
- @store_class = options.key?(:store_class) ? options[:store_class] : Slack::RealTime::Store
@token ||= Slack.config.token
- @web_client = Slack::Web::Client.new(token: token)
+ @logger ||= (Slack::Config.logger || Slack::Logger.default)
+ @web_client = Slack::Web::Client.new(token: token, logger: logger)
end
[:users, :self, :channels, :team, :teams, :groups, :ims, :bots].each do |store_method|
define_method store_method do
store.send(store_method) if store
@@ -89,29 +92,33 @@
def socket_options
socket_options = {}
socket_options[:ping] = websocket_ping if websocket_ping
socket_options[:proxy] = websocket_proxy if websocket_proxy
+ socket_options[:logger] = logger
socket_options
end
def run_loop(socket)
@socket = socket
@socket.connect! do |driver|
yield driver if block_given?
driver.on :open do |event|
+ logger.debug("#{self.class}##{__method__}") { event.class.name }
open(event)
callback(event, :open)
end
driver.on :message do |event|
+ logger.debug("#{self.class}##{__method__}") { "#{event.class}, #{event.data}" }
dispatch(event)
end
driver.on :close do |event|
+ logger.debug("#{self.class}##{__method__}") { event.class.name }
callback(event, :close)
close(event)
end
end
end
@@ -121,10 +128,11 @@
concurrency::Socket
end
def send_json(data)
fail ClientNotStartedError unless started?
+ logger.debug("#{self.class}##{__method__}") { data }
@socket.send_data(data.to_json)
end
def open(_event)
end
@@ -142,33 +150,49 @@
callbacks = self.callbacks[type.to_s]
return false unless callbacks
callbacks.each do |c|
c.call(event)
end
+ true
+ rescue StandardError => e
+ logger.error e
+ false
end
def dispatch(event)
return false unless event.data
data = Slack::Messages::Message.new(JSON.parse(event.data))
type = data.type
return false unless type
type = type.to_s
+ logger.debug("#{self.class}##{__method__}") { data.to_s }
run_handlers(type, data) if @store
run_callbacks(type, data)
+ rescue StandardError => e
+ logger.error e
+ false
end
def run_handlers(type, data)
- handler = Slack::RealTime::Client.events[type]
- handler.call(self, data) if handler
+ handlers = store.class.events[type.to_s]
+ handlers.each do |handler|
+ store.instance_exec(data, &handler)
+ end if handlers
+ rescue StandardError => e
+ logger.error e
+ false
end
def run_callbacks(type, data)
callbacks = self.callbacks[type]
return false unless callbacks
callbacks.each do |c|
c.call(data)
end
true
+ rescue StandardError => e
+ logger.error e
+ false
end
end
end
end