Class: Seo::ReplaceLegacyImages

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

Defined Under Namespace

Classes: Result

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

#process(html_fragment) ⇒ Object



6
7
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
# File 'app/services/seo/replace_legacy_images.rb', line 6

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 unless url.present?
      logger.info "Processing #{url}"
      new_url = DragonflyTranslator.new.process(url) rescue nil
      unless new_url
        puts "!!! No DF Url : #{url}" 
        next 
      end
      if new_url != url
        links_modified << { in: url, out: new_url }
        img.attributes["src"].value = new_url
        logger.info "New URL: #{new_url}"
      end
    end
    links_modified.uniq!(&:inspect)
    links_modified.sort_by!(&:inspect)
    html = html_doc.at('body')&.inner_html
    logger.error "!!! HTML empty" unless html.present?
    Result.new(html_out: html, 
               links_modified: links_modified )
  end
end