class Md2site::StatusFile

ダウンロードステータスファイルクラス

Attributes

baseurl[RW]

@return [String] ダウンロード先ホストのURL

fname[RW]

@return [String] ダウンロードヘッダ一覧ファイル名

fpath[RW]

@return [String] ダウンロードヘッダ一覧ファイルへのパス

last_contents_path[RW]

ダウンロード保存先ディレクトリへのパス

last_datetime[RW]

@return [String] ダウンロード開始日時

Public Class Methods

new(path, absolutepath_root, baseurl, mes) click to toggle source

初期化

@param path [String] ダウンロードステータスファイルへのパス @param baseurl [String] ダウンロード先ホストのURL @param mes [Messagex] Messagexクラスのインスタンス

# File lib/md2site/statusfile.rb, line 24
def initialize(path, absolutepath_root, baseurl, mes)
  @path = path
  @absolutepath_root_pn = Pathname.new(absolutepath_root)

  @baseurl = baseurl
  @mes = mes

  mes.add_exitcode("EXIT_CODE_CANNOT_ANALYZE_YAMLFILE")
  mes.add_exitcode("EXIT_CODE_CANNOT_DUMP_TO_YAML")

  content = get_statusfile(@path)

  if content && !content.strip.empty?
    obj = Filex::Filex.load_yaml(content, @mes)
    @baseurl = obj[:baseurl]
    @fname = obj[:fname]
    @fpath = obj[:fpath]
    @absolutepath_fpath_pn = Pathname.new(get_absolute_path(@fpath))
    @last_datetime = obj[:last_datetime]

    @last_contents_path = obj[:last_contents_path]
    @absolutepath_last_contents_path_pn = Pathname.new(get_absolute_path(@last_contents_path))
  else
    @baseurl = @baseurl
    @fname = nil
    @fpath = nil
    @last_datetime = nil
    @last_contents_path = nil
  end
end

Public Instance Methods

get_statusfile(path) click to toggle source

ダウンロードステータスファイルの内容の取得

@param path [String] ダウンロードステータスファイルへのパス @return [String] ダウンロードステータスファイルの内容 @note 引数pathがnilであれば例外発生

# File lib/md2site/statusfile.rb, line 106
def get_statusfile(path)
  unless path
    raise
  end

  content = nil
  if File.exist?(path)
    @mes.exc_file_read(path) { content = File.read(path) }
  end
  content
end
output(path, content) click to toggle source

ダウンロードステータスファイル出力

@param path [String] ダウンロードステータスファイルへのパス @param content [String] ダウンロードステータスファイルの内容 @return [void]

# File lib/md2site/statusfile.rb, line 61
def output(path, content)
  File.open(path, "w") do |ofile|
    ofile.puts(content)
    ofile.flush
  end
end
print() click to toggle source

ダウンロードステータスファイルの内容の出力(デバッグ用)

@return [void]

update() click to toggle source

ダウンロードステータスファイル更新

@return [void] @note YAML形式に変換できなければダウンロードステータスファイルを更新しない

# File lib/md2site/statusfile.rb, line 73
def update
  if @fpath
    @absolutepath_fpath_pn = Pathname.new(get_absolute_path(@fpath))
    @fpath = @absolutepath_fpath_pn.relative_path_from(@absolutepath_root_pn).to_s
  else
    @fpath = ""
  end

  if @last_contents_path
    @absolutepath_last_contents_path_pn = Pathname.new(get_absolute_path(@last_contents_path))
    @last_contents_path = @absolutepath_last_contents_path_pn.relative_path_from(@absolutepath_root_pn).to_s
  else
    @last_contents_path = ""
  end
  hs = { baseurl: @baseurl, fname: @fname, fpath: @fpath, last_datetime: @last_datetime, last_contents_path: @last_contents_path }
  begin
    content = YAML.dump(hs)
  rescue Error => e
    @mes.output_exception(e)
    exit(@mes.ec("EXIT_CODE_CANNOT_DUMP_TO_YAML"))
  end

  return unless content

  @mes.exc_file_write(@path) { output(@path, content) }
end

Private Instance Methods

get_absolute_path(path) click to toggle source

プロジェクトのルートディレクトリと相対パスを合わせた絶対パスを得る

@param path [String] 相対パス @return [String] プロジェクトのルートディレクトリと相対パスを合わせた絶対パスを得る

# File lib/md2site/statusfile.rb, line 154
def get_absolute_path(path)
  if path
    path_pn = Pathname.new(path)
    if path_pn.absolute?
      path_pn.to_s
    else
      (@absolutepath_root_pn + path_pn).to_s
    end
  else
    ""
  end
end
get_relative_path(path) click to toggle source

プロジェクトのルートディレクトリに対する相対パス

@param path [String] 相対パスにしたいパス @return [String] プロジェクトのルートディレクトリに対する相対パス

# File lib/md2site/statusfile.rb, line 137
def get_relative_path(path)
  if path
    to_path_pn = Pathname.new(path)
    unless to_path_pn.absolute?
      to_path_pn = to_path_pn.expand_path
    end
  else
    to_path_pn = Pathname.new("")
  end
  to_path_pn.relative_path_from(@absolutepath_fpath_pn).to_s
end