module Beaker::DSL::Helpers::WebHelpers

def fetch_http_dir(url, dst_dir)

Returns:
  • (String) - dst The name of the newly-created subdirectory of

Parameters:
  • dst_dir (String) -- The local destination directory.
  • url (String) -- The base http url from which to recursively download
def fetch_http_dir(url, dst_dir)
  logger.notify "fetch_http_dir (url: #{url}, dst_dir #{dst_dir})"
  if url[-1, 1] !~ /\//
    url += '/'
  end
  url = URI.parse(url)
  chunks = url.path.split('/')
  dst = File.join(dst_dir, chunks.last)
  #determine directory structure to cut
  #only want to keep the last directory, thus cut total number of dirs - 2 (hostname + last dir name)
  cut = chunks.length - 2
  wget_command = "wget -nv -P #{dst_dir} --reject \"index.html*\",\"*.gif\" --cut-dirs=#{cut} -np -nH --no-check-certificate -r #{url}"
  logger.notify "Fetching remote directory: #{url}"
  logger.notify "  and saving to #{dst}"
  logger.notify "  using command: #{wget_command}"
  #in ruby 1.9+ we can upgrade this to popen3 to gain access to the subprocess pid
  result = `#{wget_command} 2>&1`
  result.each_line do |line|
    logger.debug(line)
  end
  if $?.to_i != 0
    raise "Failed to fetch_remote_dir '#{url}' (exit code #{$?}"
  end
  dst
end

def fetch_http_file(base_url, file_name, dst_dir)

Returns:
  • (String) - dst The name of the newly-created file.

Parameters:
  • dst_dir (String) -- The local destination directory.
  • file_name (String) -- The trailing name component of both the source url
  • base_url (String) -- The base url from which to recursively download
def fetch_http_file(base_url, file_name, dst_dir)
  require 'open-uri'
  require 'open_uri_redirections'
  FileUtils.makedirs(dst_dir)
  base_url.chomp!('/')
  src = "#{base_url}/#{file_name}"
  dst = File.join(dst_dir, file_name)
  if File.exists?(dst)
    logger.notify "Already fetched #{dst}"
  else
    logger.notify "Fetching: #{src}"
    logger.notify "  and saving to #{dst}"
    open(src, :allow_redirections => :all) do |remote|
      File.open(dst, "w") do |file|
        FileUtils.copy_stream(remote, file)
      end
    end
  end
  return dst
end

def link_exists?(link)

Returns:
  • (Boolean) - true if the URL has a '200' HTTP response code, false otherwise

Parameters:
  • link (String) -- The URL to examine
def link_exists?(link)
  require "net/http"
  require "net/https"
  require "open-uri"
  url = URI.parse(link)
  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = (url.scheme == 'https')
  http.start do |http|
    return http.head(url.request_uri).code == "200"
  end
end

def port_open_within?( host, port = 8140, seconds = 120 )

on failure
Blocks until the port is open on the host specified, returns false
def port_open_within?( host, port = 8140, seconds = 120 )
  repeat_for( seconds ) do
    host.port_open?( port )
  end
end