Class: SiteHealth::Nurse
- Inherits:
-
Object
- Object
- SiteHealth::Nurse
- Defined in:
- lib/site_health/nurse.rb
Overview
Holds page analysis data
Instance Attribute Summary collapse
-
#checkers ⇒ Object
readonly
Returns the value of attribute checkers.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#failures ⇒ Object
readonly
Returns the value of attribute failures.
-
#issues ⇒ Array<Issue>
readonly
Found issues.
Instance Method Summary collapse
-
#check_failed_url(url) ⇒ Array
All URL that have failed.
-
#check_page(page) ⇒ Hash
Result data.
-
#clerk {|the| ... } ⇒ Object
The event emitter.
-
#initialize(config: SiteHealth.config) ⇒ Nurse
constructor
A new instance of Nurse.
-
#journal ⇒ Hash
Check results.
-
#lab_results(page) ⇒ Hash
Results of all checkers for page.
-
#method_missing(method, *args, &block) ⇒ Object
Provides transparent access to the methods in #clerk.
-
#punch_out! ⇒ Nurse
Returns self.
-
#respond_to_missing?(method, include_private = false) ⇒ Boolean
True if it can respond to method name, false otherwise.
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.
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
#checkers ⇒ Object (readonly)
Returns the value of attribute checkers.
10 11 12 |
# File 'lib/site_health/nurse.rb', line 10 def checkers @checkers end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
10 11 12 |
# File 'lib/site_health/nurse.rb', line 10 def config @config end |
#failures ⇒ Object (readonly)
Returns the value of attribute failures.
10 11 12 |
# File 'lib/site_health/nurse.rb', line 10 def failures @failures end |
#issues ⇒ Array<Issue> (readonly)
Returns 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.
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.
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.
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 |
#journal ⇒ Hash
Returns 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.
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
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.
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 |