Sha256: 58c8832e377d4aca7ebf42bf8150dd2e59cc27d00487fd2a06d5b99ad2b187b4

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

require 'slack'
require 'slappy/event'
require 'slappy/listener'
require 'slappy/configuration'

module Slappy
  class Client
    def initialize
      Slack.configure { |slack| slack.token = config.token }
      @listeners = {}
    end

    def client
      @client ||= Slack.realtime
    end

    def start
      @listeners.each do |key, array|
        client.on key do |data|
          array.each do |listener|
            event = Event.new(data, listener.pattern) if key == :message
            listener.call(event)
          end
        end
      end
      client.start
    end

    def hello(&block)
      @listeners[:hello] ||= []
      @listeners[:hello].push block
    end

    def hear(regexp, &block)
      @listeners[:message] ||= []
      @listeners[:message].push Listener.new(regexp, block)
    end

    def say(text, options = {})
      options[:text] = text
      params = merge_send_params options
      Slack.chat_postMessage params
    end

    private

    def config
      Slappy.configuration
    end

    def merge_send_params(options)
      default = config.send_params
      default.merge options
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
slappy-0.2.0 lib/slappy/client.rb
slappy-0.1.1 lib/slappy/client.rb
slappy-0.1.0 lib/slappy/client.rb