Get Feeds source with Feedjira Gem

231 Views Asked by At

I use Feedjira Gem (Rails) to fetch/grab the rss-feeds from several websites. Everything works fine, but the only that has bugged me long time is the source-feed (the website I grab the rss from).

I want to show users which website the rss comes from. Right now, I grab the whole url, but I want to only grab what is after www and before .com. Anyway I can get it work or I can grab the feed source from the rss file.

I see this in rss file, but can't grab it.

 <channel>
 <link>http://www.domain.com/</link>

And here is my whole model.

class FeedEntry < ActiveRecord::Base
  acts_as_punchable

  def self.update_from_feed(feed_url)
    feed = Feedjira::Feed.fetch_and_parse(feed_url)
    add_entries(feed.entries)
  end

  private

  def self.add_entries(entries)
    entries.each do |entry|
      unless exists? :guid => entry.id
        create!(
            :name         => entry.title,
            :url          => entry.url,
            :guid         => entry.id,
            :source       => entry.url,
            :summary      => entry.summary,
            :published_at => entry.published,
        )
      end
    end
  end
end

Thanks in advance!

1

There are 1 best solutions below

1
On BEST ANSWER

You can grab the source by

feed = Feedjira::Feed.fetch_and_parse(feed_url)
source = feed.url # -> http://www.domain.com/

To grab what is after www and before .com, you can do something like this

source = URI.parse(feed.url).host.split('.')[-2] # -> domain

Now, here we go

  def self.update_from_feed(feed_url)
    feed = Feedjira::Feed.fetch_and_parse(feed_url)
    source = URI.parse(feed.url).host.split('.')[-2]
    add_entries(feed.entries, source)
  end

  private

  def self.add_entries(entries, source)
    entries.each do |entry|
      unless exists? :guid => entry.id
        create!(
            :name         => entry.title,
            :url          => entry.url,
            :guid         => entry.id,
            :source       => source,
            :summary      => entry.summary,
            :published_at => entry.published,
        )
      end
    end
  end

It would work!