Module: Motor::Slack::Client

Defined in:
lib/motor/slack/client.rb

Constant Summary collapse

BASE_API_URL =
'https://slack.com/api'
POST_MESSAGE_ENPOINT =
"#{BASE_API_URL}/chat.postMessage"
LOAD_CONVERSATIONS_ENPOINT =
"#{BASE_API_URL}/conversations.list"
LOAD_USERS_ENPOINT =
"#{BASE_API_URL}/users.list"
SEND_FILE_ENPOINT =
"#{BASE_API_URL}/files.upload"
ApiError =
Class.new(StandardError)

Class Method Summary collapse

Class Method Details

.auth_tokenObject



51
52
53
54
55
56
57
58
59
# File 'lib/motor/slack/client.rb', line 51

def auth_token
  return ENV['SLACK_AUTH_TOKEN'] unless defined?(Motor::EncryptedConfig)

  config = Motor::EncryptedConfig.find_by(key: EncryptedConfig::SLACK_CREDENTIALS_KEY)

  return '' unless config

  config.value[:api_key]
end

.load_conversations(params = {}) ⇒ Object



31
32
33
34
35
# File 'lib/motor/slack/client.rb', line 31

def load_conversations(params = {})
  resp = Motor::NetHttpUtils.get(LOAD_CONVERSATIONS_ENPOINT, params.merge(token: auth_token))

  parse_json_response_or_throw_error(resp)
end

.load_users(params = {}) ⇒ Object



37
38
39
40
41
# File 'lib/motor/slack/client.rb', line 37

def load_users(params = {})
  resp = Motor::NetHttpUtils.get(LOAD_USERS_ENPOINT, params.merge(token: auth_token))

  parse_json_response_or_throw_error(resp)
end

.parse_json_response_or_throw_error(resp) ⇒ Object

Raises:



43
44
45
46
47
48
49
# File 'lib/motor/slack/client.rb', line 43

def parse_json_response_or_throw_error(resp)
  data = JSON.parse(resp.body)

  raise ApiError, resp.body unless data['ok']

  data
end

.send_file(params = {}) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/motor/slack/client.rb', line 22

def send_file(params = {})
  content = params.delete(:content)
  body = { content: content }.to_query

  resp = Motor::NetHttpUtils.post(SEND_FILE_ENPOINT, params.merge(token: auth_token), {}, body)

  parse_json_response_or_throw_error(resp)
end

.send_message(params = {}) ⇒ Object



16
17
18
19
20
# File 'lib/motor/slack/client.rb', line 16

def send_message(params = {})
  resp = Motor::NetHttpUtils.post(POST_MESSAGE_ENPOINT, params.merge(token: auth_token))

  parse_json_response_or_throw_error(resp)
end