Class: Image::ImagekitUrlReplacer

Inherits:
BaseService show all
Defined in:
app/services/image/imagekit_url_replacer.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(articles: nil, limit: nil) ⇒ Object



3
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
32
33
34
35
# File 'app/services/image/imagekit_url_replacer.rb', line 3

def process(articles: nil, limit: nil)
  logger.tagged('Image::ImagekitUrlReplacer') do
    articles ||= Article.where("solution like '%img.warmlyyours.com%'")
    articles = articles.limit(limit) if limit.present?
    results = []
    errors = []
    index = 0
    total = articles.size
    articles.each do |article|
      index += 1
      puts "[#{index}/#{total}] Evaluating article #{article.id}"
      html_fragment = article.solution

      r = Image::LegacyImageParamTranslator::REGEXP_FULL_URL
      # The nature of the gsub regexp block combo is we get the url as a string
      # So we have to rescan it to extract its components
      html_changed = false
      new_frag = html_fragment.gsub(r) do |url|
        updated_url = Image::LegacyImageParamTranslator.translate_from_url(url)
        results << "[Article:#{article.id}][Match:#{index}] #{url} was translated to #{updated_url}"
        html_changed = true
        updated_url
      end
      if html_changed
        article.update_columns(solution: new_frag, updated_at: Time.current)
      end
    end
    puts "Results:\n----------\n"
    puts results.join("\n")
    puts "\nErrors:\n----------\n"
    puts errors.join("\n")
  end
end