Class: Image::BlogLiquidImageFixer
- Inherits:
-
BaseService
- Object
- BaseService
- Image::BlogLiquidImageFixer
- Defined in:
- app/services/image/blog_liquid_image_fixer.rb
Constant Summary collapse
- PATTERN =
/{% image_tag (.*?)%}/
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
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'app/services/image/blog_liquid_image_fixer.rb', line 4 def process(articles: nil, limit: nil) logger.tagged('Image::BlogLiquidImageFixer') do articles ||= Article.where("solution like '%{\% image_tag%'") 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 html_changed = false new_frag = html_fragment.gsub(PATTERN) do |liquid_tag| match_data = liquid_tag.scan(PATTERN)&.first&.first args = CSV.parse(match_data.gsub(/\s+"/, '"')).flatten.map(&:to_s).map(&:squish).map(&:presence) rescue nil args ||= match_data.split(',').map(&:to_s).map(&:squish).map(&:presence) # The last bits after position 5 seem to have the info we need = {} image_id = args[0].presence [:alt] = args[1].presence [:width] = args[2].presence [:height] = args[3].presence [:class] = args[4].presence img_url = args[5..].join if img_url.starts_with?('https://ik.warmlyyours.com') ik_url = img_url else #legacy most likely, convert it ik_url = Image::LegacyImageParamTranslator.translate_from_url(img_url) end # Let's check this image loads up and we have the right slug transform,slug,format,version = ik_url.scan(/https:\/\/ik.warmlyyours.com\/?(\S*)?\/img\/(\S*)\.(\w{3,4})\?(\S*)$/)&.first # Image lookup # First hardcode some slug love slug = { 'be-radiant-may-2016-0bb2ff' => 'be-radiant-may-2016-4484c6', 'holiday-sales-warmlyyours-39eceb' => 'holiday-sales-warmlyyours-c52d63', 'heated-floor-install-5fd1aa' => 'heated-floor-install-563aed' }[slug] || slug begin img = Image.friendly.find(slug) # url [:'data-image-id'] = img.id img_file_name = img.ik_file_name # url_file_name url_file_name = "#{slug}.#{format}" if url_file_name != img_file_name ik_url.gsub!(url_file_name, img_file_name) end rescue ActiveRecord::RecordNotFound => exc errors << "[Article:#{article.id}][Match:#{index}] #{liquid_tag} was translated to #{ik_url} but slug #{slug} is missing!" end # Todo, find the image in our db and populate the tag and other info if needed updated_tag = ApplicationController.helpers.image_tag(ik_url, **.compact) # Test the url # http_status = HTTP.head(ik_url).status #if http_status == 200 results << "[Article:#{article.id}][Match:#{index}] #{liquid_tag} was translated to #{updated_tag}" html_changed = true updated_tag #else # errors << "[Article:#{article.id}][Match:#{index}] #{liquid_tag} was translated to #{updated_tag} but HTTP status is #{http_status}" # liquid_tag # Return original #end 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 |