Source of currencyExchange.stt

<?stt
    |url fetchRate form|

    url := 'http://se.finance.yahoo.com/m5?s=USD&t=EUR'.

    fetchRate := [
	|response page lines regEx rate|

	response := HTTPInterface get:url.
	response isErrorResponse ifFalse:[
	    page := response data.
	    lines := page asCollectionOfLines.
	    lines := lines select:[:l | l includesString:'USDEUR'].

	    regEx := '.+USDEUR.+<td>([0-9]+.[0-9]+)</td>.+' asRegex.
	    lines := lines select:[:l | regEx matches:l].
	    lines size == 1 ifTrue:[
		(regEx matches:lines first) ifTrue:[
		    rate := FixedPoint readFrom:(regEx subexpression:2).
		]
	    ]
	].
	rate
    ].
?>

<!-- $Header: /cvs/stx/stx/goodies/webServer/data/comancheSTT/currencyExchange.stt,v 1.7 2003/11/20 09:12:48 penk Exp $ 
  --
  -- Inspired by a similar example found on www.smlserver.org
  --
  -- asks for an amount,
  -- sends a POST to the currency rate service at se.finance.yahoo.com,
  -- extracts and presents the rate.
  -->

<!-- $Header: /cvs/stx/stx/goodies/webServer/data/comancheSTT/currencyExchange.stt,v 1.7 2003/11/20 09:12:48 penk Exp $ -->

<html>
  <head>
    <title>Currency exchange</title>
  </head>

  <body>
    <h1>Currency exchange</h1>
    <p>

<?stt
    request postDataFields notEmptyOrNil 
    ifTrue:[
	|amountUSD amountEUR factor|

	amountUSD := Number fromString:((request postDataFieldAt:#amount) ? '')  onError:nil.
	amountUSD notNil ifTrue:[
	    factor := fetchRate value.
	    factor isNil ifTrue:[
		"cannot contact yahoo"
		request reportServiceUnavailable:'Cannot contact currency service at http://se.finance.yahoo.com'.
	    ].
	    amountEUR := factor * amountUSD.
?>
	    <?stt = amountUSD ?> dollar<?stt = (amountUSD ~= 1 ifTrue:'s' ifFalse:'') ?> (USD) is currently 
	    <?stt = amountEUR ?> Euro<?stt = (amountEUR ~= 1 ifTrue:'s' ifFalse:'') ?> (EUR).
<?stt
	]
    ].
?>
    <form method=post action="/stt/currencyExchange.stt"> 
      <b>Dollar amount</b> <input type="text" name="amount">
      <p>
      <input type="submit" value="Value in Euro">
    </form>

    <p>
    <hr>
    <p>
    Credits: 
    <br>
	Inspired by a similar example found on <A HREF="http://www.smlserver.org">http://www.smlserver.org</A>.
    <br>
	Financial Service by: <a href="http://se.finance.yahoo.com">http://se.finance.yahoo.com</a>
    <p>
    <hr>
    <address>Served by your friendly <a href=http://www.exept.de>ST/X WebServer</a></address>
  </body>
</html>

  

Served by your friendly ST/X WebServer