Class: Oembed::ContentRefreshService

Inherits:
Object
  • Object
show all
Defined in:
app/services/oembed/content_refresh_service.rb

Overview

Service to refresh oEmbed content in blog posts

Scans posts for oEmbed markers and re-fetches content:

  • data-wy-oembed="faq" - FAQ sections with JSON-LD schema
  • data-wy-oembed="product" - Product embeds with pricing
  • data-wy-oembed="video" / wy-video-embed - Video embeds (wrapper + play overlay)
  • data-wy-oembed="image" / wy-image-embed - Image embeds (URLs, srcset, caption)

Product embeds are special:

  • Prices are updated to reflect current catalog prices
  • Products that become discontinued/unavailable are hidden
  • Locale-aware pricing for US/Canada

Usage:
service = Oembed::ContentRefreshService.new
result = service.refresh_all
result = service.refresh_post(post)

Constant Summary collapse

OEMBED_TYPES =
%w[faq product video image].freeze

Instance Method Summary collapse

Constructor Details

#initializeContentRefreshService

Returns a new instance of ContentRefreshService.



25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/services/oembed/content_refresh_service.rb', line 25

def initialize
  @stats = {
    posts_scanned: 0,
    posts_updated: 0,
    faqs_refreshed: 0,
    products_refreshed: 0,
    products_hidden: 0,
    videos_refreshed: 0,
    images_refreshed: 0,
    errors: []
  }
end

Instance Method Details

#refresh_allHash

Refresh all posts with oEmbed content

Returns:

  • (Hash)

    Statistics about the refresh operation



40
41
42
43
44
45
46
# File 'app/services/oembed/content_refresh_service.rb', line 40

def refresh_all
  posts_with_oembed.find_each do |post|
    refresh_post(post)
  end

  @stats
end

#refresh_post(post) ⇒ Boolean

Refresh oEmbed content in a single post

Parameters:

  • post (Post)

    The post to refresh

Returns:

  • (Boolean)

    true if post was updated



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/services/oembed/content_refresh_service.rb', line 51

def refresh_post(post)
  @stats[:posts_scanned] += 1

  source = post_solution_html_for_refresh(post)
  return false if source.blank?

  updated_body = refresh_oembed_content(source)
  return false if updated_body == source

  persist_refreshed_post_solution!(post, updated_body)
  @stats[:posts_updated] += 1
  Rails.logger.info "[OembedRefresh] Updated post #{post.id}: #{post.subject}"
  true
rescue StandardError => e
  @stats[:errors] << "Post #{post.id}: #{e.message}"
  Rails.logger.error "[OembedRefresh] Error refreshing post #{post.id}: #{e.message}"
  false
end