Sha256: 7ed62dc33955cda5701f6d523e73efbe40757b0c886e9a131f24553b1168bf33

Contents?: true

Size: 1.18 KB

Versions: 2

Compression:

Stored size: 1.18 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_attributes!(active: false)
    end

    def activate!(token)
      update_attributes!(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
      {
        auth: auth,
        presence: client.users_getPresence(user: auth['user_id'])
      }
    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-1.0.0 lib/slack-ruby-bot-server/models/team/methods.rb
slack-ruby-bot-server-0.12.3 lib/slack-ruby-bot-server/models/team/methods.rb