class Lita::Adapters::Slack::API

@api private

Attributes

config[R]
post_message_config[R]
stubs[R]

Public Class Methods

new(config, stubs = nil) click to toggle source
# File lib/lita/adapters/slack/api.rb, line 13
def initialize(config, stubs = nil)
  @config = config
  @stubs = stubs
  @post_message_config = {}
  @post_message_config[:parse] = config.parse unless config.parse.nil?
  @post_message_config[:link_names] = config.link_names ? 1 : 0 unless config.link_names.nil?
  @post_message_config[:unfurl_links] = config.unfurl_links unless config.unfurl_links.nil?
  @post_message_config[:unfurl_media] = config.unfurl_media unless config.unfurl_media.nil?
end

Public Instance Methods

channels_info(channel_id) click to toggle source
# File lib/lita/adapters/slack/api.rb, line 29
def channels_info(channel_id)
  call_api("channels.info", channel: channel_id)
end
channels_list() click to toggle source
# File lib/lita/adapters/slack/api.rb, line 33
def channels_list
  call_api("channels.list")
end
groups_list() click to toggle source
# File lib/lita/adapters/slack/api.rb, line 37
def groups_list
  call_api("groups.list")
end
im_list() click to toggle source
# File lib/lita/adapters/slack/api.rb, line 45
def im_list
  call_api("im.list")
end
im_open(user_id) click to toggle source
# File lib/lita/adapters/slack/api.rb, line 23
def im_open(user_id)
  response_data = call_api("im.open", user: user_id)

  SlackIM.new(response_data["channel"]["id"], user_id)
end
mpim_list() click to toggle source
# File lib/lita/adapters/slack/api.rb, line 41
def mpim_list
  call_api("mpim.list")
end
rtm_start() click to toggle source
# File lib/lita/adapters/slack/api.rb, line 72
def rtm_start
  response_data = call_api("rtm.start")

  TeamData.new(
    SlackIM.from_data_array(response_data["ims"]),
    SlackUser.from_data(response_data["self"]),
    SlackUser.from_data_array(response_data["users"]),
    SlackChannel.from_data_array(response_data["channels"]) +
      SlackChannel.from_data_array(response_data["groups"]),
    response_data["url"],
  )
end
send_attachments(room_or_user, attachments) click to toggle source
# File lib/lita/adapters/slack/api.rb, line 49
def send_attachments(room_or_user, attachments)
  call_api(
    "chat.postMessage",
    as_user: true,
    channel: room_or_user.id,
    attachments: MultiJson.dump(attachments.map(&:to_hash)),
  )
end
send_messages(channel_id, messages) click to toggle source
# File lib/lita/adapters/slack/api.rb, line 58
def send_messages(channel_id, messages)
  call_api(
    "chat.postMessage",
    **post_message_config,
    as_user: true,
    channel: channel_id,
    text: messages.join("\n"),
  )
end
set_topic(channel, topic) click to toggle source
# File lib/lita/adapters/slack/api.rb, line 68
def set_topic(channel, topic)
  call_api("channels.setTopic", channel: channel, topic: topic)
end

Private Instance Methods

call_api(method, post_data = {}) click to toggle source
# File lib/lita/adapters/slack/api.rb, line 91
def call_api(method, post_data = {})
  response = connection.post(
    "https://slack.com/api/#{method}",
    { token: config.token }.merge(post_data)
  )

  data = parse_response(response, method)

  raise "Slack API call to #{method} returned an error: #{data["error"]}." if data["error"]

  data
end
connection() click to toggle source
# File lib/lita/adapters/slack/api.rb, line 104
def connection
  if stubs
    Faraday.new { |faraday| faraday.adapter(:test, stubs) }
  else
    options = {}
    unless config.proxy.nil?
      options = { proxy: config.proxy }
    end
    Faraday.new(options)
  end
end
parse_response(response, method) click to toggle source
# File lib/lita/adapters/slack/api.rb, line 116
def parse_response(response, method)
  unless response.success?
    raise "Slack API call to #{method} failed with status code #{response.status}: '#{response.body}'. Headers: #{response.headers}"
  end

  MultiJson.load(response.body)
end