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
Public Class Methods
@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
# 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
@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
# 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
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
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
# 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
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
@return [Hash] issue types data
# File lib/site_health/checkers/checker.rb, line 104 def issue_types self.class.issue_types end
@return [String] the name of the checker
# File lib/site_health/checkers/checker.rb, line 94 def name self.class.name end
@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
@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
@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
@return [String] the page URL
# File lib/site_health/checkers/checker.rb, line 89 def url page.url end
Protected Instance Methods
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