Class: Html2rss::RssBuilder::Channel
- Inherits:
-
Object
- Object
- Html2rss::RssBuilder::Channel
- Defined in:
- lib/html2rss/rss_builder/channel.rb
Overview
Extracts channel information from
-
the HTML document’s <head>.
-
the HTTP response
Constant Summary collapse
- DEFAULT_TTL_IN_MINUTES =
Fallback RSS ttl (in minutes) when no cache directives are present.
360- DEFAULT_DESCRIPTION_TEMPLATE =
Description template used when no explicit or discovered description exists.
'Latest items from %<url>s'
Instance Method Summary collapse
-
#author ⇒ String?
Channel author metadata.
-
#description ⇒ String
Channel description text.
-
#image ⇒ Html2rss::Url?
Channel image URL.
-
#initialize(response, overrides: {}) ⇒ Channel
constructor
A new instance of Channel.
-
#language ⇒ String?
ISO-like language code when available.
-
#last_build_date ⇒ String, Time
Source last-modified timestamp or current time fallback.
-
#title ⇒ String
Channel title derived from overrides, document title, or URL.
-
#ttl ⇒ Integer
Cache time-to-live in minutes.
-
#url ⇒ Html2rss::Url
Canonical channel URL.
Constructor Details
#initialize(response, overrides: {}) ⇒ Channel
Returns a new instance of Channel.
18 19 20 21 |
# File 'lib/html2rss/rss_builder/channel.rb', line 18 def initialize(response, overrides: {}) @response = response @overrides = overrides end |
Instance Method Details
#author ⇒ String?
Returns channel author metadata.
67 68 69 70 71 72 73 |
# File 'lib/html2rss/rss_builder/channel.rb', line 67 def return overrides[:author] if overrides[:author] return unless html_response? parsed_body.at_css('meta[name="author"]')&.[]('content') end |
#description ⇒ String
Returns channel description text.
32 33 34 35 36 37 38 39 40 |
# File 'lib/html2rss/rss_builder/channel.rb', line 32 def description return overrides[:description] unless overrides[:description].to_s.empty? description = parsed_body.at_css('meta[name="description"]')&.[]('content') if html_response? return format(DEFAULT_DESCRIPTION_TEMPLATE, url:) if description.to_s.empty? description end |
#image ⇒ Html2rss::Url?
Returns channel image URL.
79 80 81 82 83 84 85 86 87 |
# File 'lib/html2rss/rss_builder/channel.rb', line 79 def image return overrides[:image] if overrides[:image] return unless html_response? if (image_url = parsed_body.at_css('meta[property="og:image"]')&.[]('content')) Url.sanitize(image_url) end end |
#language ⇒ String?
Returns ISO-like language code when available.
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/html2rss/rss_builder/channel.rb', line 54 def language return overrides[:language] if overrides[:language] if (language_code = headers['content-language']&.match(/^([a-z]{2})/)) return language_code[0] end return unless html_response? parsed_body['lang'] || parsed_body.at_css('[lang]')&.[]('lang') end |
#last_build_date ⇒ String, Time
Returns source last-modified timestamp or current time fallback.
76 |
# File 'lib/html2rss/rss_builder/channel.rb', line 76 def last_build_date = headers['last-modified'] || Time.now |
#title ⇒ String
Returns channel title derived from overrides, document title, or URL.
24 25 26 |
# File 'lib/html2rss/rss_builder/channel.rb', line 24 def title @title ||= fetch_title end |
#ttl ⇒ Integer
Returns cache time-to-live in minutes.
43 44 45 46 47 48 49 50 51 |
# File 'lib/html2rss/rss_builder/channel.rb', line 43 def ttl return overrides[:ttl] if overrides[:ttl] if (ttl = headers['cache-control']&.match(/max-age=(\d+)/)&.[](1)) return ttl.to_i.fdiv(60).ceil end DEFAULT_TTL_IN_MINUTES end |
#url ⇒ Html2rss::Url
Returns canonical channel URL.
29 |
# File 'lib/html2rss/rss_builder/channel.rb', line 29 def url = @url ||= Html2rss::Url.from_absolute(@response.url) |