class SlackScratcher::Loader::Api

Loader class for importing latest slack logs through Slack API

@since 0.1

Constants

WAIT_TIME

Duration of waiting between API call

Public Class Methods

new(token = nil) click to toggle source

Initialize SlackScratcher::Loader::Api object.

@see api.slack.com/web

@param [String] token Slack API token. It also can be set by using

ENV['SLACK_TOKEN']

@example Create File loader obect

token = '123456789'
SlackScratcher::Loader::Api.new token

@return [SlackScratcher::Loader::Api] Api loader object

# File lib/slack_scratcher/loader/api.rb, line 24
def initialize(token = nil)
  authenticate_slack(token)
end

Public Instance Methods

each(adapter) { |parse_log(channel, from), channel| ... } click to toggle source

Iterate all log data which is parsed from Slack API.

@param [SlackScratcher::Adapter::Base] adapter Datastore adapter

for getting when to starting fetch log

@example Iterate all logs for priniting contents

loader.each { |data| puts data }

return [Boolean] If there isn't any ploblem, it returns true

# File lib/slack_scratcher/loader/api.rb, line 37
def each(adapter)
  @users || set_users

  active_channels.each do |channel|
    from = adapter.timestamp_of_last_channel_log(channel[:name])
    yield parse_log(channel, from), channel
  end

  true
end

Private Instance Methods

active_channels() click to toggle source

@private

# File lib/slack_scratcher/loader/api.rb, line 87
def active_channels
  wait

  response = validate_response(Slack.channels_list)
  index_channels filter_active_channels(response['channels'])
end
authenticate_slack(token) click to toggle source

@private

# File lib/slack_scratcher/loader/api.rb, line 74
def authenticate_slack(token)
  token ||= ENV['SLACK_TOKEN']
  fail SlackScratcher::Error::TokenNotSet unless token
  Slack.configure { |config| config.token = token }
end
channel_history(channel_id, from, to = Time.now) click to toggle source

@private

# File lib/slack_scratcher/loader/api.rb, line 113
def channel_history(channel_id, from, to = Time.now)
  wait

  attrs = {
    channel: channel_id,
    oldest: from,
    latest: to,
    count: 1000
  }

  validate_response Slack.channels_history(attrs), :messages
end
channels() click to toggle source

@private

# File lib/slack_scratcher/loader/api.rb, line 81
def channels
  response = validate_response(Slack.channels_list)
  index_channels response['channels']
end
check_users(logs) click to toggle source

@private

# File lib/slack_scratcher/loader/api.rb, line 57
def check_users(logs)
  logs
    .select { |log| log.key? 'user' }
    .map { |log| log['user'] }
    .any? { |user| @users[user].nil? }
end
filter_active_channels(data) click to toggle source

@private

# File lib/slack_scratcher/loader/api.rb, line 95
def filter_active_channels(data)
  data.select { |channel| channel['is_archived'] == false }
end
index_channels(data) click to toggle source

@private

# File lib/slack_scratcher/loader/api.rb, line 100
def index_channels(data)
  data.map { |channel| { id: channel['id'], name: channel['name'] } }
end
parse_log(channel, from) click to toggle source

@private

# File lib/slack_scratcher/loader/api.rb, line 65
def parse_log(channel, from)
  logs = channel_history(channel[:id], from)
  if check_users(logs)
    set_users
  end
  SlackScratcher::Model::Chats.new(logs, channel, @users).refined_data
end
set_users() click to toggle source

@private

# File lib/slack_scratcher/loader/api.rb, line 51
def set_users
  @users = users
  SlackScratcher.logger.info "* Users list refreshed"
end
users() click to toggle source

@private

# File lib/slack_scratcher/loader/api.rb, line 105
def users
  wait

  user_list = validate_response(Slack.users_list, :members)
  SlackScratcher::Helper.index_data user_list, 'id'
end
validate_response(response, key = nil) click to toggle source

@private

# File lib/slack_scratcher/loader/api.rb, line 127
def validate_response(response, key = nil)
  fail SlackScratcher::Error::ApiError unless response['ok'] == true
  return response unless key
  response[key.to_s]
end
wait() click to toggle source

@private

# File lib/slack_scratcher/loader/api.rb, line 134
def wait
  sleep WAIT_TIME
end