<commit_before>class Feed < ActiveRecord::Base
  # Contains many entries
  has_many :entries, class_name: 'FeedEntry', dependent: :destroy

  # Can be subscribed to by users 
  has_many :subscriptions
  has_many :postboxes, through: :subscriptions

  # Fetch latest entries after feed creation
  after_create :fetch

  ##
  # Get new entries from feed URL, create entries
  #
  # [todo] - integrate Feedjira update method to parse faster
  def fetch
    feed = Feedjira::Feed.fetch_and_parse(self.url)
    
    self.update!(title: feed.title) if self.title.nil?

    feed.entries.each do |entry|
      unless self.entries.exists? :original_id => entry.id
        self.entries.create!(
          :title         => entry.title,
          :summary      => entry.summary,
          :url          => entry.url,
          :published_at => entry.published,
          :original_id  => entry.id
        )
      end
    end
  end

  ##
  # Update all existing feeds
  #
  # [todo] - send owning user as a param
  #
  def self.fetch_all
    Feed.all.each do |feed|
      feed.fetch
    end
  end

  ##
  # Return feed subscribers
  #
  def subscribers 
    return self.postboxes
  end 

end<commit_msg>Feedjira error handling<commit_after>class Feed < ActiveRecord::Base
  # Contains many entries
  has_many :entries, class_name: 'FeedEntry', dependent: :destroy

  # Can be subscribed to by users 
  has_many :subscriptions
  has_many :postboxes, through: :subscriptions

  # Fetch latest entries after feed creation
  after_create :fetch

  ##
  # Get new entries from feed URL, create entries
  #
  # [todo] - integrate Feedjira update method to parse faster
  def fetch
    feed = Feedjira::Feed.fetch_and_parse(self.url)
    
    # Handle empty feed fetching
    unless feed.is_a?(Fixnum)
      self.update!(title: feed.title) if self.title.nil?

      feed.entries.each do |entry|
        unless self.entries.exists? :original_id => entry.id
          self.entries.create!(
            :title         => entry.title,
            :summary      => entry.summary,
            :url          => entry.url,
            :published_at => entry.published,
            :original_id  => entry.id
          )
        end
      end
    else
      logger.error "[ERROR] Feed expected, got Fixnum instead (in Feed#fetch)"
    end

  end

  ##
  # Update all existing feeds
  #
  # [todo] - send owning user as a param
  #
  def self.fetch_all
    Feed.all.each do |feed|
      feed.fetch
    end
  end

  ##
  # Return feed subscribers
  #
  def subscribers 
    return self.postboxes
  end 

end
{'fraction_non_alphanumeric': 0.08928571428571429, 'fraction_numerical': 0.0, 'mean_word_length': 2.3038461538461537, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3347670', 'n_tokens_mistral': 813, 'n_tokens_neox': 772, 'n_words': 228}