Class: NDD::UrlChecker::StatusDecorator

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/ndd/url_checker/status_decorator.rb

Overview

Decorate a [Status] for reporting.

Author:

  • David DIDIER

Instance Method Summary collapse

Instance Method Details

#code_as_cssObject

Returns the CSS class associated with the status.


11
12
13
# File 'lib/ndd/url_checker/status_decorator.rb', line 11

def code_as_css
  code.to_s.gsub(/_/, '-')
end

#details_bodyObject

Returns:

  • nil if the status is :direct

  • all the URLs if the status is :redirected or :too_many_redirects or :unknown_host

  • the error message if the status is :failed


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ndd/url_checker/status_decorator.rb', line 40

def details_body
  case code
    when :direct then
      nil
    when :redirected then
      uris.map { |uri| "- #{uri}\n" }.join
    when :failed then
      return nil unless error
      return error.body if error.kind_of?(Net::HTTPResponse)
      return error.to_s
    when :too_many_redirects then
      uris.map { |uri| "- #{uri}\n" }.join
    when :unknown_host then
      uris.map { |uri| "- #{uri}\n" }.join
  end
end

#details_body_as_htmlObject

Returns an HTML description of the status.


58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ndd/url_checker/status_decorator.rb', line 58

def details_body_as_html
  body = <<-HTML.gsub(/^ +/, '')
    <p><em>Status:</em> #{code}</p>
    <p><em>URIs:</em><p>
    <ol>#{uris.map { |uri| "<li>#{uri}</li>" }.join}</ol>
  HTML

  if error
    body += "<p><em>Error:</em> #{error.class.to_s}</p>"
    body += "<p><pre>#{error.body}</pre></p>" if error.kind_of?(Net::HTTPResponse)
  end

  body
end

#details_titleObject

Returns:

  • nil if the status is :direct

  • the number of redirections if the status is :redirected or :too_many_redirects

  • the error if the status is :failed

  • the last URL if the status is :unknown_host


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ndd/url_checker/status_decorator.rb', line 20

def details_title
  case code
    when :direct then
      nil
    when :redirected then
      uris.size - 1
    when :failed then
      return nil unless error
      return error.class.to_s
    when :too_many_redirects then
      uris.size - 1
    when :unknown_host then
      return uris.last
  end
end