Water 5-Getting Started and Examples-Level 1 Examples
Stock Quote Example

Stock Quote Example

This code let's you retrieve and format a set of stock quotes. You must be connect to the net for this example to work. The guts of this example is just retireving the string representing the content of a url. Here we get the quote for the stock symbol 'ABC'.
<resource "http://finance.yahoo.com/d/quotes.csv?s=ABC&f=sl1d1t1c1ohgv"/>.content
The returned string from this url is a a bunch of comma-separated values. make_objects_from_csv expects multiple lines, each of which has a bunch of comma separate values. We are getting back just one line so we just take the first element, which is a vector, out of the returned vector.
<resource "http://finance.yahoo.com/d/quotes.csv?s=ABC&f=sl1d1t1c1ohgv"/>.content.
  <make_objects_from_csv/>.0.0
"ABC"
In the vector for the stock quote, the first element is the name of the stock, (in this case "ABC"). The second element is its price, the third element is the date, etc. Next we use the above code in a method that takes as an argument the name of the stock we want to get information on. This hides the gory url and let's us just provide the symbol for the stock we're interested in. This method returns a returns a vector.
<method get_quote_for_symbol a_symbol="ABC">
  <resource
   <join "http://finance.yahoo.com/d/quotes.csv?s=" a_symbol "&f=sl1d1t1c1ohgv"
   />
  />.content.<make_objects_from_csv/>.0
</>
<get_quote_for_symbol "TRW"/>.0
"TRW"
Now we're ready to write our method that takes as an argument all the stock symbols we're interested in. It loops over these symbols. For each one, we retrieve its data by calling get_quote_for_symbol, then format that data using join on strings and html tags. The result is a mini-web page with horizontal lines between the data for each stock in our input vector.
<method format_quotes symbols=req=vector>
  symbols.<for_each combiner=join>
    <set stock_data=<get_quote_for_symbol value/>/>
    <join "Ticker: " 
           stock_data.0 <BR />
          "Close: $" stock_data.1 <BR />
         <HR />
     />
  </for_each>
</method>

<format_quotes <vector "GEF" "HAS" "SUN"/>/>