class SiteHealth::Checker

Parent class for all checkers (all checkers must inheirit from this class)

Constants

CHECKABLE_TYPES

All possible page types that can be checked

Attributes

config[R]
data[R]
issues[R]
logger[R]
page[R]

Public Class Methods

issue_types(types = :__get_value__) click to toggle source

@param [Hash] types the issues data - optional, if not present it will return the current data @return [Hash] the issues types data

# File lib/site_health/checkers/checker.rb, line 50
def self.issue_types(types = :__get_value__)
  if types == :__get_value__
    return @issue_types ||= {}
  end

  default = types.fetch(:_default, {})
  @issue_types = types.map do |key, data|
    issue_data = { code: key }.merge!(default).merge!(data)
    [key, issue_data]
  end.to_h
end
name(name = '__get_value__') click to toggle source
Calls superclass method
# File lib/site_health/checkers/checker.rb, line 27
def self.name(name = '__get_value__')
  if name == '__get_value__'
    return @name if @name

    @name = (super() || SecureRandom.hex).downcase.gsub(/sitehealth::/, '')
    return @name
  end

  @name = name.to_s
end
new(page, config: SiteHealth.config) click to toggle source

@param [Spidr::Page] page the crawled page @param config [SiteHealth::Configuration]

# File lib/site_health/checkers/checker.rb, line 66
def initialize(page, config: SiteHealth.config)
  @page = page
  @config = config
  @logger = config.logger
  @issues = Issues.new(name)
  @data = CheckData.new
end
types(types = '__get_value__') click to toggle source
# File lib/site_health/checkers/checker.rb, line 38
def self.types(types = '__get_value__')
  if types == '__get_value__'
    @types ||= CHECKABLE_TYPES
    return @types
  end

  @types = Array(types).map(&:to_sym)
end

Public Instance Methods

add_data(hash) click to toggle source

Adds data @param [Hash] the hash to be added @return [Hash] the current data

# File lib/site_health/checkers/checker.rb, line 131
def add_data(hash)
  data.add(hash)
end
add_issue(**args) click to toggle source

Adds an issue @return [Array<Issue>] the current list of issues @see Issue#initialize for supported arguments

# File lib/site_health/checkers/checker.rb, line 116
def add_issue(**args)
  issues << Issue.new({ name: name, url: page.url }.merge!(**args))
end
add_issue_type(type, **args) click to toggle source
# File lib/site_health/checkers/checker.rb, line 120
def add_issue_type(type, **args)
  data = issue_types.fetch(type) do
    raise(ArgumentError, "unknown issue type #{type}, known types are: #{issue_types.keys.join(', ')}") # rubocop:disable Metrics/LineLength
  end

  add_issue(data.merge(**args))
end
call() { |self| ... } click to toggle source

Run the checker @yieldparam [Checker] yields self @return [CheckerResult] returns self

# File lib/site_health/checkers/checker.rb, line 77
def call
  timer = Timer.measure { check }
  add_data(
    started_at: timer.started_at,
    finished_at: timer.finished_at,
    runtime_in_seconds: timer.diff.to_f
  )
  yield(self) if block_given?
  self
end
issue_types() click to toggle source

@return [Hash] issue types data

# File lib/site_health/checkers/checker.rb, line 104
def issue_types
  self.class.issue_types
end
name() click to toggle source

@return [String] the name of the checker

# File lib/site_health/checkers/checker.rb, line 94
def name
  self.class.name
end
should_check?() click to toggle source

@return [Boolean] determines whether the checker should run

# File lib/site_health/checkers/checker.rb, line 109
def should_check?
  types.any? { |type| page.public_send("#{type}?") }
end
to_h() click to toggle source

@return [Hash] hash representation of the object

# File lib/site_health/checkers/checker.rb, line 136
def to_h
  {
    name: name.to_sym,
    data: data.to_h,
    issues: issues.map(&:to_h),
  }
end
types() click to toggle source

@return [Array<Symbol>] list of page types the checker will run on

# File lib/site_health/checkers/checker.rb, line 99
def types
  self.class.types
end
url() click to toggle source

@return [String] the page URL

# File lib/site_health/checkers/checker.rb, line 89
def url
  page.url
end

Protected Instance Methods

check() click to toggle source

Abstract method that subclasses must implement @raise [NotImplementedError] subclasses must implement

# File lib/site_health/checkers/checker.rb, line 148
def check
  raise(NotImplementedError, 'please implement!')
end