feeds/app/models/site_feed.rb

49 lines
1.6 KiB
Ruby
Raw Normal View History

2015-07-07 11:51:56 +00:00
class SiteFeed
include Mongoid::Document
include Mongoid::Timestamps
field :remote_site_url
field :merge_with_category
field :channel_name
2015-11-09 17:38:30 +00:00
field :channel_title, :localize => true
2015-07-07 11:51:56 +00:00
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)
2020-04-08 12:54:44 +00:00
before_destroy do
tmp = SiteFeedAnnc.where(feed_id: self.id).first
if !tmp.nil?
tmp.destroy
end
end
2015-07-07 11:51:56 +00:00
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
2016-04-12 06:23:43 +00:00
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
2015-07-07 11:51:56 +00:00
end