Sha256: dd5d496bda3106184d9e371378244c15f200a187286a687ffd1c36b588d285f7

Contents?: true

Size: 1.29 KB

Versions: 2

Compression:

Stored size: 1.29 KB

Contents

module Methods
  extend ActiveSupport::Concern

  included do
    attr_accessor :server # server at runtime

    SORT_ORDERS = ['created_at', '-created_at', 'updated_at', '-updated_at'].freeze

    scope :active, -> { where(active: true) }

    validates_uniqueness_of :token, message: 'has already been used'
    validates_presence_of :token
    validates_presence_of :team_id

    def deactivate!
      update!(active: false)
    end

    def activate!(token)
      update!(active: true, token: token)
    end

    def to_s
      {
        name: name,
        domain: domain,
        id: team_id
      }.map do |k, v|
        "#{k}=#{v}" if v
      end.compact.join(', ')
    end

    def ping!
      client = Slack::Web::Client.new(token: token)

      auth = client.auth_test

      presence = begin
        client.users_getPresence(user: auth['user_id'])
                 rescue Slack::Web::Api::Errors::MissingScope
                   nil
      end

      {
        auth: auth,
        presence: presence
      }
    end

    def ping_if_active!
      return unless active?

      ping!
    rescue Slack::Web::Api::Errors::SlackError => e
      logger.warn "Active team #{self} ping, #{e.message}."
      case e.message
      when 'account_inactive', 'invalid_auth'
        deactivate!
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
slack-ruby-bot-server-2.1.1 lib/slack-ruby-bot-server/models/team/methods.rb
slack-ruby-bot-server-2.1.0 lib/slack-ruby-bot-server/models/team/methods.rb