Class: Html2rss::RssBuilder::Channel

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

Overview

Extracts channel information from

  1. the HTML document’s <head>.

  2. 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

Constructor Details

#initialize(response, overrides: {}) ⇒ Channel

Returns a new instance of Channel.

Parameters:



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

#authorString?

Returns channel author metadata.

Returns:

  • (String, nil)

    channel author metadata



67
68
69
70
71
72
73
# File 'lib/html2rss/rss_builder/channel.rb', line 67

def author
  return overrides[:author] if overrides[:author]

  return unless html_response?

  parsed_body.at_css('meta[name="author"]')&.[]('content')
end

#descriptionString

Returns channel description text.

Returns:

  • (String)

    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

#imageHtml2rss::Url?

Returns channel image URL.

Returns:



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

#languageString?

Returns ISO-like language code when available.

Returns:

  • (String, nil)

    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_dateString, Time

Returns source last-modified timestamp or current time fallback.

Returns:

  • (String, Time)

    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

#titleString

Returns channel title derived from overrides, document title, or URL.

Returns:

  • (String)

    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

#ttlInteger

Returns cache time-to-live in minutes.

Returns:

  • (Integer)

    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

#urlHtml2rss::Url

Returns canonical channel URL.

Returns:



29
# File 'lib/html2rss/rss_builder/channel.rb', line 29

def url = @url ||= Html2rss::Url.from_absolute(@response.url)