class SeoReport::Report

Attributes

data[R]
start_url[R]

Public Class Methods

extractions() click to toggle source
# File lib/seo_report/report.rb, line 13
def self.extractions
  @extractions ||= {}
end
new(start_url) click to toggle source
# File lib/seo_report/report.rb, line 17
def initialize(start_url)
  @start_url = start_url
end
register_extraction(type, module_name, *method_names) click to toggle source
# File lib/seo_report/report.rb, line 7
def self.register_extraction(type, module_name, *method_names)
  include module_name
  extractions_for_type = extractions[type] ||= []
  method_names.each { |m| extractions_for_type << m }
end

Public Instance Methods

produce() click to toggle source
# File lib/seo_report/report.rb, line 21
def produce
  @data = generate_report
end

Protected Instance Methods

build_request_chain() click to toggle source
# File lib/seo_report/report.rb, line 41
def build_request_chain
  RequestChain.new(start_url).tap(&:perform)
end
content_through_extractions(base, document, type: :html) click to toggle source
# File lib/seo_report/report.rb, line 58
def content_through_extractions(base, document, type: :html)
  self.class.extractions.fetch(type, []).reduce(base) do |current, method_name|
    current.merge(send(method_name, document))
  end
end
generate_html_report(request) click to toggle source
# File lib/seo_report/report.rb, line 45
def generate_html_report(request)
  if request.response.is_a?(Net::HTTPOK)
    doc = Nokogiri::HTML(request.response.body)
    content_through_extractions({}, doc, type: :html)
  elsif request.response.is_a?(Net::HTTPRedirection)
    {
      location: request.response["Location"],
    }
  else
    {}
  end
end
generate_report() click to toggle source
# File lib/seo_report/report.rb, line 26
def generate_report
  {
    requests: request_chain.request_chain.map do |request|
      {
        request_url: request.url.to_s,
        response_code: request.response.code.to_i
      }.merge(generate_html_report(request))
    end
  }
end
request_chain() click to toggle source
# File lib/seo_report/report.rb, line 37
def request_chain
  @request_chain ||= build_request_chain
end
unarray(array) click to toggle source
# File lib/seo_report/report.rb, line 64
def unarray(array)
  if array.respond_to?(:length) && array.respond_to?(:first)
    if array.length <= 1
      array.first
    else
      array
    end
  else
    array
  end
end