class SlackScratcher::Loader::File

Loader class for importing slack log data from exported data.

@since 0.0.1

Public Class Methods

new(target_dir) click to toggle source

Initialize SlackScratcher::Loader::File object.

@param [String] target_dir Directory which unarchived log data

@example Create File loader obect

target_dir = '~/tmp/my_slack-2015-03-16/'
SlackScratcher::Loader::File.new target_dir

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

# File lib/slack_scratcher/loader/file.rb, line 18
def initialize(target_dir)
  fail ArgumentError unless ::File.directory?(target_dir)

  @target = target_dir
  @users = users
  @channels = channels
end

Public Instance Methods

each(_ = nil) { |parse_log_file(file), file| ... } click to toggle source

Iterate all log data which is parsed from log file in log directory

@param [NilClass] _ Not use

@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/file.rb, line 34
def each(_ = nil)
  files.each do |file|
    yield parse_log_file(file), file
  end

  true
end

Private Instance Methods

channel_dirs() click to toggle source

@private

# File lib/slack_scratcher/loader/file.rb, line 88
def channel_dirs
  Dir["#{@target}/*/"]
end
channel_info(log_file) click to toggle source

@private

# File lib/slack_scratcher/loader/file.rb, line 68
def channel_info(log_file)
  name = get_channel_dir(log_file)
  { name: name, id: @channels[name]['id'] }
end
channels() click to toggle source

@private

# File lib/slack_scratcher/loader/file.rb, line 55
def channels
  load "#{@target}/channels.json", 'name'
end
files() click to toggle source

@private

# File lib/slack_scratcher/loader/file.rb, line 81
def files
  channel_dirs.inject([]) do |arr, channel|
    arr + log_files(channel)
  end
end
get_channel_dir(path) click to toggle source

@private

# File lib/slack_scratcher/loader/file.rb, line 45
def get_channel_dir(path)
  ::File.dirname(path).split('/').last
end
load(target, index_column) click to toggle source

@private

# File lib/slack_scratcher/loader/file.rb, line 60
def load(target, index_column)
  fail SlackScratcher::Error::FileNotFound unless ::File.exist? target

  channels = Oj.load(::File.read(target))
  SlackScratcher::Helper.index_data channels, index_column
end
log_files(channel) click to toggle source

@private

# File lib/slack_scratcher/loader/file.rb, line 93
def log_files(channel)
  Dir["#{channel}*.json"]
end
parse_log_file(log_file) click to toggle source

@private

# File lib/slack_scratcher/loader/file.rb, line 74
def parse_log_file(log_file)
  channel = channel_info(log_file)
  logs = Oj.load(::File.read(log_file))
  SlackScratcher::Model::Chats.new(logs, channel, @users).refined_data
end
users() click to toggle source

@private

# File lib/slack_scratcher/loader/file.rb, line 50
def users
  load "#{@target}/users.json", 'id'
end