Module: Slack::Web::Api::Mixins::Users

Included in:
Endpoints
Defined in:
lib/slack/web/api/mixins/users.id.rb,
lib/slack/web/api/mixins/users.search.rb

Defined Under Namespace

Classes: Member

Instance Method Summary collapse

Instance Method Details

#users_id(options = {}) ⇒ Object

This method returns a user ID given a user name.

Options Hash (options):

  • :user (user)

    User to get ID for, prefixed with '@'.


11
12
13
14
15
16
17
18
19
20
21
# File 'lib/slack/web/api/mixins/users.id.rb', line 11

def users_id(options = {})
  name = options[:user]
  throw ArgumentError.new('Required arguments :user missing') if name.nil?
  return { 'ok' => true, 'user' => { 'id' => name } } unless name[0] == '@'
  users_list.tap do |list|
    list.members.each do |user|
      return Slack::Messages::Message.new('ok' => true, 'user' => { 'id' => user.id }) if user.name == name[1..-1]
    end
  end
  fail Slack::Web::Api::Error, 'user_not_found'
end

#users_search(options = {}) ⇒ Object

This method searches for users.

Options Hash (options):

  • :user (user)

    Free-formed text to search for.


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/slack/web/api/mixins/users.search.rb', line 13

def users_search(options = {})
  query = options[:user]
  throw ArgumentError.new('Required arguments :user missing') if query.nil?
  index = Picky::Index.new(:users) do
    category :name
    category :first_name
    category :last_name
    category :real_name
    category :email
  end
  members = users_list.members
  members.each_with_index do |member, id|
    user = Member.new(
      id,
      member.name,
      member.profile.first_name,
      member.profile.last_name,
      member.profile.real_name,
      member.profile.email
    )
    index.add(user)
  end
  ids = Picky::Search.new(index).search(query, 5, 0, unique: true).ids
  results = ids.map { |id| members[id] }
  Slack::Messages::Message.new('ok' => true, 'members' => results)
end