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, line 52
def discover_feed(url, fallback = false)
  unless url.nil? || url.empty? || url == 'about:blank'
    load_feed(url) { |doc|
      doc.xpath(ALTERNATE_XPATH).each { |link|
        if link[:type] =~ FEED_RE && href = link[:href]
          return href =~ URI_RE ? href : begin
            base = doc.at_xpath('//base')
            URI.join(base && base[:href] || url, href).to_s
          end
        end
      }

      doc.xpath(FACEBOOK_XPATH).each { |node|
        return FACEBOOK_FEED + node[:id][/\d+/]
      } if url.include?('facebook.com')
    }
  end

  url if fallback
end
dump_feeds(store, key, value) click to toggle source
# File lib/rss2mail/util.rb, line 95
def dump_feeds(store, key, value)
  store.transaction { store[key] = value }
end
load_feed(url) { |doc| ... } click to toggle source
# File lib/rss2mail/util.rb, line 73
def load_feed(url)
  doc = Nokogiri.HTML(open_feed(url))
rescue => err
  warn "Unable to load feed `#{url}': #{err} (#{err.class})"
else
  block_given? ? yield(doc) : doc
end
load_feeds(feeds_file, options = { deserialize_symbols: true }) click to toggle source
# File lib/rss2mail/util.rb, line 89
def load_feeds(feeds_file, options = { deserialize_symbols: true })
  SafeYAML::Store.new(feeds_file, {}, options).tap { |store|
    def store.get(key); transaction(true) { self[key] }; end
  }
end
open_feed(url, options = {}, tries = 10, &block) click to toggle source
# File lib/rss2mail/util.rb, line 81
def open_feed(url, options = {}, tries = 10, &block)
  open(url, options.merge('User-Agent' => USER_AGENT), &block)
rescue RuntimeError => err
  raise unless err.to_s.include?('redirection loop') && (tries -= 1) > 0
  sleep 10 - tries
  retry
end