Class: Socrates::Adapters::Slack

Inherits:
Object
  • Object
show all
Includes:
Adapter
Defined in:
lib/socrates/adapters/slack.rb

Instance Method Summary collapse

Methods included from Adapter

#flush_session, #lookup_user, #queue_direct_message, #queue_message

Constructor Details

#initialize(real_time_client) ⇒ Slack

Returns a new instance of Slack.



9
10
11
# File 'lib/socrates/adapters/slack.rb', line 9

def initialize(real_time_client)
  @real_time_client = real_time_client
end

Instance Method Details

#channel_from(context: nil, user: nil) ⇒ Object

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
34
35
36
# File 'lib/socrates/adapters/slack.rb', line 27

def channel_from(context: nil, user: nil)
  unless context.nil?
    raise ArgumentError, "Expected context to respond to :channel" unless context.respond_to?(:channel)

    return context.channel
  end
  return lookup_dm_channel(user) unless user.nil?

  raise ArgumentError, "Must provide one of context or user"
end

#client_id_from(context: nil, user: nil) ⇒ Object

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/socrates/adapters/slack.rb', line 13

def client_id_from(context: nil, user: nil)
  unless context.nil?
    raise ArgumentError, "Expected context to respond to :user" unless context.respond_to?(:user)

    return context.user
  end
  unless user.nil?
    raise ArgumentError, "Expected user to respond to :id" unless user.respond_to?(:id)

    return user.id
  end
  raise ArgumentError, "Must provide one of context or user"
end

#lookup_email(context:) ⇒ Object

Raises:

  • (ArgumentError)


56
57
58
59
60
61
62
# File 'lib/socrates/adapters/slack.rb', line 56

def lookup_email(context:)
  raise ArgumentError, "Expected context to respond to :user" unless context.respond_to?(:user)

  client = @real_time_client.web_client
  info   = client.users_info(user: context.user)
  info.present? ? info.user.profile.email.presence : nil
end

#user_from(context:) ⇒ Object

Raises:

  • (ArgumentError)


47
48
49
50
51
52
53
54
# File 'lib/socrates/adapters/slack.rb', line 47

def user_from(context:)
  raise ArgumentError, "context cannot be nil" if context.nil?
  raise ArgumentError, "Expected context to respond to :user" unless context.respond_to?(:user)

  client = @real_time_client.web_client
  info   = client.users_info(user: context.user)
  info.present? ? info.user : nil
end

#users(include_deleted: false, include_bots: false) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/socrates/adapters/slack.rb', line 38

def users(include_deleted: false, include_bots: false)
  client = @real_time_client.web_client

  client.users_list.tap { |response|
    response.members.reject!(&:deleted?) unless include_deleted
    response.members.reject!(&:is_bot?) unless include_bots
  }.members
end