Last active
September 30, 2016 01:00
-
-
Save jrusbatch/8815094 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(global) { | |
'use strict'; | |
var BaseUri = 'http://query.yahooapis.com/v1/public/yql'; | |
var DefaultParameters = { | |
diagnostics: true, | |
env: 'http://datatables.org/alltables.env' | |
}; | |
function createQuery() { | |
var args = Array.prototype.slice.call(arguments); | |
var symbols = args.map(encodeURIComponent).join(); | |
return 'select * from yahoo.finance.quotes where symbol in ("' + symbols + '")'; | |
} | |
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 | |
}; | |
}); | |
} | |
var YahooFinance = { | |
baseUri: BaseUri, | |
defaultParameters: DefaultParameters, | |
lookupSymbols: function() { | |
var args = Array.prototype.slice.call(arguments); | |
var params = $.extend({}, DefaultParameters, { q: createQuery(args) }); | |
return $.Deferred(function(dfd) { | |
$.get(YahooFinance.baseUri, params) | |
.fail(dfd.reject) | |
.done(function(xmlDoc) { | |
var quotes = parseResponse(xmlDoc); | |
dfd.resolve(quotes); | |
}); | |
}); | |
} | |
}; | |
})(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment