Class: Seo::ReplaceLegacyImages

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

Overview

Service object: replace legacy images.

Defined Under Namespace

Classes: Result

Instance Attribute Summary

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 Method Details

#process(html_fragment) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/services/seo/replace_legacy_images.rb', line 8

def process(html_fragment)
  logger.tagged('Seo::ReplaceLegacyImages') do
    links_modified = []
    html_doc = Nokogiri::HTML(html_fragment)
    images = html_doc.css('img')
    images.each do |img|
      url = img.attribute('src').to_s
      next if url.blank?

      logger.info "Processing #{url}"
      new_url = begin
        DragonflyTranslator.new.process(url)
      rescue StandardError
        nil
      end
      unless new_url
        puts "!!! No DF Url : #{url}"
        next
      end
      next unless new_url != url

      links_modified << { in: url, out: new_url }
      img.attributes["src"].value = new_url
      logger.info "New URL: #{new_url}"
    end
    links_modified.uniq!(&:inspect)
    links_modified.sort_by!(&:inspect)
    html = html_doc.at('body')&.inner_html
    logger.error "!!! HTML empty" if html.blank?
    Result.new(html_out: html,
               links_modified: links_modified)
  end
end