Sha256: 3fff9050c8e1e82e25b72cfee7767513dabb71addbfa96fe6856b2b364c1f258

Contents?: true

Size: 1.24 KB

Versions: 3

Compression:

Stored size: 1.24 KB

Contents

require 'faye/websocket'
require 'eventmachine'

module Slack
  module RealTime
    class Client
      def initialize(rtm_start_response)
        @response = rtm_start_response
        @url = rtm_start_response["url"]
        @callbacks ||= {}
      end

      def on(type, &block)
        @callbacks[type] ||= []
        @callbacks[type] << block
      end

      def start
        EM.run do
          ws = Faye::WebSocket::Client.new(@url, nil, ping: 30)

          ws.on :open do |event|
          end

          ws.on :message do |event|
            data = JSON.parse(event.data)
            if !data["type"].nil? && !@callbacks[data["type"].to_sym].nil?
              @callbacks[data["type"].to_sym].each do |c|
                c.call data
              end
            end
          end

          ws.on :close do |event|
            @callbacks[:close].each { |c| c.call } unless @callbacks[:close].nil?
            EM.stop
          end
        end
      end

      def method_missing(method, *args, &block)
        return super if @response[method.to_s].nil?
        @response[method.to_s]
      end

      # Delegate to Slack::Client
      def respond_to?(method, include_all=false)
        return !@response[method.to_s].nil? || super
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
slack-api-1.6.1 lib/slack/realtime/client.rb
slack-api-1.6.0 lib/slack/realtime/client.rb
slack-api-1.5.0 lib/slack/realtime/client.rb