SHARE
TWEET

Untitled

a guest Dec 27th, 2014 241 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. angular.module('finance3', [])
  2.   .factory('currencyConverter', ['$http', function($http) {
  3.     var YAHOO_FINANCE_URL_PATTERN =
  4.           '//query.yahooapis.com/v1/public/yql?q=select * from '+
  5.           'yahoo.finance.xchange where pair in ("PAIRS")&format=json&'+
  6.           'env=store://datatables.org/alltableswithkeys&callback=JSON_CALLBACK';
  7.     var currencies = ['USD', 'EUR', 'CNY'];
  8.     var usdToForeignRates = {};
  9.  
  10.     var convert = function (amount, inCurr, outCurr) {
  11.       return amount * usdToForeignRates[outCurr] / usdToForeignRates[inCurr];
  12.     };
  13.  
  14.     var refresh = function() {
  15.       var url = YAHOO_FINANCE_URL_PATTERN.
  16.                  replace('PAIRS', 'USD' + currencies.join('","USD'));
  17.       return $http.jsonp(url).success(function(data) {
  18.         var newUsdToForeignRates = {};
  19.         angular.forEach(data.query.results.rate, function(rate) {
  20.           var currency = rate.id.substring(3,6);
  21.           newUsdToForeignRates[currency] = window.parseFloat(rate.Rate);
  22.         });
  23.         usdToForeignRates = newUsdToForeignRates;
  24.       });
  25.     };
  26.  
  27.     refresh();
  28.  
  29.     return {
  30.       currencies: currencies,
  31.       convert: convert,
  32.       refresh: refresh
  33.     };
  34.   }]);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top