module RSS2Mail::Util

Constants

ALTERNATE_XPATH
FACEBOOK_FEED
FACEBOOK_XPATH
FEED_RE
URI_RE
USER_AGENT

Public Instance Methods

discover_feed(url, fallback = false) click to toggle source

cf. <www.rssboard.org/rss-autodiscovery>

   # File lib/rss2mail/util.rb
52 def discover_feed(url, fallback = false)
53   unless url.nil? || url.empty? || url == 'about:blank'
54     load_feed(url) { |doc|
55       doc.xpath(ALTERNATE_XPATH).each { |link|
56         if link[:type] =~ FEED_RE && href = link[:href]
57           return href =~ URI_RE ? href : begin
58             base = doc.at_xpath('//base')
59             URI.join(base && base[:href] || url, href).to_s
60           end
61         end
62       }
63 
64       doc.xpath(FACEBOOK_XPATH).each { |node|
65         return FACEBOOK_FEED + node[:id][/\d+/]
66       } if url.include?('facebook.com')
67     }
68   end
69 
70   url if fallback
71 end
dump_feeds(store, key, value) click to toggle source
   # File lib/rss2mail/util.rb
95 def dump_feeds(store, key, value)
96   store.transaction { store[key] = value }
97 end
load_feed(url) { |doc| ... } click to toggle source
   # File lib/rss2mail/util.rb
73 def load_feed(url)
74   doc = Nokogiri.HTML(open_feed(url))
75 rescue => err
76   warn "Unable to load feed `#{url}': #{err} (#{err.class})"
77 else
78   block_given? ? yield(doc) : doc
79 end
load_feeds(feeds_file, options = { deserialize_symbols: true }) click to toggle source
   # File lib/rss2mail/util.rb
89 def load_feeds(feeds_file, options = { deserialize_symbols: true })
90   SafeYAML::Store.new(feeds_file, {}, options).tap { |store|
91     def store.get(key); transaction(true) { self[key] }; end
92   }
93 end
open_feed(url, options = {}, tries = 10, &block) click to toggle source
   # File lib/rss2mail/util.rb
81 def open_feed(url, options = {}, tries = 10, &block)
82   open(url, options.merge('User-Agent' => USER_AGENT), &block)
83 rescue RuntimeError => err
84   raise unless err.to_s.include?('redirection loop') && (tries -= 1) > 0
85   sleep 10 - tries
86   retry
87 end