'use strict';

var request = require('request');

// Tradable assets from https://api.kraken.com/0/public/AssetPairs
var assetNames = {
    'EUR': 'XXBTZEUR',
    'USD': 'XXBTZUSD'
};
var KrakenAPIURL = 'https://api.kraken.com/0/public/Ticker';

var KrakenTicker = function () {

};

KrakenTicker.factory = function factory() {
    return new KrakenTicker();
};

KrakenTicker.prototype.ticker = function ticker(currencies, callback) {
    if (currencies.length === 0) {
	return callback(new Error('No currency specified.'));
    };

    var queryArray = [];
    for (var i = 0; i < currencies.length; i++) {
	var currency = currencies[i];
	if (currency in assetNames) {
	    queryArray.push(assetNames[currency]);
	} else {
	    return callback(new Error('Unsupported currencies.'));
	}
    }

    var queryOptions = {
	url: KrakenAPIURL,
	headers: {
            'User-Agent': 'lamassu-kraken'
	},
	qs: {
	    'pair': queryArray.join(",")
	}
    };

    request(queryOptions, function(error, response, body) {
	if (error) {
	    return callback(error);
	}
	try {
	    var responseJSON = JSON.parse(body);
	} catch(e) {
	    return callback(new Error('Can\'t parse response data.'));
	}
	if ('result' in responseJSON) {
	    var result = responseJSON['result'];
	    var tickerResponse = {}
	    for (var i = 0; i < currencies.length; i++) {
		var currency = currencies[i];
		var assetName = assetNames[currency];
		if (assetName in result) {
		    // add the "ask" price of the given currency to the ticker data
		    tickerResponse[currency] = {'currency': currency, 'rate': result[assetName].a[0]}
		} else {
		    return callback(new Error('Wrong response data.'));
		}
	    }
	    return callback(null, tickerResponse);
	} else {  // result object not in response
	    return callback(new Error('Can\'t find required parts in response data.'));
	}
    });
};

module.exports = KrakenTicker;

@@ -12,43 +12,8 @@
 ';%0A%0A
-var request = require('request');%0A%0A
 // T
@@ -142,16 +142,17 @@
 USD'%0A%7D;%0A
+%0A
 var Krak
@@ -157,93 +157,111 @@
 aken
-APIURL = 'https://api.kraken.com/0/public/Ticker';%0A%0Avar KrakenTicker = function () %7B%0A
+Client = require('kraken-api');%0A%0Avar KrakenTicker = function () %7B%0A    this.client = new KrakenClient();
 %0A%7D;%0A
@@ -408,24 +408,25 @@
 callback) %7B%0A
+%0A
     if (curr
@@ -771,17 +771,16 @@
 %7D%0A    %7D%0A
-%0A
     var 
@@ -788,110 +788,11 @@
 uery
-Options = %7B%0A%09url: KrakenAPIURL,%0A%09headers: %7B%0A            'User-Agent': 'lamassu-kraken'%0A%09%7D,%0A%09qs: %7B%0A%09   
+= %7B
  'pa
@@ -816,23 +816,16 @@
 oin(%22,%22)
-%0A%09%7D%0A   
  %7D;%0A%0A   
@@ -829,28 +829,39 @@
     
-request(queryOptions
+this.client.api('Ticker', query
 , fu
@@ -886,14 +886,8 @@
 onse
-, body
 ) %7B%0A
@@ -935,140 +935,21 @@
 ;%0A%09%7D
-%0A%09try %7B%0A%09    var responseJSON = JSON.parse(body);%0A%09%7D catch(e) %7B%0A%09    return callback(new Error('Can%5C't parse response data.'));%0A%09%7D%0A%09
+ else %7B%0A%09    
 if (
@@ -972,21 +972,14 @@
 onse
-JSON
 ) %7B%0A%09
-    
+%09
 var 
@@ -999,12 +999,8 @@
 onse
-JSON
 %5B're
@@ -1008,20 +1008,17 @@
 ult'%5D;%0A%09
-    
+%09
 var tick
@@ -1034,20 +1034,17 @@
 e = %7B%7D%0A%09
-    
+%09
 for (var
@@ -1079,24 +1079,28 @@
 h; i++) %7B%0A%09%09
+    
 var currency
@@ -1115,24 +1115,28 @@
 ncies%5Bi%5D;%0A%09%09
+    
 var assetNam
@@ -1163,16 +1163,20 @@
 ncy%5D;%0A%09%09
+    
 if (asse
@@ -1196,20 +1196,17 @@
 lt) %7B%0A%09%09
-    
+%09
 // add t
@@ -1263,20 +1263,17 @@
  data%0A%09%09
-    
+%09
 tickerRe
@@ -1348,16 +1348,20 @@
 a%5B0%5D%7D%0A%09%09
+    
 %7D else %7B
@@ -1363,20 +1363,17 @@
 lse %7B%0A%09%09
-    
+%09
 return c
@@ -1421,19 +1421,16 @@
 );%0A%09
-%09%7D%0A
 %09    %7D%0A%09
     
@@ -1425,20 +1425,21 @@
 %09    %7D%0A%09
-    
+%09%7D%0A%09%09
 return c
@@ -1466,24 +1466,28 @@
 Response);%0A%09
+    
 %7D else %7B  //
@@ -1518,20 +1518,17 @@
 sponse%0A%09
-    
+%09
 return c
@@ -1591,24 +1591,31 @@
 e data.'));%0A
+%09    %7D%0A
 %09%7D%0A    %7D);%0A%7D

move over to the kraken API lib, closes #1