Sha256: 2b636faf2ae94722e30554229ddca45e7da2da262fbd28319c11562f39eea147

Contents?: true

Size: 1.62 KB

Versions: 14

Compression:

Stored size: 1.62 KB

Contents

module Slacky
  class Bookkeeper
    def initialize(client)
      @client = client
    end

    def web_client
      @client.web_client
    end

    def keep_the_books
      @client.on :presence_change do |data|
        next unless ( user = User.find data.user )
        user.presence = data['presence']
        user.save
      end

      @client.on :channel_created do |data|
        web_client.channels_info(channel: data.channel.id).tap do |resp|
          if resp.ok
            channel = Channel.channel resp.channel
            puts "Channel ##{channel.name}: channel_created"
          end
        end
      end

      handle_channel(:channel_deleted)   { |c| c.delete }
      handle_channel(:channel_archive)   { |c| c.archive }
      handle_channel(:channel_unarchive) { |c| c.unarchive }
      handle_channel(:channel_rename)    { |c, data| c.rename data.channel.name }

      @client.on :group_joined do |data|
        next unless data.channel.is_group
        channel = Channel.group data.channel
        puts "Channel ##{channel.name}: group_joined"
      end

      handle_channel(:group_left)        { |g| g.delete }
      handle_channel(:group_archive)     { |g| g.archive }
      handle_channel(:group_unarchive)   { |g| g.unarchive }
      handle_channel(:group_rename)      { |g, data| g.rename data.channel.name }
    end

    def handle_channel(event)
      @client.on event do |data|
        channel_id = data.channel.is_a?(String) ? data.channel : data.channel.id
        Channel.find(channel_id).tap do |channel|
          yield channel, data
          puts "Channel ##{channel.name}: #{event}"
        end
      end
    end

  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
slacky-0.2.8 lib/slacky/bookkeeper.rb
slacky-0.2.7 lib/slacky/bookkeeper.rb
slacky-0.2.6 lib/slacky/bookkeeper.rb
slacky-0.2.5 lib/slacky/bookkeeper.rb
slacky-0.2.4 lib/slacky/bookkeeper.rb
slacky-0.2.3 lib/slacky/bookkeeper.rb
slacky-0.2.2 lib/slacky/bookkeeper.rb
slacky-0.2.1 lib/slacky/bookkeeper.rb
slacky-0.2 lib/slacky/bookkeeper.rb
slacky-0.1.4 lib/slacky/bookkeeper.rb
slacky-0.1.3 lib/slacky/bookkeeper.rb
slacky-0.1.2 lib/slacky/bookkeeper.rb
slacky-0.1.1 lib/slacky/bookkeeper.rb
slacky-0.1 lib/slacky/bookkeeper.rb