This repository has been archived by the owner on Jul 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
127 lines (106 loc) · 4.06 KB
/
main.js
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
(function (global, $) {
'use strict';
global.stockRetriever = global.stockRetriever || {};
global.stockRetriever.dataProvider = {
getPrices: function (symbols) {
if (typeof symbols !== 'string' && !$.isArray(symbols)) {
throw new TypeError('Must provide a string or an array for symbols.');
}
if (typeof symbols === 'string') {
symbols = [symbols];
}
return $.Deferred(function (dfd) {
var url = getUrl(symbols);
$.get(url)
.done(function (res) {
var result = parseResponse(res);
dfd.resolve(result);
})
.fail(dfd.reject);
}).promise();
}
};
function getUrl(symbols) {
return 'http://query.yahooapis.com/v1/public/yql' +
'?q=select * from yahoo.finance.quotes where symbol in ("' +
symbols.map(encodeURIComponent).join() +
'")&diagnostics=true&env=http://datatables.org/alltables.env';
}
function parseResponse(xml) {
var $query = $(xml).find('query').first();
var created = new Date($query.attr('yahoo:created'));
var quotes = $query.find('quote').get();
return quotes.map(function (quote) {
var $quote = $(quote);
return {
symbol: $quote.attr('symbol'),
lastTradePrice: parseFloat($quote.find('LastTradePriceOnly').text()),
retrieved: created
};
});
}
})(this, jQuery);
(function (global, $) {
'use strict';
global.stockRetriever = global.stockRetriever || {};
global.stockRetriever.uiProvider = {
configuration: {
selectors: {
symbol: $('#stock-symbol'),
currentPrice: $('#current-stock-price'),
priceLog: $('#stock-price-log')
}
},
displayLoading: function () {
var selectors = this.configuration.selectors;
selectors.currentPrice.html('Loading ...');
return this;
},
getSymbol: function () {
var selectors = this.configuration.selectors;
return $.trim(selectors.symbol.val()).toUpperCase();
},
setPrice: function (symbol, price, retrievedAt) {
var selectors = this.configuration.selectors;
var retrievedAtStr = retrievedAt.toString();
selectors.currentPrice.html('<strong>' + htmlEncode(symbol) + '</strong>: $' + htmlEncode(price) + ' retrieved at ' + htmlEncode(retrievedAtStr));
selectors.priceLog.append('<li><strong>' + htmlEncode(symbol) + '</strong> $' + htmlEncode(price) + ' retrieved at ' + htmlEncode(retrievedAtStr) + '</li>');
return this;
}
};
// http://stackoverflow.com/a/1219983/941536
function htmlEncode(value) {
return $('<div/>').text(value).html();
}
})(this, jQuery);
(function (global, $, uiProvider, dataProvider) {
'use strict';
global.stockRetriever = global.stockRetriever || {};
global.stockRetriever.app = {
configuration: {
selectors: {
fetchButton: $('#fetch')
}
},
init: function () {
var selectors = this.configuration.selectors;
selectors.fetchButton.on('click', this.fetch);
return this;
},
fetch: function () {
var symbol = uiProvider.getSymbol();
uiProvider.displayLoading();
dataProvider.getPrices(symbol).then(function (res) {
var item = res[0];
uiProvider.setPrice(item.symbol, item.lastTradePrice, item.retrieved);
}).fail(function () {
uiProvider.setPrice(item.symbol, '(n/a)');
});
return this;
}
};
})(this, jQuery, this.stockRetriever.uiProvider, this.stockRetriever.dataProvider);
(function (global, app) {
'use strict';
app.init();
})(this, this.stockRetriever.app);