Class: Html2rss::RssBuilder::Article
- Inherits:
-
Object
- Object
- Html2rss::RssBuilder::Article
- Includes:
- Comparable, Enumerable
- Defined in:
- lib/html2rss/rss_builder/article.rb
Overview
Article is a simple data object representing an article extracted from a page. It is enumerable and responds to all keys specified in PROVIDED_KEYS.
Constant Summary collapse
- PROVIDED_KEYS =
Allowed article attributes accepted by the value object constructor.
%i[id title description url image author guid published_at enclosures categories scraper].freeze
- DEDUP_FINGERPRINT_SEPARATOR =
Separator used to build deterministic deduplication fingerprints.
'#!/'
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer?
Comparison result for compatible Article values.
- #author ⇒ String?
-
#categories ⇒ Array<String>
Normalized, unique category names.
-
#deduplication_fingerprint ⇒ String, Integer
Returns a deterministic fingerprint used to detect duplicate articles.
-
#description ⇒ String
Rendered article description.
-
#each {|key, value| ... } ⇒ Enumerator
If no block is given.
- #enclosure ⇒ Html2rss::RssBuilder::Enclosure?
-
#enclosures ⇒ Array<Html2rss::RssBuilder::Enclosure>
Normalized enclosure objects.
-
#guid ⇒ String
Generates a unique identifier based on the URL and ID using CRC32.
-
#id ⇒ String?
Stable article identifier.
- #image ⇒ Url?
-
#initialize(**options) ⇒ Article
constructor
A new instance of Article.
-
#published_at ⇒ DateTime?
Parses and returns the published_at time.
-
#scraper ⇒ Class?
Scraper class that produced this article.
-
#title ⇒ String?
Article title.
- #url ⇒ Url?
-
#valid? ⇒ Boolean
Checks if the article is valid based on the presence of URL, ID, and either title or description.
Constructor Details
#initialize(**options) ⇒ Article
Returns a new instance of Article.
33 34 35 36 37 38 39 40 41 |
# File 'lib/html2rss/rss_builder/article.rb', line 33 def initialize(**) @to_h = {} .each_pair { |key, value| @to_h[key] = value.freeze if value } @to_h.freeze return unless (unknown_keys = .keys - PROVIDED_KEYS).any? Log.warn "Article: unknown keys found: #{unknown_keys.join(', ')}" end |
Instance Method Details
#<=>(other) ⇒ Integer?
Returns comparison result for compatible Article values.
147 148 149 150 151 |
# File 'lib/html2rss/rss_builder/article.rb', line 147 def <=>(other) return nil unless other.is_a?(Article) 0 if other.all? { |key, value| value == public_send(key) ? public_send(key) <=> value : false } end |
#author ⇒ String?
85 |
# File 'lib/html2rss/rss_builder/article.rb', line 85 def = blank_string_to_nil(@to_h[:author]) |
#categories ⇒ Array<String>
Returns normalized, unique category names.
122 123 124 125 126 127 128 |
# File 'lib/html2rss/rss_builder/article.rb', line 122 def categories @categories ||= @to_h[:categories].dup.to_a.tap do |categories| categories.map! { |category| category.to_s.strip } categories.reject!(&:empty?) categories.uniq! end end |
#deduplication_fingerprint ⇒ String, Integer
Returns a deterministic fingerprint used to detect duplicate articles.
97 98 99 |
# File 'lib/html2rss/rss_builder/article.rb', line 97 def deduplication_fingerprint dedup_from_url || dedup_from_id || dedup_from_guid || hash end |
#description ⇒ String
Returns rendered article description.
64 65 66 67 68 69 70 71 72 |
# File 'lib/html2rss/rss_builder/article.rb', line 64 def description @description ||= Rendering::DescriptionBuilder.new( base: @to_h[:description], title:, url:, enclosures:, image: ).call end |
#each {|key, value| ... } ⇒ Enumerator
Returns if no block is given.
51 52 53 54 55 |
# File 'lib/html2rss/rss_builder/article.rb', line 51 def each return enum_for(:each) unless block_given? PROVIDED_KEYS.each { |key| yield(key, public_send(key)) } end |
#enclosure ⇒ Html2rss::RssBuilder::Enclosure?
108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/html2rss/rss_builder/article.rb', line 108 def enclosure return @enclosure if defined?(@enclosure) case (object = @to_h[:enclosures]&.first) when Hash @enclosure = Html2rss::RssBuilder::Enclosure.new(**object) when nil @enclosure = Html2rss::RssBuilder::Enclosure.new(url: image) if image else Log.warn "Article: unknown enclosure type: #{object.class}" end end |
#enclosures ⇒ Array<Html2rss::RssBuilder::Enclosure>
Returns normalized enclosure objects.
102 103 104 105 |
# File 'lib/html2rss/rss_builder/article.rb', line 102 def enclosures @enclosures ||= Array(@to_h[:enclosures]) .map { |enclosure| Html2rss::RssBuilder::Enclosure.new(**enclosure) } end |
#guid ⇒ String
Generates a unique identifier based on the URL and ID using CRC32.
89 90 91 |
# File 'lib/html2rss/rss_builder/article.rb', line 89 def guid @guid ||= Zlib.crc32(fetch_guid).to_s(36).encode('utf-8') end |
#id ⇒ String?
Returns stable article identifier.
58 |
# File 'lib/html2rss/rss_builder/article.rb', line 58 def id = blank_string_to_nil(@to_h[:id]) |
#image ⇒ Url?
80 81 82 |
# File 'lib/html2rss/rss_builder/article.rb', line 80 def image @image ||= Url.sanitize(@to_h[:image]) end |
#published_at ⇒ DateTime?
Parses and returns the published_at time.
132 133 134 135 136 137 138 |
# File 'lib/html2rss/rss_builder/article.rb', line 132 def published_at return if (string = @to_h[:published_at].to_s.strip).empty? @published_at ||= DateTime.parse(string) rescue ArgumentError nil end |
#scraper ⇒ Class?
Returns scraper class that produced this article.
141 142 143 |
# File 'lib/html2rss/rss_builder/article.rb', line 141 def scraper @to_h[:scraper] end |
#title ⇒ String?
Returns article title.
61 |
# File 'lib/html2rss/rss_builder/article.rb', line 61 def title = blank_string_to_nil(@to_h[:title]) |
#url ⇒ Url?
75 76 77 |
# File 'lib/html2rss/rss_builder/article.rb', line 75 def url @url ||= Url.sanitize(@to_h[:url]) end |
#valid? ⇒ Boolean
Checks if the article is valid based on the presence of URL, ID, and either title or description.
45 46 47 |
# File 'lib/html2rss/rss_builder/article.rb', line 45 def valid? !url.to_s.empty? && (!title.to_s.empty? || !description.to_s.empty?) && !id.to_s.empty? end |