Class: SiteHealth::Checker
- Inherits:
-
Object
- Object
- SiteHealth::Checker
- Defined in:
- lib/site_health/checkers/checker.rb
Overview
Parent class for all checkers (all checkers must inheirit from this class)
Direct Known Subclasses
FacebookShareLink, GooglePageSpeed, HTMLProofer, JSONSyntax, MissingDescription, MissingTitle, PageNotFound, Redirect, W3CCSS, W3CHTML, XML
Constant Summary collapse
- CHECKABLE_TYPES =
All possible page types that can be checked
%i[ plain_text directory xsl rss atom ms_word pdf zip javascript json css xml html ].freeze
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#issues ⇒ Object
readonly
Returns the value of attribute issues.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#page ⇒ Object
readonly
Returns the value of attribute page.
Class Method Summary collapse
-
.issue_types(types = :__get_value__) ⇒ Hash
the issues data - optional, if not present it will return the current data.
- .name(name = '__get_value__') ⇒ Object
- .types(types = '__get_value__') ⇒ Object
Instance Method Summary collapse
-
#add_data(hash) ⇒ Hash
Adds data.
-
#add_issue(**args) ⇒ Array<Issue>
Adds an issue.
- #add_issue_type(type, **args) ⇒ Object
-
#call {|yields| ... } ⇒ CheckerResult
Run the checker.
-
#initialize(page, config: SiteHealth.config) ⇒ Checker
constructor
A new instance of Checker.
-
#issue_types ⇒ Hash
Issue types data.
-
#name ⇒ String
The name of the checker.
-
#should_check? ⇒ Boolean
Determines whether the checker should run.
-
#to_h ⇒ Hash
Hash representation of the object.
-
#types ⇒ Array<Symbol>
List of page types the checker will run on.
-
#url ⇒ String
The page URL.
Constructor Details
#initialize(page, config: SiteHealth.config) ⇒ Checker
Returns a new instance of Checker.
66 67 68 69 70 71 72 |
# 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 |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
62 63 64 |
# File 'lib/site_health/checkers/checker.rb', line 62 def config @config end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
62 63 64 |
# File 'lib/site_health/checkers/checker.rb', line 62 def data @data end |
#issues ⇒ Object (readonly)
Returns the value of attribute issues.
62 63 64 |
# File 'lib/site_health/checkers/checker.rb', line 62 def issues @issues end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
62 63 64 |
# File 'lib/site_health/checkers/checker.rb', line 62 def logger @logger end |
#page ⇒ Object (readonly)
Returns the value of attribute page.
62 63 64 |
# File 'lib/site_health/checkers/checker.rb', line 62 def page @page end |
Class Method Details
.issue_types(types = :__get_value__) ⇒ Hash
the issues data - optional, if not present it will return the current data
50 51 52 53 54 55 56 57 58 59 60 |
# 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__') ⇒ Object
27 28 29 30 31 32 33 34 35 36 |
# 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 |
.types(types = '__get_value__') ⇒ Object
38 39 40 41 42 43 44 45 |
# 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 |
Instance Method Details
#add_data(hash) ⇒ Hash
Adds data
131 132 133 |
# File 'lib/site_health/checkers/checker.rb', line 131 def add_data(hash) data.add(hash) end |
#add_issue(**args) ⇒ Array<Issue>
Adds an issue
116 117 118 |
# 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) ⇒ Object
120 121 122 123 124 125 126 |
# 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 {|yields| ... } ⇒ CheckerResult
Run the checker
77 78 79 80 81 82 83 84 85 86 |
# 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 ⇒ Hash
Returns issue types data.
104 105 106 |
# File 'lib/site_health/checkers/checker.rb', line 104 def issue_types self.class.issue_types end |
#name ⇒ String
Returns the name of the checker.
94 95 96 |
# File 'lib/site_health/checkers/checker.rb', line 94 def name self.class.name end |
#should_check? ⇒ Boolean
Returns determines whether the checker should run.
109 110 111 |
# File 'lib/site_health/checkers/checker.rb', line 109 def should_check? types.any? { |type| page.public_send("#{type}?") } end |
#to_h ⇒ Hash
Returns hash representation of the object.
136 137 138 139 140 141 142 |
# 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 ⇒ Array<Symbol>
Returns list of page types the checker will run on.
99 100 101 |
# File 'lib/site_health/checkers/checker.rb', line 99 def types self.class.types end |
#url ⇒ String
Returns the page URL.
89 90 91 |
# File 'lib/site_health/checkers/checker.rb', line 89 def url page.url end |