Class: RssFeed::Parser

Inherits:
Object show all
Defined in:
lib/rss_feed/parser.rb

Overview

The Parser class is responsible for parsing RSS feeds.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(feed_urls, options = {}) ⇒ Parser

Initialize the Parser with a list of feed URLs.

Parameters:

  • feed_urls —

    String The URLs of the RSS feeds to parse.



19
20
21
22
23
24
25
# File 'lib/rss_feed/parser.rb', line 19

def initialize(feed_urls, options = {})
  @feed_urls = feed_urls
  @xml_parser = options.fetch(:xml_parser, Nokogiri)
  @uri_parser = options.fetch(:uri_parser, URI)
  @timeout = options.fetch(:timeout, 10) # Default timeout: 10 seconds
  @logger = options[:logger]
end

Instance Attribute Details

#feed_urls ⇒ Object (readonly)

Returns the value of attribute feed_urls.



14
15
16
# File 'lib/rss_feed/parser.rb', line 14

def feed_urls
  @feed_urls
end

#uri_parser ⇒ Object (readonly)

Returns the value of attribute uri_parser.



14
15
16
# File 'lib/rss_feed/parser.rb', line 14

def uri_parser
  @uri_parser
end

#xml_parser ⇒ Object (readonly)

Returns the value of attribute xml_parser.



14
15
16
# File 'lib/rss_feed/parser.rb', line 14

def xml_parser
  @xml_parser
end

Instance Method Details

#parse ⇒ Hash

Parse the RSS feeds and extract channel and item information.

Returns:

  • (Hash) —

    The parsed channel and item information.



30
31
32
33
34
35
36
37
38
39
# File 'lib/rss_feed/parser.rb', line 30

def parse
  document = fetch_and_parse_xml(feed_urls)
  channel = RssFeed::Feed::Channel.new(document)
  channel_info = extract_channel_info(channel)

  items = RssFeed::Feed::Item.new(document)
  item_info = extract_item_info(items)

  { 'channel' => channel_info, 'items' => item_info }
end

#parse_as_object ⇒ Object



41
42
43
# File 'lib/rss_feed/parser.rb', line 41

def parse_as_object
  DynamicObject.new(parse)
end