Class: Seo::HardcodedImageToLiquid

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

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



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/services/seo/hardcoded_image_to_liquid.rb', line 3

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 unless image_url.present?
      next unless (URI.parse(image_url).host.include?('warmlyyours') rescue false)
      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