Ruby - リンクチェック - classific.rb リンク先分類

クラウディア 
1. リンク先分類

1. リンク先分類

=begin

リンクを分類してチェックする

=end

require('pry')
require('assemble')
require('searchName')
require('fetchBefore')
require('exceptlog.rb')

class Classific
  def initialize()
  end

  def execute(parent, top, doc, index, child, tree, checked)

    search   = SearchName.new
    assemble = Assemble.new
    fetchBef = FetchBefore.new

    child.each_key do | key |

      full = key
      kind = "                 "
      res  = '未チェック'
      link = nil                                          # Fetch 呼出し時のリンク先

      begin
        case key[0]
        when '#'                                          # ページ内リンクなので html 内に id=key が存在すること
          kind = "ページ内リンク   "
          res = search.execute(parent, doc, key)
          full = parent + key
        when 'h'                                          # 外部リンクなので存在の有無のみ
          kind = "外部    リンク   "
          link = key
        when '.'                                          # 内部リンクなのでたどる
          kind = "内部  . リンク   "
          link = assemble.execute(index, key)
        else
          if ((key[-3, 3] == 'png') ||                    # イメージなので存在の有無のみ
              (key[-3, 3] == 'gif'))
            kind = "イメージファイル "
            link = key
          elsif ((key[-4, 4] == 'html') ||                # 内部リンクなのでたどる
                 (key[-1, 1] == '/'))
            kind = "内部    リンク   "
            link = key
          else                                            # 上記以外なのでチェックしない
            kind = "その他のもの     "
          end
        end

        if (link)                                         # リンク先をたどる場合
          $nocheck.each do | uncheck |                    # 対象外でないかを確認する
            if (link[0, uncheck.length] == uncheck)       # 対象外であれば
              res  = 'nocheck'                            # nocheck を設定
              full = link
              checked[full] = res
              link = nil
              break
            end
          end
        end

        if (link)                                         # リンク先を取得できるかを試してみる

#          if (link == "https://freebsd.sing.ne.jp/desktop/02/11.html")
#            binding.pry
#          end

          res, full = fetchBef.execute(parent, top, link, checked)

#          if (res == 'SELF')
#            $selfref.push(full)
#          end
        end

        child[key] = { :リンク元 => parent, :種別 => kind, :リンク => key, :結果 => res, :URI => full }
        $result[full] = { :リンク元 => parent, :種別 => kind, :リンク => key, :結果 => res }
#        if (res == 'SELF')
#          puts("#{full} #{$result[full]}");
#        end
      rescue => error
        exeptlog = ExceptLog.new
        binding.pry
        exeptlog.execute(self, error)
        exit 1
      end
    end
  end
end

 29~52行でリンク先を分類しています。  54~64行でリンク先が対象外かチェックして対象外であれば取得しないものとします。  66~69行でリンク先を実際に取得するクラスへ URI を渡します。