-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMtgoxTickerV0.ragel
77 lines (59 loc) · 1.91 KB
/
MtgoxTickerV0.ragel
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
// Generated using:
// ragel -J -o src/com/appspot/btcticker/fsm/MtgoxTickerV0.java MtgoxTickerV0.ragel
//
package com.appspot.btcticker.fsm;
public class MtgoxTickerV0 {
public static final String URL_V0 = "https://data.mtgox.com/api/0/data/ticker.php";
%%{
machine ticker;
write data;
}%%
%%{
action log { System.out.println(json.substring(p)); }
action mark { mark = p; }
action name {
attribute = json.substring(mark, p);
}
action clear { i = 0; f = 0.0; scale = 1.0; }
action ipart { i = i * 10 + (int)data[p] - zero; }
action fpart { scale = scale * 0.1; f = f + scale * ((int)data[p] - zero); }
action assign {
if ("buy".equals(attribute)) { buyValue = i + f; }
else if ("sell".equals(attribute)) { sellValue = i + f; }
else if ("last".equals(attribute)) { lastValue = i + f; }
}
number = digit+ >clear $ipart ('.' digit+ $fpart)?
;
attr = '"' alpha >mark (alnum | '_')+ '"' >name ':' number
;
ticker = '{' attr (',' >assign attr)+ '}' >assign
;
warning = '"warning"' ':' '"' (any - '"')* '"'
;
main := '{' warning ',' '"ticker"' ':' ticker '}'
;
}%%
public double weightedValue(String json) {
char[] data = json.toCharArray();
// Used by Ragel
int cs;
int p = 0;
int pe = data.length;
// Used to store extracted data
int mark = 0;
final int zero = (int) '0';
int i = 0;
double f = 0.0;
double scale = 1.0;
String attribute = null;
double lastValue = 0.0;
double buyValue = 0.0;
double sellValue = 0.0;
%% write init;
%% write exec;
if (cs < ticker_first_final) {
throw new RuntimeException("??? " + json);
}
return (sellValue + buyValue + 2*lastValue) / 4;
}
}