Sha256: 1381ddf6827802b635a88dc4c8d4f594cb863dad0848011fc75dbb815cbe4ed4

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

require 'slack-api-wrapper'
require 'logger'
require 'date'

module Slackpolice
  class Client
    def initialize(cli_options = {})
      @cli_options = cli_options
      @logger ||= Logger.new STDOUT

      @client = Slack::Client.new @cli_options[:api_token]
      @client.auth_test
    end

    def archive
      archives = []
      no_members_channels.each do |c|
        resp = @client.channels_archive('channel' => c['id'])
        archives << c if resp['ok']
      end
      archives
    end

    def delete_expired_files(ts_to = Date.today.prev_day(7))
      Enumerator.new do |y|
        expired_files(ts_to).each do |f|
          resp = @client.files_delete('file' => f['id'])
          y << f if resp['ok']
        end
      end
    end

    def no_members_channels
      @client.channels_list['channels'].select do |c|
        c['num_members'] == 0 && c['is_archived'] == false
      end
    end

    def expired_files(ts_to = Date.today.prev_day(7))
      page = 1
      Enumerator.new do |y|
        begin
          resp = @client.files_list('ts_to' => ts_to.to_time.to_i, 'page' => page)
          if resp['ok']
            pages = resp['paging']['pages']
            resp['files'].map do |file|
              y << file
            end
          end
          page += 1
        end while pages >= page && 100 > page # over 100 page is not found. api bug?
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
slackpolice-0.2.0 lib/slackpolice/client.rb