1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
define_type(
'stockquote',
-namespace='yahoo_',
-prototype,
-description='Retrieves and displays delayed stock quotes from Yahoo! Finance.'
);
local('symbols' = map);
// allow add on create
define_tag('onCreate', -opt='in');
local_defined('in') ? self->add(#in);
/define_tag;
// lookup data for new symbols
define_tag('add', -req='in', -copy);
// strip any non-alphanumeric characters except commas from input
#in = string_replaceregexp(
#in,
-find='[^a-zA-Z0-9,]',
-replace=''
);
// retrieve the data
local('response') = string(
include_url('http://quote.yahoo.com/d/quote.csv?s=' + #in + '&f=sl1d1t1c&e=.txt')
);
// parse results
local('out' = map);
// normalize line endings
#response->replace('\r\n','\n')&replace('\r','\n')&removetrailing('\n');
// split into lines
local('lines') = #response->split('\n');
local('current' = string);
local('lastupdate' = null);
protect;
iterate(#lines, local('line'));
local('linedata') = #line->split(',');
iterate(#linedata, local('i'));
#i->removeleading('"')&removetrailing('"');
select(loop_count);
case(1);
#out->insert(#i = map);
#current = #i;
case(2);
#out->find(#current)->insert('price' = decimal(#i));
case(3);
#lastupdate = #i;
case(4);
#lastupdate += ' ' + #i;
#lastupdate = date(#lastupdate, -format='%D %-H:%M%p');
#out->find(#current)->insert('lastupdate' = #lastupdate);
#lastupdate = null;
case(5);
#out->find(#current)->insert('change' = #i);
#current = '';
/select;
/iterate;
/iterate;
iterate(#out->keys, local('i'));
self->symbols->insert(#i = #out->find(#i));
/iterate;
/protect;
/define_tag;
// returns the data for the requested symbol
define_tag('getsymbol', -req='in');
return(@self->symbols->find(#in));
/define_tag;
// on convert, return a simple pair array of symbols = prices
define_tag('onConvert');
local('out' = array);
iterate(self->symbols->keys, local('i'));
#out->insert(#i = self->symbols->find(#i)->find('price'));
/iterate;
#out->sort;
return(#out);
/define_tag;
// this avoids a recursion error due to the use of _unknowntag
define_tag('symbols');
return(@self->'symbols');
/define_tag;
// allows lookups by symbol directly
define_tag('_unknownTag');
return(@self->getsymbol(tag_name));
/define_tag;
/define_type;
|