class SlackExport::Exporter

Attributes

base_path[R]
channel[R]
client[R]
logger[RW]

Public Class Methods

new(api_key, channel, base_path, logger=nil) click to toggle source
# File lib/slack_export/exporter.rb, line 11
def initialize(api_key, channel, base_path, logger=nil)
  @client = SlackClient.new(api_key, channel)
  @channel = channel
  @base_path = base_path
  @logger = logger || self.logger
end

Public Instance Methods

export() click to toggle source
# File lib/slack_export/exporter.rb, line 18
def export
  raise StandardError, "Directory #{base_path} does not exist" unless Dir.exist?(base_path)
  log "Exporting #{channel} to folder #{base_path}"

  # CHANNELS
  channels = client.get_channels.select {|c| c["name"] == channel}
  log "Exporting #{channels.count} channels"
  File.open(channels_path, "w") {|f| f.write(channels.to_json)}

  # MESSAGES
  messages = client.get_messages
  log "Exporting #{messages.count} messages"
  Dir.mkdir(messages_base_path) unless Dir.exist?(messages_base_path)
  File.open(messages_path, "w") do |file|
    file.write(messages.to_json)
  end

  # USERS
  users = client.get_users
  log "Exporting #{users.count} users"
  users = users.select do |u|
    messages.any? {|m| m["user"] == u["id"]}
  end
  File.open(users_path, "w") {|f| f.write(users.to_json)}

  # BUNDLE TO ZIP
  log "Bundling export file to #{export_path}"
  Zip::File.open(export_path, Zip::File::CREATE) do |zip|
    zip.add(users_filename, users_path)
    zip.add(channels_filename, channels_path)
    zip.add(messages_sub_path, messages_path)
  end

  # cleanup
  File.delete(users_path, messages_path, channels_path)
  Dir.delete(messages_base_path)
end
export_path() click to toggle source
# File lib/slack_export/exporter.rb, line 56
def export_path
  File.join(base_path, "#{channel}.zip")
end

Private Instance Methods

channels_filename() click to toggle source
# File lib/slack_export/exporter.rb, line 70
def channels_filename
  "channels.json"
end
channels_path() click to toggle source
# File lib/slack_export/exporter.rb, line 74
def channels_path
  File.join(base_path, channels_filename)
end
log(message) click to toggle source
# File lib/slack_export/exporter.rb, line 94
def log(message)
  @logger.call message if @logger
end
messages_base_path() click to toggle source
# File lib/slack_export/exporter.rb, line 82
def messages_base_path
  File.join(base_path, channel)
end
messages_filename() click to toggle source
# File lib/slack_export/exporter.rb, line 78
def messages_filename
  "messages.json"
end
messages_path() click to toggle source
# File lib/slack_export/exporter.rb, line 90
def messages_path
  File.join(base_path, messages_sub_path)
end
messages_sub_path() click to toggle source
# File lib/slack_export/exporter.rb, line 86
def messages_sub_path
  File.join(channel, messages_filename)
end
users_filename() click to toggle source
# File lib/slack_export/exporter.rb, line 62
def users_filename
  "users.json"
end
users_path() click to toggle source
# File lib/slack_export/exporter.rb, line 66
def users_path
  File.join(base_path, users_filename)
end