lib/slack/real_time/client.rb in slack-ruby-client-1.1.0 vs lib/slack/real_time/client.rb in slack-ruby-client-2.0.0
- old
+ new
@@ -9,19 +9,13 @@
include Api::MessageId
include Api::Ping
include Api::Message
include Api::Typing
- @events = {}
-
- class << self
- attr_accessor :events
- end
-
attr_accessor :web_client, :store, :url, *Config::ATTRIBUTES
- protected :store_class, :store_class=
+ protected :store_class, :store_class=, :store_options, :store_options=
def initialize(options = {})
@callbacks = Hash.new { |h, k| h[k] = [] }
Slack::RealTime::Config::ATTRIBUTES.each do |key|
send("#{key}=", options.key?(key) ? options[key] : Slack::RealTime.config.send(key))
@@ -29,11 +23,11 @@
@token ||= Slack.config.token
@logger ||= (Slack::Config.logger || Slack::Logger.default)
@web_client = Slack::Web::Client.new(token: token, logger: logger)
end
- %i[users self channels team teams groups ims bots].each do |store_method|
+ [:self, :team, *Stores::Base::CACHES].each do |store_method|
define_method store_method do
store&.send(store_method)
end
end
@@ -160,38 +154,28 @@
protected
def restart_async
logger.debug("#{self}##{__method__}")
@socket.close
- start = web_client.send(rtm_start_method, start_options)
+ start = web_client.rtm_connect(start_options)
data = Slack::Messages::Message.new(start)
@url = data.url
- @store = @store_class.new(data) if @store_class
+ @store = store_class.new(data, store_options.to_h) if store_class
@socket.restart_async(self, @url)
end
# @return [Slack::RealTime::Socket]
def build_socket
raise ClientAlreadyStartedError if started?
- start = web_client.send(rtm_start_method, start_options)
+ start = web_client.rtm_connect(start_options)
data = Slack::Messages::Message.new(start)
@url = data.url
- @store = @store_class.new(data) if @store_class
+ @store = store_class.new(data, store_options.to_h) if store_class
@socket = socket_class.new(@url, socket_options)
end
- def rtm_start_method
- if start_method
- start_method
- elsif @store_class && @store_class <= Slack::RealTime::Stores::Store
- :rtm_start
- else
- :rtm_connect
- end
- end
-
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
@@ -247,14 +231,12 @@
logger.error("#{self}##{__method__}") { e }
false
end
def run_handlers(type, data)
- return unless store.class.events
-
handlers = store.class.events[type.to_s]
- handlers&.each do |handler|
- store.instance_exec(data, &handler)
+ handlers.each do |handler|
+ store.instance_exec(data, self, &handler)
end
rescue StandardError => e
logger.error("#{self}##{__method__}") { e }
false
end