class SiteFeed include Mongoid::Document include Mongoid::Timestamps field :remote_site_url field :merge_with_category field :channel_name field :channel_title, :localize => true field :channel_key field :feed_name, localize: true field :disabled, type: Boolean, default: false field :feed_url field :feed_uid require 'feed_model/cache' include FeedModel::Cache Category.send(:include,FeedModel::Cache) before_destroy do tmp = SiteFeedAnnc.where(feed_id: self.id).first if !tmp.nil? tmp.destroy end end scope :enabled, ->{where(:disabled => false)} def get_annc main_directory = File.join("#{Rails.root}","public","site_feeds") feed_directory = File.join(main_directory.to_s, self.id.to_s) if File.exists?(feed_directory) anns = JSON.parse(File.read(File.join(feed_directory.to_s, self.feed_uid + ".json")))['announcements'] rescue [] else uri = URI(self.feed_url) res = Net::HTTP.start(uri.host, uri.port,:use_ssl => uri.scheme == 'https',open_timeout: 20,read_timeout: 20) do |http| req = Net::HTTP::Get.new(uri) http.request(req).body rescue '' end FileUtils.mkdir_p(feed_directory) if !File.exists?(feed_directory) File.open(File.join(feed_directory.to_s,self.feed_uid + ".json"),"w") do |file| res.force_encoding("utf-8") file.write(res) end anns = JSON.parse(res)['announcements'] rescue [] end anns end def category Category.find(self.merge_with_category) rescue nil end def channel_title_for_cache !self[:channel_title].to_s.empty? ? self[:channel_title] : I18n.t("feed.source") end end