In one of projects we have encounter a need to check updates from many blogs and download their entries. The first thought which came into my mind was to use feedjira and so did we. In this entry I would like to show how to use this gem.

Before we start please feel free to download the sample app.

Little theory

Many of todays websites uses technologies like RSS or atom in order to keep visitors up to date with news while not making them visit the site all the time.

Downloading entries from feed

Fetching and parsing entries from feed using feedjira is really simple, lets take a look at an example from feedjira documentation and get some entries:

 feed = Feedjira::Feed.fetch_and_parse 'http://feedjira.com/blog/feed.xml'
    entries = feed.entries

We can obtain data from entries with ease try calling:

entries.each do |entry|
      puts entry.url
      puts entry.published
      puts entry.author
      puts entry.content
    end

Error handling

Feedjira offers a nice way to distinguish where fetching and parsing was successful, usingcallbacks. I think code below is self-explanatory.

eedjira::Feed.fetch_and_parse(
      url,
      on_success: download_success,
      on_failure: download_failure
    )

Take a closer look at what other arguments you can use at feedjira rdoc

Putting it inside rails app

When using feedjira we would like to put entries inside our database. In order to do this I used following method.

def self.create_entry_from_feed(entry)
      new_entry = Entry.new(title: entry.title,
                            author: entry.author,
                            content: check_content(entry),
                            published: entry.published,
                            url: entry.url,
                            )
      new_entry
    end

This method accepts entry object taken from feed.

Putting all together

Lets take a look at my blog.rb in which everything I mentioned before is put together.

def download_all_entries!
      Feedjira::Feed.fetch_and_parse(
        url,
        on_success: download_success,
        on_failure: download_failure
        )
    end

    def download_success
      lambda { |url, feed|
        feed.entries.each do |entry|
          add_new_entry_from_feed(entry)
        end
      }
    end

    def download_failure
      lambda { |curl, err|
        logger.error "Downloading #{curl} failed due to #{error}"
      }
    end

    def add_new_entry_from_feed(entry)
      db_entry = Entry.create_entry_from_feed(entry)
      db_entry.blog_id = id
      db_entry.save
    end

Updating feed

So I downloaded entries but I would like to update the feed, what to do?

def update_feed!
      @newest_entry = entries.order(published: :desc).first
      download_success_date = lambda { |url, feed|
        next unless !@newest_entry || entry.published > @newest_entry.published
        add_new_entry_from_feed(entry)
      }
      Feedjira::Feed.fetch_and_parse(
        url,
        on_success: download_success_date,
        on_failure: download_failure
      )
    end

I use similar method as in previous example but with slightly different on_success callback – instead of adding all entries I only add those, which ale newer than the newest one in database.

Conclusion

I hope I showed how to use feedjira in a proper way. Please feel free to play with sample app and ask questions. Thanks for your attention.

Post tags:

Join our awesome team
Check offers

Work
with us

Tell us about your idea
and we will find a way
to make it happen.

Get estimate