#!/usr/bin/ruby -w
#
# $Id: chkshares,v 1.3 2004/07/10 22:55:21 ianmacd Exp $
#
# A Ruby port of Perl's Finance::Quote's sample script, chkshares.pl.
#
# This program depends on Paul Rubel's FormatR module, which is available
# from http://formatr.sourceforge.net/.

require 'finance/quote'
include Finance

require 'format.rb'
include FormatR

top_ex = <<HEADER

                                 STOCK REPORT

TICKER         DATE      LAST  %CHANGE       HIGH      LOW    VOLUME     CLOSE
-------------------------------------------------------------------------------
HEADER

ex = <<MAIN
@<<<<<< @>>>>>>>>>>  @###.### @###.###   @###.### @###.### @>>>>>>>>  @###.###
stock,  date,        last,    p_change,  high,    low,     volume,    close
MAIN

body_fmt = Format.new( top_ex, ex )

abort "Usage: #{$0} market stocks" unless ARGV.size >= 2

qd = Quote.new.fetch( *ARGV )

ARGV[1..-1].each do |stock|
  stock = stock.upcase

  unless qd[stock][:success]
    warn "Look-up of stock #{stock} failed: #{qd[stock][:error_msg]}"
    next
  end

  date	    = qd[stock][:date]
  last	    = qd[stock][:last]
  p_change  = qd[stock][:p_change]
  high	    = qd[stock][:high]
  low	    = qd[stock][:low]
  volume    = qd[stock][:volume]
  close	    = qd[stock][:close]

  body_fmt.printFormat( binding )
end
