Class: SiteHealth::Nurse

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

Overview

Holds page analysis data

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: SiteHealth.config) ⇒ Nurse

Returns a new instance of Nurse.



15
16
17
18
19
20
21
22
23
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Provides transparent access to the methods in #clerk.

Parameters:

  • name (Symbol)

    The name of the missing method.

  • arguments (Array)

    Additional arguments for the missing method.

Raises:

  • (NoMethodError)

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

See Also:



114
115
116
117
118
119
120
# 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

Instance Attribute Details

#checkersObject (readonly)

Returns the value of attribute checkers.



10
11
12
# File 'lib/site_health/nurse.rb', line 10

def checkers
  @checkers
end

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/site_health/nurse.rb', line 10

def config
  @config
end

#failuresObject (readonly)

Returns the value of attribute failures.



10
11
12
# File 'lib/site_health/nurse.rb', line 10

def failures
  @failures
end

#issuesArray<Issue> (readonly)

Returns found issues.

Returns:

  • (Array<Issue>)

    found issues



13
14
15
# File 'lib/site_health/nurse.rb', line 13

def issues
  @issues
end

Instance Method Details

#check_failed_url(url) ⇒ Array

Returns all URL that have failed.

Returns:

  • (Array)

    all URL that have failed



42
43
44
45
# File 'lib/site_health/nurse.rb', line 42

def check_failed_url(url)
  clerk.emit_failed_url(url)
  @failures << url
end

#check_page(page) ⇒ Hash

Returns result data.

Returns:

  • (Hash)

    result data



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 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 {|the| ... } ⇒ Object

Returns the event emitter.

Yield Parameters:

  • the (Object)

    event emiiter

Returns:

  • (Object)

    the event emitter



49
50
51
52
53
54
# 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

#journalHash

Returns check results.

Returns:

  • (Hash)

    check results



34
35
36
37
38
39
# 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) ⇒ Hash

Returns results of all checkers for page.

Returns:

  • (Hash)

    results of all checkers for page



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# 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

#punch_out!Nurse

Returns self

Returns:

  • (Nurse)

    returns self



26
27
28
29
30
31
# 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) ⇒ Boolean

Returns true if it can respond to method name, false otherwise.

Parameters:

  • name (Symbol)

    The name of the missing method.

  • include_private (Boolean) (defaults to: false)

    optional (default: false) Whether to include private methods

Returns:

  • (Boolean)

    true if it can respond to method name, false otherwise



128
129
130
# File 'lib/site_health/nurse.rb', line 128

def respond_to_missing?(method, include_private = false)
  clerk.respond_to?(method, include_private) || super
end