#!/usr/bin/env ruby
#
# raa2xml.rb -- get RAA data as XML/RSS.
# written by Maki (maki@rubycolor.org)
#
# 2001/09/21 support RDF RSS 1.0 and add RAA module.
# 2001/09/12 support RSS.
# 2001/09/11 initial release.
$PROXY = ENV["http_proxy"] || nil
$LOGDIR = "."
$LOGFILE = $LOGDIR + "/" + "raa2xml.log"
require 'soap/driver'
module RAAUtils
## from cgi.rb
def url_escape(string)
string.gsub(/([^ a-zA-Z0-9_.-]+)/n) do
'%' + $1.unpack('H2' * $1.size).join('%').upcase
end.tr(' ', '+')
end
def url_unescape(string)
string.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n) do
[$1.delete('%')].pack('H*')
end
end
end
##------------------------------------------------------------------
#
# RAA module --- from sample/RAA/iRAA.rb of SOAP4R/1.3.7
#
# * add to_xml methods.
# * add to_rss methods.
#
module RAA
include RAAUtils
InterfaceNS = "http://www.ruby-lang.org/xmlns/soap/interface/RAA/0.0.1"
RAA_LIST_URL = 'http://www.ruby-lang.org/en/raa-list.html'
class Category
include SOAP::Marshallable
@@typeNamespace = InterfaceNS
attr_reader :major, :minor
def initialize( major, minor = nil )
@major = major
@minor = minor
end
def to_s
"#{ @major }/#{ @minor }"
end
def to_xml
"\n#{@major}#{@minor}\n\n"
end
def to_rss091
""
end
def to_rss
"\n#{@major}#{@minor}\n\n"
end
def ==( rhs )
if @major != rhs.major
false
elsif !@minor or !rhs.minor
true
else
@minor == rhs.minor
end
end
end
class Product
include SOAP::Marshallable
@@typeNamespace = InterfaceNS
attr_reader :name
attr_accessor :version, :status, :homepage, :download, :license, :description
def initialize( name, version = nil, status = nil, homepage = nil, download = nil, license = nil, description = nil )
@name = name
@version = version
@status = status
@homepage = homepage
@download = download
@license = license
@description = description
end
def to_xml
"
#{@name}\n" +
"#{@license}\n" +
"#{@version}\n" +
"#{@status}\n" +
"#{@homepage}\n" +
"#{@download}\n" +
"#{@description}\n"
end
def to_rss091
"#{@name}\n" +
"#{@description}\n" +
"#{@download}\n"
end
def to_rss
"#{@name}\n" +
"#{@license}\n" +
"#{@version}\n" +
"#{@status}\n" +
"#{@homepage}\n" +
"#{@download}\n" +
"#{@description}\n"
end
end
class Owner
include SOAP::Marshallable
@@typeNamespace = InterfaceNS
attr_reader :id
attr_accessor :email, :name
def initialize( email, name )
@email = email
@name = name
@id = "#{ @email }-#{ @name }"
end
def to_xml
"\n#{@name}#{@email}\n\n"
end
def to_rss091
""
end
def to_rss
"#{@name}\n"
end
end
class Info
include SOAP::Marshallable
@@typeNamespace = InterfaceNS
attr_accessor :category, :product, :owner, :update
def initialize( category = nil, product = nil, owner = nil, update = nil )
@category = category
@product = product
@owner = owner
@update = update
end
def about
"#{RAA_LIST_URL}?name=#{url_escape(@product.name)}"
end
def to_xml
"\n"+
@category.to_xml +
@product.to_xml +
@owner.to_xml +
"#{@update}\n" +
"\n"
end
def to_rss091
"- \n" +
@category.to_rss091 +
@product.to_rss091 +
@owner.to_rss091 +
"
\n"
end
def to_rss
"- \n" +
@category.to_rss +
@product.to_rss +
@owner.to_rss +
"#{@update}\n" +
"
\n"
end
end
Methods = {
'getAllListings' => [ 'Array' ],
'getProductTree' => [ 'Hash' ],
'getInfoFromCategory' => [ 'Array', 'category' ],
'getModifiedInfoSince' => [ 'Array', 'time' ],
'getInfoFromName' => [ 'Info', 'name' ],
}
end
##------------------------------------------------------------------
include RAA
server = 'http://www.ruby-lang.org/~nahi/soap/raa/'
class RAA2XMLClient < Application
private
AppName = 'RAA2XML'
def initialize( server, proxy )
super( AppName )
@server = server
@proxy = proxy
@logDev = open($LOGFILE, File::WRONLY | File::APPEND | File::CREAT)
@logId = Time.now.gmtime.strftime( "%Y-%m-%dT%X+0000" )
@drv = nil
@day = nil
end
def usage()
STDERR.print "usage: ruby raa2xml.rb [-rss|-rss091] [day]\n"
exit()
end
def getopt()
usage() if ARGV.length < 1
arg = ARGV.shift
if arg == "-rss"|| arg == "-rss091"
@rss = arg
arg = ARGV.shift
end
usage() if ARGV.length > 0
@day = arg.to_i
end
def add_raamethod()
# Method definition.
RAA::Methods.each do | method, params |
@drv.addMethod( method, *( params[1..-1] ))
end
end
def get_items()
# get new RAA data and print it.
t = Time.at( Time.now.to_i - @day * 3600 * 24)
# @drv.getAllListings.each{|item|
return @drv.getModifiedInfoSince( t ).collect{|item|
item
}
end
def run()
getopt()
@log.sevThreshold = SEV_WARN
@drv = SOAP::Driver.new( @log, @logId, RAA::InterfaceNS, @server, @proxy )
add_raamethod()
items = get_items()
case @rss
when "-rss091"
print_rss091(items)
when "-rss"
print_rss(items)
else
print_xml(items)
end
end
def print_rss(items)
items_list = items.sort{|a,b| b.update <=> a.update}.collect{|item|
name = url_escape(item.product.name)
"\n"
}.to_s
items_str = items.sort{|a,b| b.update <=> a.update}.collect{|item|
item.to_rss
}.to_s
pub_date = Time.now.gmtime.strftime("%Y-%m-%dT%H:%M:%S+00:00")
print <<-EOB
Ruby Application Archive
http://www.ruby-lang.org/en/raa.html
list of Ruby Application Archive
en-us
#{pub_date}
RubyColor.org
#{items_list}
#{items_str}
EOB
end
def print_rss091(items)
items_str = items.sort{|a,b|a.update <=> b.update}.collect{|item|
item.to_rss091
}.to_s
print <
Ruby Application Archive
http://www.ruby-lang.org/en/raa.html
en
#{items_str}
EOB
end
def print_xml(items)
items_str = items.sort{|a,b|a.update <=> b.update}.collect{|item|
item.to_xml
}.to_s
print "\n"
print "\n"
print items_str
print "\n"
end
###
## Other utility methods
#
def log( sev, message )
@log.add( sev, "<#{ @logId }> #{ message }", @appName ) if @log
end
end
app = RAA2XMLClient.new( server, $PROXY ).start()