lib/boty/session.rb in boty-0.0.1 vs lib/boty/session.rb in boty-0.0.2

- old
+ new

@@ -18,25 +18,17 @@ end def start(&block) EM.run do login - bot = Bot.new @slack_info["self"], self - block.call bot if block_given? - debug "bot is configured and ready to go!" + bot = initialize_bot(&block) - debug "starting to listen on #{@session_url}" - @ws = Faye::WebSocket::Client.new @session_url - - @ws.on :message do |event| - debug "message arrived", event.data - bot.event JSON.parse(event.data) + stablish_connection do |ws| + ws.on :message do |event| + on_message event, bot + end end - - @ws.on :close do - debug "bye bye" - end end end private def verbose?; @verbose; end @@ -52,9 +44,31 @@ def login debug "logging in against slack right now" @slack_info = JSON.parse Net::HTTP.get(URI @rtm_start_url) debug "yep! logged in!" @session_url = @slack_info["url"] - @logged = true + end + + def initialize_bot(&block) + Bot.new(@slack_info["self"], self).tap { |bot| + block.call bot if block_given? + debug "bot is configured and ready to go!" + } + end + + def on_message(event, bot) + debug "message arrived", event.data + bot.event JSON.parse(event.data) + end + + def on_close + debug "bye byeb." + end + + def stablish_connection + debug "starting to listen on #{@session_url}" + ws = Faye::WebSocket::Client.new @session_url + ws.on :close do on_close end + yield ws if block_given? end end end