Created
September 5, 2012 07:29
-
-
Save Jarred-Sumner/3632627 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Feed < ActiveRecord::Base | |
attr_accessible :name, :url, :etag, :last_modified | |
has_many :statuses, :order => "publication_date DESC" | |
def self.update_each! | |
all.each { |feed| feed.update_from_url! } | |
end | |
def self.update_or_parse_from_url!(url) | |
if @@feed = Feed.find_by_url(url) | |
@@feed.update_from_url! | |
else | |
Feed.parse_from_url(url) | |
end | |
end | |
def self.parse_from_url(url) | |
feed = Feed.new | |
rss = Feedzirra::Feed.fetch_and_parse(url) | |
feed.name = rss.title | |
feed.url = rss.feed_url | |
feed.etag = rss.etag | |
feed.last_modified = rss.last_modified.to_datetime | |
feed.save | |
rss.entries.inject { |entry| Status.update_or_create_with_entry(entry, feed.id) } | |
end | |
def update_from_url! | |
rss = Feedzirra::Feed.fetch_and_parse(url) | |
self.name = rss.title.split("Service Status").first.strip || rss.title unless rss.title.class.to_s == "Fixnum" | |
self.url = rss.feed_url | |
self.etag = rss.etag | |
self.last_modified = rss.last_modified.to_datetime | |
self.touch | |
self.save | |
rss.entries.each { |entry| Status.update_or_create_with_entry(entry, self.id) } | |
end | |
def self.last_day | |
@@time = 24.hours.ago.to_datetime | |
Status.where("publication_date > ?", @@time).collect { |status| status.feed if status.bad? }.uniq.compact.reject { |feed| true unless feed.bad? } | |
end | |
def self.last_week | |
@@time = 7.days.ago.to_datetime | |
Status.where("publication_date > ?", @@time).collect { |status| status.feed if status.bad? }.uniq.compact.reject { |feed| true unless feed.bad? } | |
end | |
def self.update_each_recursively! | |
loop do | |
update_each! | |
sleep(60) | |
end | |
end | |
def self.internet_down? | |
unless self.last_day.blank? | |
self.last_day.each do |feed| | |
return true if feed.name.include?("Elastic Compute Cloud") | |
end | |
end | |
end | |
def self.recovering? | |
unless self.last_week.blank? | |
self.last_week.each do |feed| | |
return true if feed.name.include?("Elastic Compute Cloud") | |
end | |
end | |
end | |
def self.recovering | |
@@time = 7.days.ago.to_datetime | |
Status.where("publication_date > ?", @@time).collect { |status| status.feed if status.good? }.uniq.compact.reject { |feed| true unless feed.good? } | |
end | |
def bad? | |
true if self.statuses.first.bad? | |
end | |
def good? | |
!bad? | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment