module Orange#:nodoc: module Acts#:nodoc: module Feeder #:nodoc: # included is called from the ActiveRecord::Base # when you inject this module def self.included(base) #:nodoc: # Add acts_as_roled availability by extending the module # that owns the function. base.extend AddActsAsFeeder end # this module stores the main function and the two modules for # the instance and class functions module AddActsAsFeeder#:nodoc: def acts_as_feeder class_eval do extend Orange::Acts::Feeder::SingletonMethods end include Orange::Acts::Feeder::InstanceMethods end end module SingletonMethods#:nodoc: end # Istance methods module InstanceMethods # doing this our target class # acquire all the methods inside ClassMethods module # as class methods. def self.included(aClass)#:nodoc: aClass.extend ClassMethods end module ClassMethods # Check if the url passed is a feed links or contains a feed link # returns diferents object depends of what option was passed # # option: # * :only_verify : return true if is a feed url or the url page contains a feed url link # * :feed_url :return a feed url if is a feed url or a feed url link if it's contatined in the url page # * :xml : return the xml feed and the feed url in this form: {:file => file , :feed_url => feed_url} # def is_feed? (url , option=:feed_url) return FeedFetcher::FeedFetcher.is_feed? (url , option) end end #class methods #Parse the feed_url and return the feed model # #option: #* :feed_url (set with this value when you pass the feed url) #* :xml (set with this value when you pass the hash with the xml feed file and feed url) # #want_save: If is set to true, saves the model returned def parse_feed (url_or_xml , option = :feed_url, want_save = false) case option when :feed_url then real_url = url_or_xml xml_file = FeedFetcher::FeedFetcher.fetch_page url_or_xml when :xml then real_url = url_or_xml[:feed_url] xml_file = url_or_xml[:file] end feed_parsed = FeedFetcher::FeedFetcher.parse xml_file , option self.attributes = {:real_url => real_url ,:title => feed_parsed.title ,:description => feed_parsed.description , :last_updated => feed_parsed.last_updated} self.save if want_save end # Get the new feed entries and saves. # # option: # * :new Get entries for a new feed created and saves # * :update Checks if exists news entries for a feed and saves def get_entries (option = :new) feed_parsed = FeedFetcher::FeedFetcher.parse self.real_url for entry in feed_parsed.entries.reverse case option when :new then save_entry entry self.last_updated = entry.date_published self.save when :update then if is_new? entry save_entry entry self.last_updated = entry.date_published self.save end end end end # _Alias_: get_entries (:update) def update_entries get_entries (:update) end protected # check if the entry is new def is_new? entry self.last_updated < entry.date_published end # Saves the entry and the urls (links or permalinks) def save_entry entry tmp = Entry.new (:date_published => entry.date_published ,:title => entry.title , :description =>entry.description ,:content => entry.content ,:authors => entry.authors ,:feed_id => self.id) tmp.save for url in entry.urls url_temp = Url.new (:url => url ,:entry_id => tmp.id) url_temp.save end end end #instance methods end#feeder end#acts end#orange