class Md2site::StatusFile
ダウンロードステータスファイルクラス
Attributes
@return [String] ダウンロード先ホストのURL
@return [String] ダウンロードヘッダ一覧ファイル名
@return [String] ダウンロードヘッダ一覧ファイルへのパス
ダウンロード保存先ディレクトリへのパス
@return [String] ダウンロード開始日時
Public Class Methods
初期化
@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
ダウンロードステータスファイルの内容の取得
@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
ダウンロードステータスファイル出力
@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
ダウンロードステータスファイルの内容の出力(デバッグ用)
@return [void]
# File lib/md2site/statusfile.rb, line 122 def print @mes.output_info("baseurl=#{@baseurl}") @mes.output_info("fname=#{@fname}") @mes.output_info("fpath=#{@fpath}") @mes.output_info("last_datetime=#{@last_datetime}") @mes.output_info("last_contents_path=#{@last_contents_path}") end
ダウンロードステータスファイル更新
@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
プロジェクトのルートディレクトリと相対パスを合わせた絶対パスを得る
@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
プロジェクトのルートディレクトリに対する相対パス
@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