Class: Seo::LinkAnalyzer

Inherits:
BaseService show all
Defined in:
app/services/seo/link_analyzer.rb

Overview

Service object: link analyzer.

Defined Under Namespace

Classes: Result

Constant Summary collapse

HEAD_PROBE_POLICY =

HEAD probe policy for per-link checks: no redirect following (the
Location header IS the finding), no content extraction. The crawler
builds a fresh HTTP client per call, so the 15-thread fan-out stays
thread-safe (http.rb connection state isn't shareable).

Heatwave::Crawler::Policy.new(
  http_method: :head,
  follow_redirects: false,
  timeout_seconds: 15
).freeze

Instance Attribute Summary collapse

Attributes inherited from BaseService

#options

Instance Method Summary collapse

Methods inherited from BaseService

#initialize, #log_debug, #log_error, #log_info, #log_warning, #logger, #tagged_logger

Constructor Details

This class inherits a constructor from BaseService

Instance Attribute Details

#html_docObject (readonly)

Returns the value of attribute html_doc.



5
6
7
# File 'app/services/seo/link_analyzer.rb', line 5

def html_doc
  @html_doc
end

#html_rawObject (readonly)

Returns the value of attribute html_raw.



5
6
7
# File 'app/services/seo/link_analyzer.rb', line 5

def html_raw
  @html_raw
end

Returns the value of attribute link_analysis.



5
6
7
# File 'app/services/seo/link_analyzer.rb', line 5

def link_analysis
  @link_analysis
end

Returns the value of attribute links.



5
6
7
# File 'app/services/seo/link_analyzer.rb', line 5

def links
  @links
end

#localeObject (readonly)

Returns the value of attribute locale.



5
6
7
# File 'app/services/seo/link_analyzer.rb', line 5

def locale
  @locale
end

Instance Method Details



48
49
50
51
52
53
54
55
56
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
84
# File 'app/services/seo/link_analyzer.rb', line 48

def check_links(links)
  results = {}
  links.each_slice(15) do |group| # 15 threads at a time
    threads = []
    group.each do |href|
      if (uri = begin
        Addressable::URI.parse(href)
      rescue StandardError
        nil
      end)
        logger.info "Checking #{uri}"
        threads << Thread.new do
          probe = Heatwave::Crawler.fetch(uri.to_s, mode: :probe, policy: HEAD_PROBE_POLICY)
          if probe.success?
            results[href] = { result: probe.status }
            if (redirect_location = probe.details[:location].presence)
              uri_r = Addressable::URI.parse(redirect_location)
              unless uri_r.absolute? # Copy from original
                uri_r.scheme = uri.scheme
                uri_r.port = uri.port unless uri.port.in?([80, 443])
                uri_r.host = uri.host
              end
              results[href][:location] = uri_r.to_s
            end
          else
            results[href] = probe_failure_result(probe)
          end
        end
      else
        logger.warn "Bad url: #{href}"
        results[href] = { result: :invalid_url }
      end
    end
    threads.each(&:join)
  end
  results
end


29
30
31
32
33
34
35
36
# File 'app/services/seo/link_analyzer.rb', line 29

def extract_links(_html_raw = nil)
  return [] if _html_raw.blank?

  html_localized = localize_links(_html_raw)
  doc = Nokogiri::HTML(html_localized)
  links = doc.css('a')
  links.map { |link| link.attribute('href').to_s }.uniq.sort.delete_if { |href| href.blank? || !href.starts_with?('http') }
end


25
26
27
# File 'app/services/seo/link_analyzer.rb', line 25

def localize_links(_html_raw = nil)
  (_html_raw || html_raw).gsub(/\{\{\s*locale\s*\}\}/, (locale || 'en-US').to_s)
end

#process(html_raw, locale: I18n.locale) ⇒ Object

Feed me an html raw with links and i'll tell you everything that's wrong with them



13
14
15
16
17
18
19
20
21
22
23
# File 'app/services/seo/link_analyzer.rb', line 13

def process(html_raw, locale: I18n.locale)
  return Result.new(link_analysis: [], status: :skipped) if html_raw.blank?

  logger.tagged('Seo::LinkAnalyzer') do
    @locale = locale
    @html_raw = html_raw
    @links = extract_links
    @link_analysis = check_links(@links)
  end
  Result.new(link_analysis: @link_analysis, status: :ok)
end