Class: NDD::UrlChecker::ReportingUrlChecker
- Inherits:
-
AbstractUrlChecker
- Object
- AbstractUrlChecker
- NDD::UrlChecker::ReportingUrlChecker
- Defined in:
- lib/ndd/url_checker/reporting_url_checker.rb
Overview
Wraps an instance of URL checker and provides reporting capabilities using ERB templates.
Instance Attribute Summary collapse
-
#delegate ⇒ #check
readonly
the delegate URL checker.
Instance Method Summary collapse
-
#check(*urls) ⇒ NDD::UrlChecker::Status+
Checks that the given URLs are valid.
-
#initialize(delegate_checker) ⇒ ReportingUrlChecker
constructor
Create a new instance.
-
#report(template, output_path = nil) ⇒ String
Creates a report about the previous check using the specified template.
Constructor Details
#initialize(delegate_checker) ⇒ ReportingUrlChecker
Create a new instance.
19 20 21 22 |
# File 'lib/ndd/url_checker/reporting_url_checker.rb', line 19 def initialize(delegate_checker) @logger = Logging.logger[self] @delegate = delegate_checker end |
Instance Attribute Details
#delegate ⇒ #check (readonly)
the delegate URL checker.
13 14 15 |
# File 'lib/ndd/url_checker/reporting_url_checker.rb', line 13 def delegate @delegate end |
Instance Method Details
#check(*urls) ⇒ NDD::UrlChecker::Status+
Checks that the given URLs are valid.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/ndd/url_checker/reporting_url_checker.rb', line 28 def check(*urls) results = nil benchmark = Benchmark.measure { results = @delegate.check(*urls) } @logger.debug "Checked #{urls.size} URL(s) benchmark: #{benchmark}" if urls.size > 1 statuses = results.map { |status| StatusDecorator.new(status) }.sort_by { |status| status.uri } else statuses = [results].map { |status| StatusDecorator.new(status) } end @context = OpenStruct.new @context.statuses = statuses @context.urls = OpenStruct.new @context.urls.count = urls.size @context.urls.valid_count = statuses.select { |status| status.valid? }.size @context.urls.direct_count = statuses.select { |status| status.code == :direct }.size @context.urls.redirected_count = statuses.select { |status| status.code == :redirected }.size @context.urls.invalid_count = statuses.select { |status| status.invalid? }.size @context.urls.failed_count = statuses.select { |status| status.code == :failed }.size @context.urls.too_many_redirects_count = statuses.select { |status| status.code == :too_many_redirects }.size @context.urls.unknown_host_count = statuses.select { |status| status.code == :unknown_host }.size @context.benchmark = OpenStruct.new @context.benchmark.raw = benchmark @context.benchmark.total_duration = benchmark.real @context.benchmark.average_duration = benchmark.real / urls.size @context.benchmark.average_throughput= urls.size / benchmark.real results end |
#report(template, output_path = nil) ⇒ String
Creates a report about the previous check using the specified template. The result may be written to a file. @param template [Symbol, String] a predefined template (:csv, :html, :json) or the path of a template file. @param output_path [String, nil] the path of the output file.
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/ndd/url_checker/reporting_url_checker.rb', line 65 def report(template, output_path=nil) template_path = template_path(template) template_content = ERB.new(File.new(template_path).read) report = template_content.result(@context.instance_eval { binding }) if output_path @logger.info "Reporting to #{output_path}" File.open(output_path, 'w') { |file| file.write(report) } end report end |