class SiteHealth::Nurse

Holds page analysis data

Attributes

checkers[R]
config[R]
failures[R]
issues[R]

@return [Array<Issue>] found issues

Public Class Methods

new(config: SiteHealth.config) click to toggle source
# File lib/site_health/nurse.rb, line 15
def initialize(config: SiteHealth.config)
  @config = config
  @checkers = config.checkers
  @pages_journal = UrlMap.new { {} }
  @failures = []
  @issues = []
  @clerk = nil
  @punched_out = false
end

Public Instance Methods

check_failed_url(url) click to toggle source

@return [Array] all URL that have failed

# File lib/site_health/nurse.rb, line 42
def check_failed_url(url)
  clerk.emit_failed_url(url)
  @failures << url
end
check_page(page) click to toggle source

@return [Hash] result data

# File lib/site_health/nurse.rb, line 57
def check_page(page)
  @pages_journal[page.url].tap do |journal|
    timer = Timer.start
    clerk.emit_page(page)

    journal[:started_at] = timer.started_at
    journal[:checked] = true
    journal[:url] = page.url
    journal[:content_type] = page.content_type
    journal[:http_status] = page.code
    journal[:redirect] = page.redirect?
    journal[:title] = page.title
    journal[:links_to] = page.each_url.map do |url|
      (@pages_journal[url][:links_from] ||= []) << page.url
      url.to_s
    end

    journal[:checks] = lab_results(page)

    timer.finish

    journal[:finished_at] = timer.finished_at
    journal[:runtime_in_seconds] = timer.diff.round(1)

    clerk.emit_journal(journal, page)
  end
end
clerk() { |e| ... } click to toggle source

@return [Object] the event emitter @yieldparam [Object] the event emiiter

# File lib/site_health/nurse.rb, line 49
def clerk
  @clerk ||= begin
    events = %w[journal failed_url check page issue].concat(checkers.map(&:name))
    EventEmitter.define(*events).new.tap { |e| yield(e) if block_given? }
  end
end
journal() click to toggle source

@return [Hash] check results

# File lib/site_health/nurse.rb, line 34
def journal
  {
    checked_urls: @pages_journal.to_h,
    internal_server_error_urls: failures,
  }
end
lab_results(page) click to toggle source

@return [Hash] results of all checkers for page

# File lib/site_health/nurse.rb, line 86
def lab_results(page)
  journal = {}
  checkers.each do |checker_klass|
    checker = checker_klass.new(page, config: config)
    next unless checker.should_check?

    checker.call

    issues = checker.issues
    @issues.concat(issues.to_a)

    clerk.emit_check(checker)
    clerk.emit(checker.name, checker)
    clerk.emit_each_issue(issues)

    journal[checker.name.to_sym] = checker.to_h
  end
  journal
end
method_missing(method, *args, &block) click to toggle source

Provides transparent access to the methods in {#clerk}. @param [Symbol] name

The name of the missing method.

@param [Array] arguments

Additional arguments for the missing method.

@raise [NoMethodError]

The missing method did not map to a method in {#clerk}.

@see clerk

Calls superclass method
# File lib/site_health/nurse.rb, line 114
def method_missing(method, *args, &block)
  if clerk.respond_to?(method)
    return clerk.public_send(method, *args, &block)
  end

  super
end
punch_out!() click to toggle source

@return [Nurse] returns self

# File lib/site_health/nurse.rb, line 26
def punch_out!
  post_shift_analysis unless @punched_out

  @punched_out = true
  self
end
respond_to_missing?(method, include_private = false) click to toggle source

@param [Symbol] name

The name of the missing method.

@param [Boolean] include_private optional (default: false)

Whether to include private methods

@return [Boolean]

true if it can respond to method name, false otherwise
Calls superclass method
# File lib/site_health/nurse.rb, line 128
def respond_to_missing?(method, include_private = false)
  clerk.respond_to?(method, include_private) || super
end

Private Instance Methods

post_shift_analysis() click to toggle source
# File lib/site_health/nurse.rb, line 134
def post_shift_analysis
  issues = links_to_page_not_found_issues
  clerk.emit_each_issue(issues)
  @issues.concat(issues)
end