Module: WebTest::Util

Defined in:
lib/web_test/util.rb

Class Method Summary collapse

Class Method Details

.connection(follow: false) ⇒ Object

private



64
65
66
67
68
69
70
71
# File 'lib/web_test/util.rb', line 64

def self.connection(follow: false)
  Faraday.new do |c|
    c.options[:timeout] = TIMEOUT_IN_SECONDS
    c.options[:open_timeout] = OPEN_TIMEOUT_IN_SECONDS
    c.use(FaradayMiddleware::FollowRedirects, limit: 4) if follow
    c.adapter :net_http
  end
end

.error_message(errors) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/web_test/util.rb', line 10

def self.error_message(errors)
  return errors.message if errors.respond_to?(:message)

  errors
    .map(&:to_s)
    .join('; ')
    .capitalize
end

.get(url_or_domain_name, follow: false) ⇒ Object



29
30
31
# File 'lib/web_test/util.rb', line 29

def self.get(url_or_domain_name, follow: false)
  request(:get, url_or_domain_name, follow: follow)
end

.head(url_or_domain_name, follow: false) ⇒ Object



25
26
27
# File 'lib/web_test/util.rb', line 25

def self.head(url_or_domain_name, follow: false)
  request(:head, url_or_domain_name, follow: follow)
end

.make_domain_name(url_or_domain_name) ⇒ Object

Return just the domain name portion of a URL if it’s simply of the form name.tld



85
86
87
88
89
90
91
# File 'lib/web_test/util.rb', line 85

def self.make_domain_name(url_or_domain_name)
  if %r{^https?://(.+)} =~ url_or_domain_name
    $1
  else
    url_or_domain_name
  end
end

.make_url(url_or_domain_name) ⇒ Object

Ensure that the given string is a URL, making it into one if necessary.



75
76
77
78
79
80
81
# File 'lib/web_test/util.rb', line 75

def self.make_url(url_or_domain_name)
  if %r{^https?://} =~ url_or_domain_name
    url_or_domain_name
  else
    "http://#{url_or_domain_name}"
  end
end

.recheck_on_timeoutObject



99
100
101
102
103
# File 'lib/web_test/util.rb', line 99

def self.recheck_on_timeout
  yield
rescue Faraday::Error::TimeoutError
  yield
end

.remove_protocol(domain_name_or_url) ⇒ Object

Normalize the input: remove ‘http(s)://’ if it’s there



94
95
96
97
# File 'lib/web_test/util.rb', line 94

def self.remove_protocol(domain_name_or_url)
  %r{^https?://(?<name>.+)$} =~ domain_name_or_url
  name || domain_name_or_url
end

.request(method, url_or_domain_name, follow: false) ⇒ Object



33
34
35
36
37
# File 'lib/web_test/util.rb', line 33

def self.request(method, url_or_domain_name, follow: false)
  url = make_url(url_or_domain_name)
  response = recheck_on_timeout { connection(follow: follow).send(method, url) }
  [response.status, response.headers]
end

.status(url_or_domain_name, follow: false) ⇒ Object



19
20
21
22
23
# File 'lib/web_test/util.rb', line 19

def self.status(url_or_domain_name, follow: false)
  code = head(url_or_domain_name, follow: follow)[0]
  return code if code != 405
  get(url_or_domain_name, follow: follow)[0]
end

.try_ssl_connection(domain_name_or_url) ⇒ Object



56
57
58
59
60
# File 'lib/web_test/util.rb', line 56

def self.try_ssl_connection(domain_name_or_url)
  url = "https://#{remove_protocol(domain_name_or_url)}"
  recheck_on_timeout { connection.head(url) }
  true
end

.up?(url_or_domain_name) ⇒ Boolean

and follow a few redirects if necessary.

Returns:

  • (Boolean)

    true if the given page has status 200,



41
42
43
44
45
46
# File 'lib/web_test/util.rb', line 41

def self.up?(url_or_domain_name)
  url  = make_url(url_or_domain_name)
  conn = connection(follow: true)
  response = recheck_on_timeout { conn.head(url) }
  response.status == 200
end

.valid_cert?(domain_name_or_url) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
# File 'lib/web_test/util.rb', line 48

def self.valid_cert?(domain_name_or_url)
  try_ssl_connection(domain_name_or_url)
  true
rescue
  # Not serving SSL, expired, or incorrect domain name in certificate
  false
end