#---------------------------------------------------------------# # # # (C) Copyright Rubysophic Inc. 2007-2008 # # All rights reserved. # # # # Use, duplication or disclosure of the code is not permitted # # unless licensed. # # Dynamic Application Discovery # # # # Last Updated: 6/03/08 # #---------------------------------------------------------------# # # # RubyRunRSS provides the interfaces for creating RSS channel # # and adding items to it. # # # #---------------------------------------------------------------# require 'rss/maker' class RubyRunRSS RUBYRUN_RSS_VERSION = '2.0' RUBYRUN_RSS_CHANNEL_URL = 'http://www.rubysophic.com' RUBYRUN_RSS_IMAGE_TITLE = 'Learn more about RubyRun from Rubysophic' RUBYRUN_RSS_IMAGE_URL = 'http://www.rubysophic.com/images/logo.jpg' RUBYRUN_RSS_FOLDER = 'rubyrun_rss' RUBYRUN_RSS_PERF_SUMMARY_CHANNEL_TITLE = 'RubyRun: %s Performance Summary' RUBYRUN_RSS_PERF_SUMMARY_CHANNEL_ITEM_FILENAME = 'perf_summary_item' RUBYRUN_RSS_PERF_SUMMARY_CHANNEL_DESCRIPTION = 'RubyRun delivers up-to-the-minute performance summary of your application.' RUBYRUN_RSS_PERF_SUMMARY_CHANNEL_FILENAME = 'perf_summary.xml' RUBYRUN_RSS_PERF_SUMMARY_ITEM_TITLE = 'Report for %s at %s' RUBYRUN_RSS_PERF_SUMMARY_ITEM_DESCRIPTION = 'Performance summary of %s (%s) at %s' # Constructor of the class. # RSS XML file and HTML files will be kept in the directory as specified def initialize (title, description, directory, rss_filename, html_filename) @title = title @description = description @directory = directory @rss_filename = rss_filename @html_filename = html_filename @apps_name = Rails::Configuration.new.root_path.split('/').last @rss_xml_destination = "#{@directory}/#{@rss_filename}" create_channel_content unless File::exists?(@rss_xml_destination) @rss = load_rss_content end # Create the RSS channel def create_channel_content content = RSS::Maker.make(RUBYRUN_RSS_VERSION) do |m| m.channel.title = sprintf(@title, @apps_name) m.channel.link = RUBYRUN_RSS_CHANNEL_URL m.channel.description = @description m.items.do_sort = true # sort items by date m.image.title = RUBYRUN_RSS_IMAGE_TITLE m.image.width = 140 m.image.height = 25 m.image.url = RUBYRUN_RSS_IMAGE_URL end File.open(@rss_xml_destination,"w") do |f| f.write(content) end content.to_s end # Add an item to the RSS channel def add_item (title, description, html_content) filename = "#{@html_filename}_#{Time.now.to_i}#{rand(1000000)}.html" File.open("#{@directory}/#{filename}", 'w') { |file| file.puts html_content } item = RSS::Rss::Channel::Item.new item.title = sprintf(title, "#{$rubyrun_startup_id_type} #{$rubyrun_startup_id}",Time.now.strftime("%H:%M:%S")) item.description = sprintf(description, @apps_name, "#{$rubyrun_startup_id_type} #{$rubyrun_startup_id}", Time.now.strftime("%H:%M:%S")) item.link = "http://#{$rubyrun_host_with_port}/#{RUBYRUN_RSS_FOLDER}/#{filename}" sleep(rand(3)) @rss = load_rss_content remove_old_item(@rss) if @rss.items.length == $rubyrun_report_shift_age @rss.items << item File.open(@rss_xml_destination,"w") do |f| f.write(@rss) end end private # Load the existing RSS XML file. Create a new channel if the channel is blank def load_rss_content content = "" # raw content of rss feed will be loaded here open(@rss_xml_destination) do |s| content = s.read end content = create_channel_content if content == '' RSS::Parser.parse(content, false) end # Remove the last item in the RSS channel def remove_old_item(rss) item = rss.items.shift filename = "#{@directory}/#{item.link.split('/').last}" File.delete(filename) if File.exists?(filename) end end