Module: Html2rss::Syndication::Parser

Defined in:
lib/html2rss/syndication/parser.rb

Overview

Parses RSS 2.0 and Atom documents into AutoSource article hashes.

Class Method Summary collapse

Class Method Details

.parse(body, base_url: nil) ⇒ Array<Hash{Symbol => Object}>

Parameters:

  • body (String)

    raw RSS/Atom document

  • base_url (String, Html2rss::Url, nil) (defaults to: nil)

    fallback base for relative item links

Returns:

  • (Array<Hash{Symbol => Object}>)


26
27
28
29
30
31
32
33
34
# File 'lib/html2rss/syndication/parser.rb', line 26

def parse(body, base_url: nil)
  feed = RSS::Parser.parse(body.to_s, false)
  return [] unless feed

  items_from(feed, base_url:).filter_map { |item| article_hash(item, base_url:) }
rescue RSS::Error => error
  Log.warn("Syndication::Parser: failed to parse feed (#{error.class}: #{error.message})")
  []
end

.parse_response(response) ⇒ Array<Hash{Symbol => Object}>

Returns article hashes (may be empty).

Parameters:

Returns:

  • (Array<Hash{Symbol => Object}>)

    article hashes (may be empty)

Raises:

  • (ArgumentError)

    when the response is not a syndication feed



16
17
18
19
20
# File 'lib/html2rss/syndication/parser.rb', line 16

def parse_response(response)
  raise ArgumentError, 'response is not a syndication feed' unless response.feed_response?

  parse(response.body, base_url: response.url)
end