def Currency.convert( to, from, amount=1 )
amount = amount.to_f if amount.kind_of?( String )
amount = 1 if amount == 0 || amount.nil?
return amount.to_f if from == to
yahoo = URI.parse( CURRENCY_URL )
host = yahoo.host
path = yahoo.path
Net::HTTP::Proxy( *Finance::proxy_data ).start( host, 80 ) do |http|
response = http.get( path + "?from=%s&to=%s&submit=Convert" %
[ from, to ] )
body = response.body
body =~ %r{
<td\sclass="yfnc_tabledata1">
<a\shref="/q\?s=#{from}#{to}=X">#{from}#{to}=X</a>
</td>
<td\sclass="yfnc_tabledata1"><b>1</b></td>
<td\sclass="yfnc_tabledata1">[^<]+</td>
<td\sclass="yfnc_tabledata1">(\d+\.\d+)</td>
}x
exchange_rate = $1.to_f
if exchange_rate == 0
raise RateError, "Exchange rate could not be determined."
end
exchange_rate * amount
end
end