Class: Seo::LinkAnalyzer

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

Defined Under Namespace

Classes: Result

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseService

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

Constructor Details

This class inherits a constructor from BaseService

Instance Attribute Details

#html_docObject (readonly)

Returns the value of attribute html_doc.



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

def html_doc
  @html_doc
end

#html_rawObject (readonly)

Returns the value of attribute html_raw.



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

def html_raw
  @html_raw
end

Returns the value of attribute link_analysis.



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

def link_analysis
  @link_analysis
end

Returns the value of attribute links.



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

def links
  @links
end

#localeObject (readonly)

Returns the value of attribute locale.



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

def locale
  @locale
end

Instance Method Details



39
40
41
42
43
44
45
46
47
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
# File 'app/services/seo/link_analyzer.rb', line 39

def check_links(links)
  results = {}
  links.each_slice(15) do |group| # 15 threads at a time
    threads = []
    group.each do |href|
      if uri = Addressable::URI.parse(href) rescue nil
        logger.info "Checking #{uri}"
        threads << Thread.new do
          begin
            res = client.head(uri)
            results[href] = { result: res.status_code }
            if redirect_location = res.headers['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
          rescue HTTPClient::ConnectTimeoutError
            results[href] = { result: 408 }
          rescue HTTPClient::ReceiveTimeoutError
            results[href] = { result: 408 }
          rescue SocketError
            results[href] = { result: 404 }
          rescue StandardError => exc
            results[href] = { result: 500, exception: exc }
          end
        end
      else
        logger.warn "Bad url: #{href}"
        results[href] = { result: :invalid_url }
      end
    end
    threads.each { |thr| thr.join }
  end
  results
end

#clientObject



35
36
37
# File 'app/services/seo/link_analyzer.rb', line 35

def client
  @client ||= HTTPClient.new
end


26
27
28
29
30
31
32
33
# File 'app/services/seo/link_analyzer.rb', line 26

def extract_links(_html_raw=nil)
  return [] unless _html_raw.present?
  html_localized = localize_links(_html_raw)
  doc = Nokogiri::HTML(html_localized)
  links = doc.css('a')
  hrefs = links.map {|link| link.attribute('href').to_s}.uniq.sort.delete_if{|href| href.blank? || !href.starts_with?('http') }
  hrefs
end


22
23
24
# File 'app/services/seo/link_analyzer.rb', line 22

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



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

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