Module: Beaker::DSL::Helpers::WebHelpers

Included in:
Beaker::DSL::Helpers
Defined in:
lib/beaker/dsl/helpers/web_helpers.rb

Overview

Convenience methods for checking links and moving web content to hosts

Instance Method Summary collapse

Instance Method Details

Determine is a given URL is accessible

Examples:

extension = link_exists?("#{URL}.tar.gz") ? ".tar.gz" : ".tar"

Parameters:

  • link (String)

    The URL to examine

  • limit (Integer) (defaults to: 10)

    redirect limit, will follow redirects that many times

Returns:

  • (Boolean)

    true if the ultimate URL after following redirects (301&302) has a ‘200’ HTTP response code, false otherwise



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/beaker/dsl/helpers/web_helpers.rb', line 20

def link_exists?(link, limit = 10)
  begin
    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.verify_mode = (OpenSSL::SSL::VERIFY_NONE)
    response = http.start { |http| http.head(url.request_uri) }
    if (%w[301 302].include? response.code) && limit > 0
      logger.debug("#{__method__} following #{response.code} to #{response['location']}")
      link_exists?(response['location'], limit - 1)
    else
      response.code == "200"
    end
  rescue
    return false
  end
end

#port_open_within?(host, port = 8140, seconds = 120) ⇒ Boolean

Blocks until the port is open on the host specified, returns false on failure

Returns:

  • (Boolean)


8
9
10
11
12
# File 'lib/beaker/dsl/helpers/web_helpers.rb', line 8

def port_open_within?(host, port = 8140, seconds = 120)
  repeat_for(seconds) do
    host.port_open?(port)
  end
end