<commit_before>'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;<commit_msg>ticker: move over to the kraken API lib, closes #1<commit_after>'use strict';

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

var KrakenClient = require('kraken-api');

var KrakenTicker = function () {
    this.client = new KrakenClient();
};

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 query= { 'pair': queryArray.join(",") };

    this.client.api('Ticker', query, function(error, response) {
	if (error) {
	    return callback(error);
	} else {
	    if ('result' in response) {
		var result = response['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;
{'fraction_non_alphanumeric': 0.1483924154987634, 'fraction_numerical': 0.0032976092333058533, 'mean_word_length': 3.375, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '599452', 'n_tokens_mistral': 1241, 'n_tokens_neox': 1159, 'n_words': 310}