module PatchFinder::Helper

Attributes

verbose[RW]

Public Instance Methods

download_file(uri, dest_dir) click to toggle source

Downloads a file to a local directory. @note When the file is saved, the file name will actually include a timestamp

in this format: [original filename]_[timestamp].[ext] to avoid
name collision.

@param uri [String] URI (to download) @param dest_dir [String] The folder to save the file.

Make sure this folder exists.

@return [void]

# File lib/patch_finder/core/helper.rb, line 102
def download_file(uri, dest_dir)
  begin
    u = URI.parse(uri)
    fname, ext = File.basename(u.path).scan(/(.+)\.(.+)/).flatten
    dest_file = File.join(dest_dir, "#{fname}_#{Time.now.to_i}.#{ext}")
    res = send_http_get_request(uri)
  rescue Net::ReadTimeout, IOError, EOFError, Errno::ECONNRESET,
         Errno::ECONNABORTED, Errno::EPIPE, Net::OpenTimeout,
         Errno::ETIMEDOUT => e
    print_error("#{e.message}: #{uri}")
    return
  end

  save_file(res.body, dest_file)
  print_status("Download completed for #{uri}")
end
download_files(files, dest_dir) click to toggle source

Downloads multiple files to a local directory. @note 3 clients are used to download all the links.

@param files [Array] Full URIs. @param dest_dir [String] The folder to save the files.

Make sure this folder exists.

@return pvoid

# File lib/patch_finder/core/helper.rb, line 126
def download_files(files, dest_dir)
  pool = PatchFinder::ThreadPool.new(3)

  files.each do |f|
    pool.schedule do
      download_file(f, dest_dir)
    end
  end

  pool.shutdown

  sleep(0.5) until pool.eop?
end
print_error(msg = '') click to toggle source

Prints an error message.

@return [void]

print_line(msg = '') click to toggle source

Prints a message.

@return [void]

print_status(msg = '') click to toggle source

Prints a status message.

@return [void]

print_verbose(msg = '') click to toggle source

Prints a message if verbose is set

@return [void]

print_verbose_error(msg='') click to toggle source
read_file(file_path) click to toggle source

Returns the content of a file.

@param file_path [String] File to read. @return [String]

# File lib/patch_finder/core/helper.rb, line 81
def read_file(file_path)
  return nil unless File.exist?(file_path)

  buf = ''

  File.open(file_path, 'rb') do |f|
    buf = f.read
  end

  buf
end
send_http_get_request(uri, ssl = false) click to toggle source

Sends an HTTP request. @note If the request fails, it will try 3 times before

passing/raising an exception.

@param uri [String] URI. @param ssl [Boolean] Forces SSL option. @return [Net::HTTPResponse]

# File lib/patch_finder/core/helper.rb, line 49
def send_http_get_request(uri, ssl = false)
  attempts = 1
  u = URI.parse(uri)
  res = nil
  ssl = u.scheme == 'https' ? true : false

  begin
    Net::HTTP.start(u.host, u.port, use_ssl: ssl) do |cli|
      req = Net::HTTP::Get.new(normalize_uri(u.request_uri))
      req['Host'] = u.host
      req['Content-Type'] = 'application/x-www-form-urlencoded'
      res = cli.request(req)
    end
  rescue Net::ReadTimeout, IOError, EOFError, Errno::ECONNRESET,
         Errno::ECONNABORTED, Errno::EPIPE, Net::OpenTimeout,
         Errno::ETIMEDOUT => e
    if attempts < 3
      sleep(5)
      attempts += 1
      retry
    else
      raise e
    end
  end

  res
end

Private Instance Methods

normalize_uri(*strs) click to toggle source

Returns the normalized URI by modifying the double slashes.

@param strs [Array] URI path. @return [String]

# File lib/patch_finder/core/helper.rb, line 157
def normalize_uri(*strs)
  new_str = strs * '/'
  new_str = new_str.gsub!('//', '/') while new_str.index('//')
  new_str
end
save_file(data, dest_file) click to toggle source

Saves a file to a specific location.

@param data [String] @param dest_file [String] @return [void]

# File lib/patch_finder/core/helper.rb, line 147
def save_file(data, dest_file)
  File.open(dest_file, 'wb') do |f|
    f.write(data)
  end
end