Class: Html2rss::FeedResult

Inherits:
Object
  • Object
show all
Defined in:
lib/html2rss/feed_result.rb

Overview

Note:

Gem contract: instances must round-trip through Marshal.dump / Marshal.load so web can cache one scrape and render RSS or JSON Feed on read. Loaded results are re-frozen via private #marshal_load. For trusted cache reads only, Marshal.load(payload, freeze: true) is also safe.

Closed Marshal-cacheable handle for one scrape.

Frozen consumer query/render set (do not grow without an explicit contract change): #empty?, #channel_title, #to_rss, #to_json_feed, and #status.

Non-goals: no Channel reader, no Articles reader, no peephole into scrape internals. html2rss-web and other consumers must use only this surface.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(channel:, articles:, status:, stylesheets: []) ⇒ FeedResult

Returns a new instance of FeedResult.

Parameters:

  • channel (Html2rss::Channel)

    channel metadata (internal; not exposed on the public API)

  • articles (Array<Html2rss::Article>)

    extracted articles (deduplicated; not exposed)

  • status (Html2rss::Status)

    pipeline telemetry

  • stylesheets (Array<Hash>) (defaults to: [])

    optional RSS stylesheet configs

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/html2rss/feed_result.rb', line 23

def initialize(channel:, articles:, status:, stylesheets: [])
  raise ArgumentError, 'channel must be a Html2rss::Channel' unless channel.is_a?(Channel)
  raise ArgumentError, 'status must be a Html2rss::Status' unless status.is_a?(Status)

  articles = Array(articles)
  raise ArgumentError, 'articles must all be Html2rss::Article' unless articles.all?(Article)

  @channel = channel
  @articles = articles.dup.freeze
  @status = status
  @stylesheets = freeze_stylesheets(stylesheets)
  freeze
end

Instance Attribute Details

#statusHtml2rss::Status (readonly)

Returns frozen scraper tallies, dedup count, and generator formatter. #to_h on status is a stable consumer contract for observability payloads.

Returns:

  • (Html2rss::Status)

    frozen scraper tallies, dedup count, and generator formatter. #to_h on status is a stable consumer contract for observability payloads.



73
74
75
# File 'lib/html2rss/feed_result.rb', line 73

def status
  @status
end

Instance Method Details

#channel_titleString

Channel title string only — does not expose the channel object.

Returns:

  • (String)

    title from the materialized channel



45
# File 'lib/html2rss/feed_result.rb', line 45

def channel_title = @channel.title

#empty?Boolean

Returns true when the scrape produced no items.

Returns:

  • (Boolean)

    true when the scrape produced no items



39
# File 'lib/html2rss/feed_result.rb', line 39

def empty? = @articles.empty?

#to_json_feed(feed_url: nil) ⇒ Hash

Returns JSON Feed 1.1 hash.

Parameters:

  • feed_url (String, nil) (defaults to: nil)

    optional self URL for JSON Feed (+feed_url+)

Returns:

  • (Hash)

    JSON Feed 1.1 hash



61
62
63
64
65
66
67
68
# File 'lib/html2rss/feed_result.rb', line 61

def to_json_feed(feed_url: nil)
  FeedBuilder::JsonFeed.new(
    channel: @channel,
    articles: @articles,
    feed_url:,
    user_comment: status.to_generator_comment
  ).call
end

#to_rssRSS::Rss

Returns RSS 2.0 document.

Returns:

  • (RSS::Rss)

    RSS 2.0 document



49
50
51
52
53
54
55
56
# File 'lib/html2rss/feed_result.rb', line 49

def to_rss
  FeedBuilder::Rss.new(
    channel: @channel,
    articles: @articles,
    stylesheets: @stylesheets,
    generator: status.to_generator_comment
  ).call
end