Ruby: How to set feedjira configuration options?

266 Views Asked by At

In the Feedjira 2.0 announcement blog post, it says that if you want to set the user agent, that should be a configuration option, but it is not clear how to do this. Ideally, I would like to mimic the options previously provided in Feedjira 1.0, including user_agent, if_modified_since, timeout, and ssl_verify_peer.

http://feedjira.com/blog/2014/04/14/thoughts-on-version-two-point-oh.html

With Feedjira 1.0, you could set those options by making the following call (as described here):

feed_parsed = Feedjira::Feed.fetch_and_parse("http://sports.espn.go.com/espn/rss/news", {:if_modified_since => Time.now, :ssl_verify_peer => false, :timeout => 5, :user_agent => "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.94 Safari/537.36"})

The only example I have seen where configuration options are set was from a comment in a github pull request, which is as follows:

Feedjira::Feed.configure do |faraday|
    faraday.request :user_agent, app: "MySite", version: APP_VERSION
end

But when I tried something similar, I received the following error:

undefined method `configure' for Feedjira::Feed:Class
1

There are 1 best solutions below

0
Evan Appleby On BEST ANSWER

It looks like a patch was added to allow a timeout option to be passed to the fetch_and_parse function: https://github.com/feedjira/feedjira/pull/318/commits/fbdb85b622f72067683508b1d7cab66af6303297#diff-a29beef397e3d8624e10af065da09a14

However, until that is pushed live, a timeout and an open_timeout option can be passed by bypassing Feedjira for the fetching and instead using Faraday (or any library that can fetch HTTP requests, like Net::HTTP). You can also set ssl verify to false, and set the user agent, such as this:

require 'feedjira'
require 'pp'

url = "http://www.espn.com/espnw/rss/?sectionKey=athletes-life"

conn = Faraday.new :ssl => {:verify => false}
response = conn.get do |request|
    request.url url
    request.options.timeout = 5
    request.options.open_timeout = 5
    request.headers = {'User-Agent' => "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.94 Safari/537.36"}
end
feed_parsed = Feedjira::Feed.parse response.body
pp feed_parsed.entries.first

I haven't seen a way to check for "if_modified_since", but I will update answer if I do.