class Daimyo::Export

Public Class Methods

new() click to toggle source
# File lib/daimyo/export.rb, line 5
def initialize
  @wiki ||= Daimyo::Client.new
end

Public Instance Methods

run(project_id, wiki_id = nil) click to toggle source
# File lib/daimyo/export.rb, line 9
def run(project_id, wiki_id = nil)
  ids = select_wiki_ids(project_id)
  pb = ProgressBar.create(:format => "%a %b\u{15E7}%i %p%% %t",
                          :progress_mark => ' ',
                          :remainder_mark => "\u{FF65}",
                          :starting_at => 10,
                          :length => 50)
  ids.each do |id|
    wiki = @wiki.export(id).body
    name = wiki.name
    content = wiki.content
    write_file(project_id, id, name, content)
    pb.increment
    sleep 0.1
  end
  pb.finish
end

Private Instance Methods

create_wiki_directory(path) click to toggle source
# File lib/daimyo/export.rb, line 48
def create_wiki_directory(path)
  FileUtils.mkdir_p(path) unless FileTest.exist?(path)
  path
end
define_directory_path(project_id, name) click to toggle source
# File lib/daimyo/export.rb, line 53
def define_directory_path(project_id, name)
  space = @wiki.instance_variable_get(:@client).instance_variable_get(:@space_id)
  return space + '/' + project_id unless name.include?('/')
  space + '/' + project_id + '/' + File.dirname(name) # 最後の 1 要素をファイル名とする
end
define_file_path(name) click to toggle source
# File lib/daimyo/export.rb, line 59
def define_file_path(name)
  return name unless name.include?('/')
  File.basename(name) # 最後の 1 要素をファイル名とする
end
select_wiki_ids(project_id) click to toggle source
# File lib/daimyo/export.rb, line 29
def select_wiki_ids(project_id)
  @wiki.list(project_id).body.map { |w| w.id }
end
write_file(project_id, id, name, content) click to toggle source
# File lib/daimyo/export.rb, line 33
def write_file(project_id, id, name, content)
  path = define_directory_path(project_id, name)
  create_wiki_directory(path)
  filename = id.to_s + '_' + define_file_path(name) + '.md'
  file_path = path + '/' + filename
  File.open(file_path, 'w') do |f|
    f.puts(content.gsub("\r\n", "\n"))
  end

  original_file_path = path + '/' + '.' + filename
  File.open(original_file_path, 'w') do |f|
    f.puts(content.gsub("\r\n", "\n"))
  end
end