Class: SlackProfile::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/slack_profile/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(token) ⇒ Client

Returns a new instance of Client.



9
10
11
12
13
14
15
# File 'lib/slack_profile/client.rb', line 9

def initialize(token)
  @token = token
  @headers = {
    "Authorization" => "Bearer #{@token}",
    "Content-Type" => "application/json"
  }
end

Instance Method Details

#get_profile(user_id) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/slack_profile/client.rb', line 48

def get_profile(user_id)
  response = self.class.get("/users.profile.get",
    headers: @headers,
    query: {
      user: user_id,
      include_labels: true
    }
  )

  result = handle_response(response)
  result["profile"]
end

#get_team_profile ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/slack_profile/client.rb', line 61

def get_team_profile
  response = self.class.get("/team.profile.get",
    headers: @headers
  )

  result = handle_response(response)
  result.dig("profile", "fields") || {}
end

#set_profile(user_id, profile) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/slack_profile/client.rb', line 17

def set_profile(user_id, profile)
  response = self.class.post("/users.profile.set",
    headers: @headers,
    body: {
      user: user_id,
      profile: profile.to_json
    }.to_json
  )

  handle_response(response)
end

#set_single_field(user_id, field_name, value) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/slack_profile/client.rb', line 29

def set_single_field(user_id, field_name, value)
  profile = if field_name.start_with?("X")
    # Custom field
    {
      fields: {
        field_name => {
          value: value,
          alt: ""
        }
      }
    }
  else
    # Standard field
    { field_name => value }
  end

  set_profile(user_id, profile)
end