Ruby - リンクチェック - openuri.rb 外部ドキュメントのリカバリ

 クラウディア
1. 外部ドキュメントのリカバリ

1. 外部ドキュメントのリカバリ

 「fetch.rb」で

html = open(uri).read
 のみであるとエラーになるケースが多々あるので、「Net::HTTP」でパラメータも分けて取得するようにしました。
=begin

  open(uri).read ではエラーになるものが多いので、細かくしてみる
  本クラスへの引数の URI は encode・parse 済
  例外は上位にキャッチさせる

=end

require('pry')
require('uri')
require('open-uri')
require('searchName')
require('exceptlog.rb')

class Openuri
  def initialize()
  end

  def execute(uri)
    http = Net::HTTP::new(uri.host, uri.port)

    if (uri.port == 443)
      http.use_ssl = true
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    end

    hash = Hash.new

    ARGV[1..-1].each do | param |
      array = param.split('=')
      hash[array[0]] = array[1]
    end

    if (hash.size <= 0)
      get = Net::HTTP::Get::new(uri.path)

      response = http.start do | value |
        value.request(get)
      end
    else
      post = Net::HTTP::Post::new(uri.path)

      post.set_form_data(hash, ';')

      response = http.start do | value |
        value.request(post)
      end
    end

    return response.body;
  end
end