ruby-on-railsrubyfeedjira

How to get Feedjira to follow redirects?


I am using Feedjira's fetch_and_parse method to parse RSS feeds in my app, but some RSS feeds that I feed it are feed URLS that redirect to another feed URL, and Feedjira doesn't follow these redirects and my fetch_and_parse fails. Is there a way to get Feedjira to follow RSS redirects?


Solution

  • I ran into this same problem, and solved it by manually fetching feeds (using Faraday) and calling FeedJira::Feed.parse with the resulting XML:

    def fetch(host, url)
      conn = Faraday.new(:url => host) do |faraday|
        faraday.use FaradayMiddleware::FollowRedirects, limit: 3
      end
    
      response = conn.get url
      return response.body
    end
    
    xml = fetch("http://www.npr.org", "/templates/rss/podcast.php?id=510298")
    podcast = Feedjira::Feed.parse(xml)
    

    You could also monkey-patch Feedjira's connection method to do the same thing, though I wouldn't recommend it:

    module Feedjira
      class Feed
        def self.connection(url)
          Faraday.new(url: url) do |conn|
            conn.use FaradayMiddleware::FollowRedirects, limit: 3
          end
        end
      end
    end
    
    podcast = Feedjira::Feed.fetch_and_parse("http://www.npr.org/templates/rss/podcast.php?id=510298")