Class: Seo::LegacyUrlMigrator

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

Overview

Rewrites legacy product and support URLs in HTML content to canonical
hierarchical paths.

Legacy patterns handled:
/products/code/:sku[/reviews|/faqs] -> /:canonical_path[/reviews|/faqs]
/products/line/:url[/reviews|/faqs] -> /:canonical_path[/reviews|/faqs]
/support/sku/:sku -> /:canonical_path/support
/support/:main_pl/:sub_pl -> /:canonical_path/support
/support/:main_pl -> /:canonical_path/support

Links may be absolute (https://www.warmlyyours.com/{locale}/...)
or relative (/{locale}/...). The rewrite preserves whichever form
was used and keeps the locale prefix intact.

Usage:
result = Seo::LegacyUrlMigrator.new.process(html_string)
result.html_out # rewritten HTML
result.links_migrated # [{ from: old_href, to: new_href }, ...]
result.skipped # [{ href: ..., reason: ... }, ...]

Defined Under Namespace

Classes: Result

Constant Summary collapse

WY_HOSTNAME_PATTERN =
/\Awww\.warmlyyours\./i
LOCALE_PATTERN =
%r{/([a-z]{2}-[A-Z]{2}|\{\{[\s]*locale[\s]*\}\})}
SUPPORT_STATIC_SLUGS =
%w[search installation request articles].freeze
LEGACY_PATTERNS =
[
  { regex: %r{/products/code/([^/]+?)(/reviews|/faqs)?$},  type: :sku },
  { regex: %r{/products/line/([^/]+?)(/reviews|/faqs)?$},  type: :product_line },
  { regex: %r{/support/sku/([^/]+)$},                      type: :support_sku },
  { regex: %r{/support/([^/]+)/([^/]+)$},                  type: :support_sub_pl },
  { regex: %r{/support/([^/]+)$},                          type: :support_main_pl },
].freeze

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



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
# File 'app/services/seo/legacy_url_migrator.rb', line 40

def process(html_fragment)
  return Result.new if html_fragment.blank?

  doc = Nokogiri::HTML::DocumentFragment.parse(html_fragment)
  migrated = []
  skipped  = []

  doc.css('a[href]').each do |link|
    href = link['href'].to_s
    next if href.blank?

    result = migrate_href(href)
    case result
    in { migrated: new_href }
      migrated << { from: href, to: new_href }
      link['href'] = new_href
    in { skipped: reason }
      skipped << { href: href, reason: reason }
    in { unchanged: true }
      next
    end
  end

  Result.new(html_out: doc.to_html, links_migrated: migrated, skipped: skipped)
end