Class: Seo::HardcodedImageToLiquid

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

Overview

Service object: hardcoded image to liquid.

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(post) ⇒ Object



4
5
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
# File 'app/services/seo/hardcoded_image_to_liquid.rb', line 4

def process(post)
  logger.tagged('Seo::ReplaceLegacyImages') do
    blog_post = Post.find(post)
    html_fragment = blog_post.solution
    html_doc = Nokogiri::HTML(html_fragment)
    images = html_doc.css('img')
    images.each do |img|
      image_url = img.attribute('src').to_s
      image_id = img.attribute('data-image-id')
      image_width = img.attribute('width')
      image_height = img.attribute('height')
      image_alt = img.attribute('alt').to_s
      image_class = img.attribute('class').to_s

      next if image_url.blank?
      next unless begin
        URI.parse(image_url).host.include?('warmlyyours')
      rescue StandardError
        false
      end

      logger.info "Processing #{image_url}"
      liquid_image = "{% image_tag '#{image_id}', '#{image_alt}', '#{image_width}', '#{image_height}', '#{image_class}', '#{image_url}'  %}"
      img.replace liquid_image
    end
    blog_post.update(solution: html_doc)
  end
end