Class: SiteHealth::Checker

Inherits:
Object
  • Object
show all
Defined in:
lib/site_health/checkers/checker.rb

Overview

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

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page, config: SiteHealth.config) ⇒ Checker

Returns a new instance of Checker.

Parameters:



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

#configObject (readonly)

Returns the value of attribute config.



62
63
64
# File 'lib/site_health/checkers/checker.rb', line 62

def config
  @config
end

#dataObject (readonly)

Returns the value of attribute data.



62
63
64
# File 'lib/site_health/checkers/checker.rb', line 62

def data
  @data
end

#issuesObject (readonly)

Returns the value of attribute issues.



62
63
64
# File 'lib/site_health/checkers/checker.rb', line 62

def issues
  @issues
end

#loggerObject (readonly)

Returns the value of attribute logger.



62
63
64
# File 'lib/site_health/checkers/checker.rb', line 62

def logger
  @logger
end

#pageObject (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

Parameters:

  • types (Hash) (defaults to: :__get_value__)

Returns:

  • (Hash)

    the issues types 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

Parameters:

  • the (Hash)

    hash to be added

Returns:

  • (Hash)

    the current 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

Returns:

  • (Array<Issue>)

    the current list of issues

See Also:



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

Yield Parameters:

Returns:

  • (CheckerResult)

    returns self



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_typesHash

Returns issue types data.

Returns:

  • (Hash)

    issue types data



104
105
106
# File 'lib/site_health/checkers/checker.rb', line 104

def issue_types
  self.class.issue_types
end

#nameString

Returns the name of the checker.

Returns:

  • (String)

    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.

Returns:

  • (Boolean)

    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_hHash

Returns hash representation of the object.

Returns:

  • (Hash)

    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

#typesArray<Symbol>

Returns list of page types the checker will run on.

Returns:

  • (Array<Symbol>)

    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

#urlString

Returns the page URL.

Returns:

  • (String)

    the page URL



89
90
91
# File 'lib/site_health/checkers/checker.rb', line 89

def url
  page.url
end