SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Wizard jag5311's Avatar
    Join Date
    Jan 2003
    Location
    Somewhere in Indiana
    Posts
    3,082
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    My RSS Parser is not working (Ruby on Rails)

    Here is my current code:

    Code:
    newsFeed = (session[:user].nil?) ? 'http://blogfeed...com?feed=rss2' : 'http://blogfeed...com?feed=rss2'
    newsRss = RSS::Parser.parse(open(newsFeed){|fd|fd.read})
    @news = newsRss.items[5] if !newsRss.nil? && newsRss.items.length > 0
    
    <% if !@news.nil? -%>
            <div class="newsPost">
                <h4><%= @news.title %></h4>
                <div class="newsContent"><%= @news.content %></div>
            </div>
    <% end -%>
    The page runs, but no postings come down. I have 5 postings on the blog.

    Thanks,
    Bryan

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The code can be simplified to:

    Code:
    @news = RSS::Parser.parse('http://blogfeed...com?feed=rss2').items[0...5]
    
    <&#37; for news in @news %>
      <div class="newsPost">
        <h4><%= @news.title %></h4>
        <p><%= @news.content %></p>
      </div>
    <% end %>
    The problem with the old code is that you only select one post in newsRSS.items[5]: the sixt item (counts from 0).

    This code loops through the first five posts and displays them.

  3. #3
    SitePoint Wizard jag5311's Avatar
    Join Date
    Jan 2003
    Location
    Somewhere in Indiana
    Posts
    3,082
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Excellent!

  4. #4
    SitePoint Wizard jag5311's Avatar
    Join Date
    Jan 2003
    Location
    Somewhere in Indiana
    Posts
    3,082
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What exactly is the news in "for news"?

  5. #5
    SitePoint Wizard jag5311's Avatar
    Join Date
    Jan 2003
    Location
    Somewhere in Indiana
    Posts
    3,082
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    undefined method `title' for #<Array:0xb0d4250>   Extracted source (around line #13): 
    10: 
    11: <&#37; for news in @news %>
    12:   <div class="newsPost">
    13:     <h4><%= @news.title %></h4>
    14:     <p><%= @news.content %></p>
    15:   </div>
    That is the error I am getting.
    16: <% end %>

  6. #6
    SitePoint Wizard jag5311's Avatar
    Join Date
    Jan 2003
    Location
    Somewhere in Indiana
    Posts
    3,082
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I figured it out. I removed the @news.title and made it items.title.

    I also changed for news to be for items and said items.title

  7. #7
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry I used the wrong variable names in the code:

    Code:
    @news = RSS::Parser.parse('http://blogfeed...com?feed=rss2').items[0...5]
    
    <&#37; for news in @news %>
      <div class="newsPost">
        <h4><%= news.title %></h4>
        <p><%= news.content %></p>
      </div>
    <% end %>
    for news in @news means: iterate over @news and assign every element to the variable news.

    news is a difficult example because there is no difference between singular and plural.

    This is a better example:

    Code:
    <% for book in @books %>
    <h4><%= book.tittle %></h4>
    <% end %>
    So @books is an array containing multiple books and each book is assigned to the variable book.

    Is this understandable?

  8. #8
    SitePoint Wizard jag5311's Avatar
    Join Date
    Jan 2003
    Location
    Somewhere in Indiana
    Posts
    3,082
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Fenrir2. It works.

    I do have a 2nd part to this question.

    So if this blog is going to have 3 categories, can I pull the last 3 postings down from each category?

    So instead of this:

    Code:
    <&#37;
        @news = RSS::Parser.parse('http://news.website.com?feed=rss2').items[0...5]
    -%>
    
    <h2>News</h2>
    
    <% for items in @news %>
       <% if items.category = 'blah'
          #show last 3 postings in this category
       %>
       <% elsif items.category = 'blah2'
         #show last 3 postings in this category
       %>
       <% elsif items.category = 'blah3'
        #show last 3 postings in this category
       %>
      <div class="newsPost">
        <h4><%= items.title %></h4>
        <p><%= items.description %></p>
      </div>
    <% end %>
    I know my code isn't organized properly, but hopefully you get the drift of what I was after.

    Is that possible with pulling down that 1 rss feed?

    Thanks,
    Bryan

  9. #9
    SitePoint Wizard jag5311's Avatar
    Join Date
    Jan 2003
    Location
    Somewhere in Indiana
    Posts
    3,082
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    p.s. I am not opposed to showing 3 rss feed variables at the top

  10. #10
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd do this:

    Code:
    # ... parse rss ...
    
    # Organize the posts in categories. this code is naive so performance can be improved, but might be fast enough already.
    # The items are grouped in categories and then each category's is truncated to 3 items.
    @categories = items.group_by(&:category).map{|category| category[0...3]}
    I'm not sure if an item supports the category method; I've never used the RSS lib.

    Code:
    <&#37; for category_name, items_in_category in @categories %>
      <h1><%= category_name %></h1>
      <% for item in items_in_category %>
        <h4><%= item.title %></h4>
      <% end %>
    <% end %>
    This displays a list of categories in the RSS feed and it also lists the first 3 items in these categories.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •