Sporkmonger

purveyor of fabulously ambiguous eating utensils

Tutorial

Posted by sporkmonger
Written August 11th, 2005

Since a tutorial was requested for setting FeedTools up with Rails, I thought I’d do a quick example. It’s really, really not hard. I packaged the whole thing up and put it on RubyForge, and maybe someday I’ll polish it up. Probably not until I have support for subscriptions in place. Not really worth it until then.

Anyhow, without further ado, I give you… your really short tutorial.

Ok, first off, you’re going to want to start loading the feed as early as possible. Ideally, you should start loading it within a thread at the beginning of the controller’s action, do whatever other activities you need to, then join on the thread. This is especially true if you’re loading multiple feeds at once. In the interests of simplicity though, we’re only going to load one feed, and since the feed is the only thing we care about in this action, we’ll just skip the threading bit.

So our ‘view’ action will simply look like this:
1
2
3
4

def view
  @feed = FeedTools::Feed.open(@params['feed_url'])
end

We have a form as part of our layout that points to the view action and passes off a feed_url, so you can pick whatever feed you want to display.

In the view, it’s a simple matter of displaying the feed’s attributes and looping through the feed items and then displaying their attributes as well.

Take a look:
1
2
3
4
5
6
7
8
9
10
11
12
13
14

    <div class="contentBox">
      <h1><a href="<%= h @feed.link %>">
               <%= @feed.title %></a></h1>
      <p><%= @feed.description %></p><%
for feed_item in @feed.items %>
      <div class="feedItemBox">
        <h2><a href="<%= h feed_item.link %>">
                 <%= feed_item.title %></a></h2>
        <p><%= feed_item.description %></p>
      </div><%
end
      %>
    </div>

The second part of the tutorial is even simpler. Just for fun I threw in some of the xml building capabilities. If you switch over to the ‘xml’ action, you can translate any feed to another variety of xml feed. Right now, RSS 1.0, 2.0, and Atom 0.3 are the only valid options, but eventually the xml generation should run the whole gamut of varieties.

Here’s the ‘xml’ action:
1
2
3
4
5

def xml
  @feed_type = @params['id']
  @feed = FeedTools::Feed.open(@params['feed_url'])    
end
And… heh, you’re gonna love this… Here’s the ‘xml’ view from the xml.rxml file:
1
2

@feed.build_xml((@feed_type or "atom"), 0.0, xml)

That’s all there is to it.

You’ll probably notice in the code that I disabled the cache because I wanted to have the thing be configuration-free, but feel free to point the database.yml file to your own database and turn caching back on.

The hardcore XHTML fans should also be happy to discover that I chose to deliver the content for this tutorial as XHTML 1.1 with the application/xhtml+xml content type. Thanks to Tidy, all of the nasty-looking html from the feed gets polished up, and everything works out perfectly.

Also, FeedTools 0.2.3 is out. It’s just a hotfix for a nasty bug that several people have pointed out, where get parameters were ignored.

Update: Martin Dittus wrote a really nice introduction to FeedTools, focussing on how to get it working in a non-Rails project.

Update: Apparently, threads aren’t so hot when a database-backed cache is involved because it opens up tons of extra connections to the database.

  1. James James :
    Written December 8th, 2005 at 11:50 PM

    What, actually, would one want to use this for? Republishing feed content?

    The example seems focused on reassuring Railer Nubies rather than exposing the general value of the tool for general Ruby hacking. Are there more examples forthcoming?

  2. Written December 9th, 2005 at 12:53 PM

    Hmm, what would you use it for? I doubt you would want to use the tutorial code for anything in the real world. That wasn’t the point. The point was to demonstrate that it’s not nearly as difficult to use as some people thought it was.

    At the time I wrote it, people were wondering where all the documentation was, and the point I was trying to make with the tutorial was mostly that there wasn’t a pressing need for extensive documentation, because the library itself had such a simple interface.

    This article was written quite a while ago. Since then, documentation has gotten a lot more complete, and there’s a lot more example code floating around that would probably be a better starting point for a new user. I just linked one such example at the end of the article.

    And yes, more examples will likely show up from time to time, but as more people start to use it, you should expect that an increasing number of those examples will be coming from not-me. When good ones do appear, I’ll try to make sure to link to them.

  3. Vladare Vladare :
    Written December 28th, 2005 at 07:41 AM

    Excellent, But I have two questions concerning this framework. Is the feed_tool thread safe ? Can you add to feed_tool a support for http proxy?

    Thank You

  4. Written December 28th, 2005 at 01:23 PM

    Regard thread safety: To the best of my knowledge, threading should not be an issue. However, there are known to be major issues with trying to use the caching system within threads. If you wrote your own custom caching system, everything should be ok.

    Regarding http proxying: There’s no reason why it couldn’t happen, but it’s not something I think many people need. Meanwhile there are lots of other features that are needed by lots of people. I don’t really have the time to implement http proxy support myself, but you are quite welcome to create it and submit a patch. That’s the beauty of open source! If you were going to do this, take a look at the open-uri source code. I know there’s some good code in there for dealing with http proxies.

  5. Warren Warren :
    Written February 21st, 2006 at 03:52 PM

    Bob, how you include this into your code? i’ve tried require ‘feedtools’ but not luck

  6. Written February 21st, 2006 at 09:25 PM

    Close.

    1
    2
    
    
    require 'feed_tools'
    
  7. Written April 29th, 2006 at 06:22 AM

    Hi Bob,

    I am fighting to get FeedTools (0.2.24) to work from within a Rails App. Parsing feeds works fine, but I cannot enable caching. I used the provided migration file, my db is mysql.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    
    
    # script/console
    # Loading development environment.
    
    require 'rubygems'
    require_gem 'feedtools'
    
    FeedTools::DatabaseFeedCache.set_up_correctly?()
    # => true
    
    FeedTools::DatabaseFeedCache.table_exists?
    # => true
    
    FeedTools::DatabaseFeedCache.connected?
    # => true
    
    FeedTools::DatabaseFeedCache.initialize_cache()
    # => nil
    
    FeedTools::feed_cache_connected?
    # => false
    
    

    the logfile just says ‘select id, href, title, link, feed_data, feed_data_type, http_headers, last_retrieved from cached_feeds limit 1’ a few times.

    Any ideas what could be wrong? thanks Frank

  8. Written April 29th, 2006 at 01:37 PM

    Add this to environment.rb:

    1
    2
    3
    
    
    FeedTools.configurations[:feed_cache] =
      "FeedTools::DatabaseFeedCache"
    

    The feed cache is off by default. You have to turn it on.

  9. mathie mathie :
    Written May 2nd, 2006 at 07:22 AM

    Ooh, nice. Thanks for producing feedtools—you’ve just made a project I’m working on soo much easier. :)

    I’m hoping that my cunning plan of having thin wrapper models (for example the Feed model will just have the feed url itself, the rest will be pulled from the feed itself) along with cached feeds will work quite nicely and still perform OK… Perhaps even with a cron `script/runner refresh_all_feeds` to decrease the likelihood of a browser request catching an expired feed…

  10. Written May 2nd, 2006 at 09:07 AM

    That’s largely what the FeedUpdater is for. As for your plan, yes, it will work just fine, but it will be slow. My recommendation currently is to decide ahead of time which fields you need, create columns in your tables for each of them, and copy them over when the feed is retrieved so that the user’s page request doesn’t ever even touch FeedTools. If you were going to implement it like that, you’d also obviously need a FeedEntries model as well.

  11. adam@macrofiche.com adam@macrofiche.com :
    Written July 23rd, 2006 at 04:29 PM

    I’m so glad I found feed tools. I got it installed and seems to be configured properly as Rails is calling all neccesary files as it tries to load a feed. Then it bottoms out with error “Cannot retrieve feed using invalid URL:” My URL is relative and searching the excellent documentation I see this isn’t a problem. Is there something I am missing?

  12. Written July 23rd, 2006 at 05:17 PM

    Adam: It’s a problem if you try to load the feed with a relative path, it’s not a problem if the feed itself contains relative paths.

  13. Written August 9th, 2006 at 10:33 PM

    Hi, thanks for your library… most examples I found were aimed at loading an RSS feed and then using the data from it. I quite what to do the opposite. simply creating an RSS feed.

    so I followed your quick howto on http://rails.techno-weenie.net/question/2005/12/25/how_can_i_generate_rss_fees_in_a_rails_app

    it works as is in IRB but in my APP, I keep getting.

    Cannot build feed, missing feed unique id.

    although @feed.id returns a string and @feed.link returns the URL of my blog.

    what’s wrong?

  14. ern ern :
    Written August 12th, 2006 at 08:15 AM

    completely useless tutorial. utter crap.

  15. Written August 12th, 2006 at 12:19 PM

    On the off-chance that the above comment isn’t actually spam, I’m going to leave it there. (I’m getting a lot of context-sensitive spam that doesn’t make sense.)

  16. Written August 12th, 2006 at 01:09 PM

    Somekool:

    Try:
    1
    2
    3
    4
    5
    
    
    require 'uuidtools'
    feed.id = UUID.sha1_create(
      UUID_URL_NAMESPACE,
      feed.link).to_uri_string
    
  17. Shawn Shawn :
    Written August 15th, 2006 at 06:48 PM

    I’m writing a rails app that consumes feeds. I found FeedTools, which seems to be just what I’m looking for, but I think I might be missing something.

    I need to put each element of a feed item into a column in a separate table. I can see, from the API doc, how to get things like item description, item title, item link, but how do you get other stuff? For example, digg.com has diggCount in their feed, how would I go about pulling that element out? Can FeedTools do this with a built-in, or do i need to access item.feed_data and parse it out myself with a regexp or some other library?

  18. Written August 16th, 2006 at 01:47 AM

    Shawn:

    Use feed.find_node("xpath query") to get at those nodes. You’ll have to do some of the work, but most of the hard stuff (namespaces, etc) is done for you with the find_node method.

  19. Shawn Shawn :
    Written August 16th, 2006 at 11:33 PM

    Thank you for your quick reply! That helps a ton.

    Great work on FeedTools!

  20. Byron Byron :
    Written August 20th, 2006 at 03:25 PM

    Hey, many thanks for this. I’m just getting my feet wet with RoR and little by little it gets more exciting.

    After spending a few hours looking at various ways to consume RSS feeds and weighing the options I installed FeedTools and had it up and running in about 10 minutes.

    Great, great work, thanks again!

  21. Kevin Kevin :
    Written August 29th, 2006 at 04:53 PM

    How can I limit the number of items that are displayed from a feed?

  22. Written August 30th, 2006 at 01:51 AM

    Kevin:

    Just slice the entries array like you would any other array:

    feed.entries[0..9]

  23. jayjee jayjee :
    Written September 3rd, 2006 at 12:02 PM

    Cannot retrieve feed using invalid URL:

    for http://feeds.feedburner.com/Techcrunch

    How is this URL invalid, or how should I represent it for feedtools?

  24. Written September 3rd, 2006 at 01:09 PM

    jayjee:

    Can’t duplicate:

    require 'feed_tools'
    feed = FeedTools::Feed.open(
      "http://feeds.feedburner.com/Techcrunch")
    => #<FeedTools::Feed:0x1364d98 URL:http://feeds.feedburner.com/Techcrunch>
    FeedTools::URI.parse(
      "http://feeds.feedburner.com/Techcrunch")
    => #<FeedTools::URI:0x133d978 URL:http://feeds.feedburner.com/Techcrunch>
    URI.parse(
      "http://feeds.feedburner.com/Techcrunch")
    => #<URI::HTTP:0x133be02 URL:http://feeds.feedburner.com/Techcrunch>
    

    Perhaps you should update to the latest version? I may have fixed the bug you’re seeing.

  25. jayjee jayjee :
    Written September 4th, 2006 at 11:49 AM

    Perfect.

    Amazing product you have here.

  26. werd werd :
    Written September 4th, 2006 at 12:54 PM

    Could you perhaps get a little more in depth regarding how to limit the number of headlines (feed items)? I’m totally new to Rails, so I’m sorry for the dumb question.

    Where/how should slice the array?

  27. werd werd :
    Written September 4th, 2006 at 12:56 PM

    Nevermind! Awesome!

  28. werd werd :
    Written September 4th, 2006 at 04:06 PM

    I’m trying to do this with 3-4 feeds. I just duplicated the @feed variable, changing it to @feed_two, etc, and changing the code within the div to reflect it (again, basically just duplicating).

    Is this what I should be doing in order to display multiple feeds? I ask because I’m currently in development, have a cashed_feeds table set up according to the SQL schema file, but there’s no way they’re caching—it’s still taking forever to load a friggin’ page.

    But no exceptions are being thrown.

  29. werd werd :
    Written September 4th, 2006 at 04:24 PM

    No, they’re caching, but it’s honestly taking forever for a page to load.

  30. werd werd :
    Written September 4th, 2006 at 04:26 PM

    Also getting this error in Terminal:

    FeedTools may have been loaded improperly. This may be caused by the presence of the RUBYOPT environment variable or by using load instead of require. This can also be caused by missing the Iconv library, which is common on Windows.

  31. Written September 4th, 2006 at 06:18 PM

    werd:

    It’s taking forever to load because FeedTools is slow. Not much that can be done about that without rewriting it. However, most people who are using it in any kind of production environment get around this by using FeedTools in conjunction with FeedUpdater, which is a daemon that sits in the background and does the heavy lifting, then copies the desired information back to the database after parsing has occurred.

    As for the error, did you make sure that iconv is available?

  32. werd werd :
    Written September 4th, 2006 at 06:25 PM

    How can be sure that iconv is available?

    Thank you so much for your help by the way.

  33. werd werd :
    Written September 4th, 2006 at 06:27 PM

    And how do I exactly get feedupdater working?

  34. Written September 4th, 2006 at 06:40 PM

    werd:

    1
    2
    
    
    require 'iconv'
    

    If that errors out, iconv is missing and needs to be present. You should easily find the information for getting iconv installed if you Google for it.

    FeedUpdater is a non-trivial system and is more difficult to get going, but you should find the information you need if you search for FeedUpdater on my site.

  35. werd werd :
    Written September 6th, 2006 at 11:12 AM

    Should I include “require ‘iconv’” in the same controller that I declare the @feed variables?

    Also, you mentioned that most users work around the slow speeds by using FeedUpdater in production environments. Does that mean it doesn’t work in development?

    Thanks.

  36. Written September 6th, 2006 at 05:23 PM

    In this context, “production” simply means “on a live site that’s publicly facing”. FeedUpdater doesn’t care what your Rails environment is set to.

    Regarding iconv, I intended for you to simply type that into IRB. If it errors out, that’s your problem. If it doesn’t, you have some other problem.

  37. Chris Chris :
    Written September 19th, 2006 at 06:03 PM

    Bob, thanks for making FeedTools. It’s been great for reading feeds, but I’ve noticed that, for writing outgoing feeds, performance when calling feed.build_xml for a feed with 100 entries takes on the order of 30 seconds to run on a high-end multicore development machine. e.g. in rhtml where @feed has 100 entries: <%= @feed.build_xml(“rss”, 2.0) %>

    Is there something I can do (configuration, parameter, etc) to speed xml creation up? Thanks again.

  38. Written September 20th, 2006 at 02:33 AM

    Chris:

    Hmmm… that is an awefully long time… The only thing I can think of that might be causing that is if you weren’t creating the feed from scratch, because then the generation code also has to parse at the same time. That said, FeedTools in general is extremely slow due to a dependancy on REXML, and isn’t really in active development anymore. I intend to do a complete rewrite in C eventually, but it’s a long way off. In the meantime, if you want to do some performance improvements, you are quite welcome to do so. Patches are always welcome.

  39. Kevin Kevin :
    Written October 4th, 2006 at 04:56 PM

    Is there a way to specify your own expiration time for a feed so FeedTools will download updated feeds to the cache less often? I believe by default the expiration time is taken from the feed itself.

  40. Written October 4th, 2006 at 05:46 PM

    Kevin:

    Not with the current version of FeedTools. The next version will allow you to change the default time to live to something else, but that still doesn’t override the time to live values that are actually supplied by the feeds themselves.

  41. Michael Trier Michael Trier :
    Written October 30th, 2006 at 07:57 AM

    Have you considered replacing REXML with LibXML-Ruby which I understand is significantly faster and the APIs are almost plug and play. Nice work.

  42. Written October 30th, 2006 at 11:19 AM

    Michael:

    I considered it, but it’s not going to happen. I had to monkey patch REXML to get the behavior I needed in the first place. Monkey patching doesn’t work quite so well on C code.

    The plan is, once I have the time, to rewrite the whole thing from scratch in straight C, with a slightly different approach to parsing stuff, and then just give it a Ruby wrapper. But honestly, that could be a year or two off. (Unless, of course, someone decides to fund the project.)

  43. Written December 7th, 2006 at 11:31 PM

    There is no date?

    feed.date does not work? how do I get the date of a post?

  44. Written December 8th, 2006 at 12:51 AM

    somekool:

    You might want to try feed.entries[i].time.

  45. Brent Brent :
    Written December 15th, 2006 at 04:46 PM

    I’d recommend adding this to

    execute “ALTER TABLE cached_feeds CHANGE COLUMN feed_data feed_data MEDIUMTEXT

    to your FeedTools data migration for mysql. I’ve run into a number of feeds that have more than 2^16 in data.

  46. Written December 16th, 2006 at 01:01 AM

    Brent:

    Hmm, that’s a good point. I’ll see what I can do.

  47. Brent Brent :
    Written December 22nd, 2006 at 09:41 AM

    Is IDN required? My logs keep filling up with this error:

    no such file to load—idn (MissingSourceFile) /home/local/lib/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require’ /home/local/lib/site_ruby/1.8/rubygems/custom_require.rb:27:in `require’ /home/.gems/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:147:in `require’ /home/.gems/gems/feedtools-0.2.26/lib/feed_tools/helpers/uri_helper.rb:42:in `idn_enabled?’ /home/.gems/gems/feedtools-0.2.26/lib/feed_tools/helpers/uri_helper.rb:118:in `normalize_url’ /home/.gems/gems/feedtools-0.2.26/lib/feed_tools/feed_item.rb:530:in `links’ /home/.gems/gems/feedtools-0.2.26/lib/feed_tools/feed_item.rb:503:in `each’ /home/.gems/gems/feedtools-0.2.26/lib/feed_tools/feed_item.rb:503:in `links’ /home/.gems/gems/feedtools-0.2.26/lib/feed_tools/feed_item.rb:422:in `link’

  48. Written December 22nd, 2006 at 01:01 PM

    Brent:

    Blame ActiveSupport. No, IDN is not required, it’s optional, but FeedTools determines whether it’s available or not by trying to load it. If loading fails, it moves on and doesn’t worry about it. I assume ActiveSupport logs the failure anyways even though the exception is caught and handled. You can fix the log issue simply by installing IDN, I guess. You might also try seeing if there isn’t a way to change ActiveSupport’s default logging level. Are you running this in the development environment or the production environment? The log errors may not show up in production, but only development. Not sure.

  49. Brent Brent :
    Written December 22nd, 2006 at 01:56 PM

    I’m running this in a semi-production environment with logging turned down but I’m using FeedTools from a daemon and the daemon’s log is catching all of this rather than production.log. It doesn’t seem to affect application functionality however, so you’re probably right that it’s just being logged.

  50. Written December 23rd, 2006 at 03:39 PM

    I am having a problem getting FeedTools to parse. I am getting the following error message NoMethodError: undefined method `parse_xml’ for HTree:Module

    Here is an example of the problem copied from a console

    require ‘feed_tools’ => true slashdot_feed = FeedTools::Feed.open(‘http://www.slashdot.org/index.rss’) => #<feedtools::feed:0x1a75a2c /> slashdot_feed.title NoMethodError: undefined method `parse_xml’ for HTree:Module from c:/ruby/lib/ruby/gems/1.8/gems/feedtools-0.2.26/lib/feed_tools/help ers/html_helper.rb:300:in `sanitize_html’ from c:/ruby/lib/ruby/gems/1.8/gems/feedtools-0.2.26/lib/feed_tools/help ers/html_helper.rb:564:in `process_text_construct’ from c:/ruby/lib/ruby/gems/1.8/gems/feedtools-0.2.26/lib/feed_tools/feed .rb:860:in `title’ from (irb):3

    Any ideas. I used gem to install. I also uninstalled and reinstalled it. I am running on a windows xp machine with ruby 1.8.2

    Thanks for any help you can provide.

  51. Written December 23rd, 2006 at 03:57 PM

    Alex:

    I no longer target 1.8.2. Upgrade to 1.8.5 and see if that helps. Also verify that you have Iconv properly installed. (You can check by simply doing require 'iconv' in an irb session.)

  52. Written December 23rd, 2006 at 04:04 PM

    Brent:

    If you mean the FeedUpdater daemon, you can change the logging level in the config file.

  53. Written December 23rd, 2006 at 10:00 PM

    Excellent. Thanks for the quick response. Upgrading to 1.8.5 fixed the problem. And many thanks for feedtools. I’m going to see if I can get FeedUpdater working as well.

  54. Written December 24th, 2006 at 12:59 PM

    FeedUpdater won’t work on Windows XP. No support for daemons.

  55. Jason Jason :
    Written December 24th, 2006 at 11:27 PM

    From the on_update() function in the custom updater script, is it possible to access the feed’s ID record?

    Custom updater loads all feeds from feeds table. On update, it creates or updates the feed_items table. However, I need to assign feed_items.feed_id to the feed table’s ID.

  56. Written January 9th, 2007 at 10:33 AM

    Hi there and thanks a lot for your great work.

    I’m using the latest version of feet tools but I’m having problems with UTF-8 encoded feeds.

    Is there any specific settings I should use?

    I added a set_charset method with charset=utf-8” as after_filter on my application_controller but this does not solve the issues I’m having.

    If I parse an RSS feed with special chars (like ‘à’) somehow the results I get from Feed.title (as example) are badly encoded (at least that’s what I see if I do a puts feed.title.

    Thanks for any suggestion!

  57. Written January 10th, 2007 at 06:11 PM

    Andrea:

    Unfortunately, there’s no way to tell for sure what the problem is without having the feed in question.

    What’s the URI?

  58. Andrea Andrea :
    Written January 11th, 2007 at 08:24 AM

    I’m having the problem with any feed containing some italian text, like this one: http://feeds.feedburner.com/IlNissardo

  59. Railsroad Railsroad :
    Written January 12th, 2007 at 03:40 PM

    I’ve been trying to generate a RSS 2.0 feed.

    Your post here suggest that FeedTools can generate RSS 2.0 http://www.railsweenie.com/forums/1/topics/56

    However, when I looked at the code for FeedTools::Feed.build_xml

    I did not see any code that suggested support for RSS 2.0

    FeedTools::FeedItem doesn’t seem to have the all methods to support RSS 2.0 either. ( e.g. pubDate )

    Any suggestions are appreciated.

    thanks, RailsRoad

  60. Written January 12th, 2007 at 05:44 PM

    Railsroad:

    Not sure how you missed the code, it’s pretty obviously there. If you want to generate RSS 2.0, it’s as simple as:

    1
    2
    
    
    feed.build_xml("rss", 2.0)
    

    And as the API indicates, FeedTools uses the FeedItem#time and FeedItem#updated methods rather than a pubDate method. But it obtains the data from the pubDate element. So yes, it supports RSS 2.0.

  61. Jason Jason :
    Written January 30th, 2007 at 02:48 PM

    Hi Bob, Quick question – (I’d use the official forum if there was one)

    I’m trying to parse an Atom feed from Flickr, specifically trying to access the url to the image.

    <link href="http://farm1.static.flickr.com/175/372236890_5dba07aabc_o.jpg" />

    I’ve been struggling to find where in the documentation this is available.

    I assumed this would be the link() method, but link() is returning the URL from <link>

    Any help on this is greatly appreciated. Thanks!

  62. Jason Jason :
    Written January 30th, 2007 at 02:51 PM

    Woops, looks like tags are being stripped out of the comments.

    The XML tag I’m trying to access from the feed is:

    <link rel=”enclosure” type=”image/jpeg” href=”...” />

    But the link() method is returning the URL from this tag:

    <link rel=”alternate” type=”text/html” href=”...” />

  63. Written January 31st, 2007 at 07:49 AM

    Jason:

    Use the FeedItem#enclosures method or alternatively, the FeedItem#links method.

  64. MartOn MartOn :
    Written March 5th, 2007 at 09:39 AM

    Why does is not support http_proxy?

    Most corporations do not allow http access directly from clients on the inside.

    Exmaple of http_proxy would be nice.

    /MartOn

  65. Written March 5th, 2007 at 03:15 PM

    MartOn:

    It does support http proxy. Use the :proxy_address, :proxy_port, :proxy_user, and :proxy_password configuration options.

  66. infelix infelix :
    Written March 17th, 2007 at 02:51 PM

    Hi Bob,

    I just installed it and it works great!

    In a tag like:

    <enclosure>

    How can I access the URL, LENGTH or TYPE?

    Sorry for the nubyness

  67. infelix infelix :
    Written March 17th, 2007 at 02:53 PM

    The comment removed my exmaple tag, I meant:

    <enclosure url="urlhere" size+"34" type="audio/mpeg">
  68. Quentin Quentin :
    Written August 27th, 2007 at 10:35 AM

    Hi,

    I tried FeedTools with Ruby on Rails behind a proxy. I know there are proxy options and I tried to configure it but I can’t make it works. I used several method, including this one: feed = FeedTools::Feed.open(url, { :proxy_address => "http://172.16.0.30", :proxy_port => 8080 })

    and I always get this error: “Socket error prevented feed retrieval”

    My proxy address and port are good (it works with other RSS parsers), and the syntax looks ok.

    If anybody knows the exact configuration of FeedTools with a proxy, it would be great ;)

    Thanks.

  69. Quentin Quentin :
    Written August 29th, 2007 at 06:41 AM

    Ok I got it…

    The :proxy_address parameter should be called :proxy_host. Indeed, without ‘http://’, it works :)

    Thanks anyway for this great parser ;)

  70. Paul Paul :
    Written November 2nd, 2007 at 11:43 PM

    Hi, FeedTools looks like the solution for my project, but I am having a bit of an issue getting it to run. I type:

    1
    2
    3
    4
    5
    6
    
    
    require 'feed_tools'
    def view
      @feed = FeedTools::Feed.open(
        @params['http://feeds.feedburner.com/OracleAppslab'])
    end
    

    If I run the code, I get, "no such file to load feed_tools". This sounds like I don’t have the gem, but I did install FeedTools 2.26.

    Any ideas?

    paul

  71. Written November 3rd, 2007 at 10:57 PM

    Paul,

    Well, first, you need to make sure you’ve called:

    1
    2
    
    
    require 'rubygems'
    

    Assuming you’ve done that, the only other reason I can think of is that you might be running a different copy of ruby than the one you think you’re running. Try calling which ruby to check that you’re using the runtime that actually has the FeedTools gem. On OS X, it’s fairly common to see paths that have been set up in such a way that /usr/bin/ruby is the runtime being used, but you may have the gems installed for the /usr/local/bin/ruby runtime. If that doesn’t help, I don’t really know what totell you. If the gem is installed, and ubyGems has been loaded, it should work.

    That said, I think you’ve misunderstood how the code in this tutorial works. In the example above, 'feed_url' is a String key that allows you to pass a URI in through Rails’ params Hash. You’re not supposed to replace that string with the URI of the feed you want to retrieve.

    This is the code for what I assume you wanted to do:

    1
    2
    3
    4
    5
    6
    
    
    require 'rubygems'
    require 'feed_tools'
    
    feed = FeedTools::Feed.open(
      'http://feeds.feedburner.com/OracleAppslab')
    

    Hope that helps!

  72. Sunil Sunil :
    Written January 31st, 2008 at 07:40 AM

    Hi All,

    I am new to ROR. Please suggest how to find the list of keys in “item” variable in the following code.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    
    
    require 'rubygems'
    require 'feed_tools'
    feed = FeedTools::Feed.open('http://dekstop.de/weblog/index.xml')
    puts feed.title
    puts feed.description
    puts feed.author
    puts feed.link
    puts feed.generator
    puts feed.rights
    puts feed.language
    
    feed.items.each { |item|
       item.each_key { |key|
      puts key
       }
    }
    

    thanks in advance.

  73. Written January 31st, 2008 at 04:19 PM

    FeedItems aren’t Hash objects. To access the fields of an item you need to look at the API. Which apparently has bit-rotted out of existence. Use the gem server to access the API instead.

    
    gem server
    

    Then browse to http://localhost:8808/

  74. Written January 31st, 2008 at 10:40 PM

    The API has been uploaded to RubyForge now. Should be easier to just use that.

  75. Written February 16th, 2008 at 12:14 PM

    It seems FeedTools doesn’t manage UTF-8 strings very well. Look at following code. @feed.description should return “Tecnologia, cultura, costume, novità….” last word is incorrectly reported as “novitÔ.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    
    >> irb
    >> require "feed_tools"
    => true
    >> @feed=FeedTools::Feed.open("http://rockcastitalia.com/rockcastitalia.xml")
    => #<FeedTools::Feed:0x2a3d8e URL:http://rockcastitalia.com/rockcastitalia.xml>
    >> print @feed.description
    Tecnologia, cultura, costume, novità e vita quotidiana... il tutto condito con una buona dose di musica rock.
    Dopo aver iniziato questo podcast come un esperimento e poi travolto dal successo di ascolto, il Dok si barcamena da un episodio settimanale all'altro cercando abbastanza materiale da soddisfare il palato di tutti i rockers d'Italia.=> nil
    >> 
    

    Any help?

  76. Written February 17th, 2008 at 12:39 PM

    Franco, which version of FeedTools were you using?

  77. Written February 17th, 2008 at 01:18 PM

    Thanks Bob, I’m using 0.2.28 I downloaded with gem

    Macintosh:~ francosolerio$ gem list feedtools -l

    • LOCAL GEMS *

    feedtools (0.2.28) Parsing, generation, and caching system for xml news feeds. Macintosh:~ francosolerio$

  78. Written February 19th, 2008 at 10:52 AM

    Looks like feedtools formats everything in ISO-8859-1. Doing the following gives the correct result:

    1
    2
    3
    4
    
    
    @feed=FeedTools::Feed.open("http://rockcastitalia.com/rockcastitalia.xml")
    ic = Iconv.new('ISO-8859-1', 'UTF-8')
    print ic.iconv(@feed.description)
    

    (I’m using ruby 1.8.6 on OSX Leopard)

  79. Yuki Yuki :
    Written February 25th, 2008 at 11:58 AM

    I’m having the same problem with UTF-8 on Debian Testing and PostgreSQL (all in utf8). Using iconv it solves the problem

  80. Yuki Yuki :
    Written February 25th, 2008 at 12:10 PM

    The problem isn’t gone. When I do ic = Iconv.new(‘ISO-8859-1’, ‘UTF-8’) and try to insert the data in the DB says that I have a “PGERror: ERROR, the bytes secuence is not a valid codification UTF8”, because my DB needs UTF8 (but feed.title seems that is not utf8).

    Anybody knows how to solve the problem?

  81. Written February 25th, 2008 at 12:41 PM

    Unfortunately, there are obviously utf-8 and iso-8859-1 issues going on. I currently have no plans to fix these issues or to invest any further time in FeedTools, but if someone wants to submit a patch and test it, I will happily commit the patch and put out a new release.

  82. Written February 26th, 2008 at 02:01 PM

    I traced the problem in html_Helper.rb -> resolve_relative_uris() calls parse_fragment() without specifying the encoding.

    1
    2
    3
    4
    5
    6
    7
    8
    
    
    def self.resolve_relative_uris(html, base_uri_sources=[])
    ...
      if FeedTools.configurations[:sanitization_enabled]
        fragments = HTML5::HTMLParser.parse_fragment(
        html, :tokenizer => HTML5::HTMLSanitizer)
      else
    ...
    

    I’m an absolute beginner in developement, so I just hard-coded the encoding like this:

    1
    2
    3
    4
    5
    6
    7
    8
    
    
    def self.resolve_relative_uris(html, base_uri_sources=[])
    ...
      if FeedTools.configurations[:sanitization_enabled]
        fragments = HTML5::HTMLParser.parse_fragment(
        html, :tokenizer => HTML5::HTMLSanitizer, :encoding => 'UTF-8')
      else
    ...
    

    It works! I suppose the correct solution would be to retrieve the exact encoding of the original feed and submit that to parse_fragment().

  83. Julien Boulnois Julien Boulnois :
    Written February 27th, 2008 at 02:18 AM

    Franco Solerio is right, I made a small patch to fix this encoding problem. I don’t know if it’s the better way to do this, but it works for me.

    http://n0life.org/~julbouln/feedtools_encoding.patch

  84. David Lee David Lee :
    Written March 2nd, 2008 at 03:22 AM

    Hi, I was wondering why activerecord is a dependency for feedtools. Is there an automatic database interaction that isn’t mentioned in the tutorials?

    It would be nice if you could modularize feedtools into its finer components: reader, builder, and … cacher?

    That way, I don’t need to install builder and activerecord.

  85. Written March 3rd, 2008 at 06:43 PM

    Franco, Julien:

    Good catch on the encoding thing. My bad there. I’m pretty sure at that point in the parsing processing, everything should have been converted to utf-8 anyways, so Franco’s code should probably be correct as-is. I’ll give it a try and see about releasing another gem.

    David:

    Yes, there is a caching system built into FeedTools. Yes, if I was writing the library today, that would almost certainly the route I would go. However, as you will discover if you play with the actual code, it’s a big project, and it’s not something you can easily fix. Plus, I’ve effectively abandoned it, so I’m really not going to go taking on huge code reorganization efforts.

  86. Todd D Todd D :
    Written March 25th, 2008 at 12:02 PM

    Will the ‘UTF-8’ changes be packaged into a gem in the near term?

    If a gem fix won’t be available soon, is the patch the preferred method to solving the problem?

  87. Alvaro Alvaro :
    Written March 26th, 2008 at 09:01 PM

    Hi! I have a little problem when I try to show images from a feed, it works fine, the html code is correct (

    Thanks in advance!

  88. Alvaro Alvaro :
    Written March 26th, 2008 at 09:06 PM

    Oh, some problem in the last comment, sorry. The problem is show images that are inside of the description of a feed item, the html code is correct but I can’t see anything, any idea?

    Thanks in advance!

Leave a Response

NOTE: I'm afraid Javascript needs to be on in order to comment.

Comments should be formatted using Textile.

Ruby code should be enclosed within a <macro:code lang="ruby"> element. Other languages are supported. For output you can simply omit the lang attribute.