Class: Image::DragonflyMediaUrlReplacer
- Inherits:
-
BaseService
- Object
- BaseService
- Image::DragonflyMediaUrlReplacer
- Defined in:
- app/services/image/dragonfly_media_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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'app/services/image/dragonfly_media_url_replacer.rb', line 3 def process(articles: nil, limit: nil) logger.tagged('Image::DragonflyMediaUrlReplacer') do dt = DragonflyTranslator.new articles ||= Article.where("solution like '%/media/%'") 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 # e.g src="/media/W1siZiIsIjIwMTUvMDYvMDEvOTdib2Fuc3JhY19WaWV3aW5nX3ZpZGVvc19vbl90YWJsZXQucG5nIl0sWyJwIiwidGh1bWIiLCI2MDB4NjAwXHUwMDNlIl1d/2c06076272df86f5/Viewing_videos_on_tablet.jpg" # should return #1. W1siZiIsIjIwMTUvMDYvMDEvOTdib2Fuc3JhY19WaWV3aW5nX3ZpZGVvc19vbl90YWJsZXQucG5nIl0sWyJwIiwidGh1bWIiLCI2MDB4NjAwXHUwMDNlIl1d/2c06076272df86f5 #2. #3. Viewing_videos_on_tablet #4. jpg r = /\/media\/(\S+)(\/\S*)?\/(\w+)\.?([a-zA-Z]{3})?/ html_changed = false new_frag = html_fragment.gsub(r) do |match| puts "match: #{match}" begin # DragonflyTranslator will process the url result = dt.process(match, image_datetime: article.created_at) results << [article.id, match, result] html_changed = true rescue StandardError => exc errors << [article.id, match, exc.to_s] result = match # keep original end result 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 |