Class: SlackExport::Exporter

Inherits:
Object
  • Object
show all
Defined in:
lib/slack_export/exporter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, channel, base_path, logger = nil) ⇒ Exporter

Returns a new instance of Exporter.



11
12
13
14
15
16
# 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
  self.logger = logger
end

Instance Attribute Details

#base_pathObject (readonly)

Returns the value of attribute base_path.



8
9
10
# File 'lib/slack_export/exporter.rb', line 8

def base_path
  @base_path
end

#channelObject (readonly)

Returns the value of attribute channel.



8
9
10
# File 'lib/slack_export/exporter.rb', line 8

def channel
  @channel
end

#clientObject (readonly)

Returns the value of attribute client.



8
9
10
# File 'lib/slack_export/exporter.rb', line 8

def client
  @client
end

#loggerObject

Returns the value of attribute logger.



9
10
11
# File 'lib/slack_export/exporter.rb', line 9

def logger
  @logger
end

Instance Method Details

#export(new_channel_name = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/slack_export/exporter.rb', line 18

def export(new_channel_name=nil)
  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
end

#export_pathObject



52
53
54
# File 'lib/slack_export/exporter.rb', line 52

def export_path
  File.join(base_path, "#{channel}.zip")
end