diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..aebec38 --- /dev/null +++ b/.npmignore @@ -0,0 +1,8 @@ +test/ +coverage.html +lib-cov/ +Makefile +.travis.yml +logo.png +.gitignore +coverage/ \ No newline at end of file diff --git a/Makefile b/Makefile index 2725eee..a328399 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ TESTS = $(shell find test -type f -name "*.js") REPORTER = spec -TIMEOUT = 5000 +TIMEOUT = 10000 MOCHA_OPTS = install: diff --git a/README.md b/README.md new file mode 100644 index 0000000..98804a7 --- /dev/null +++ b/README.md @@ -0,0 +1,44 @@ +# Dota2 Web Api + + +[![Build Status](https://travis-ci.org/booxood/node-dota2-api.png?branch=master)](https://travis-ci.org/booxood/node-dota2-api) + +Dota2 web api node.js version.See detail [here](https://wiki.teamfortress.com/wiki/WebAPI#Dota_2) + +## Install + +``` +npm install dota2-api +``` + + +Example: +```javascript + var Dota2Api = require('dota2-api'); + var da = Dota2Api.create('a key'); + da.getMatchHistory(function(err, result){ + if(!err) + console.log(result); + }) +``` + + +## API + +### Dota2Api.create(key) +Create Dota2Api instance,require a key.(if no, see [here](http://steamcommunity.com/dev/apikey)) +```javascript + var da = Dota2Api.create('a key'); +``` + +### Dota2Api.prototype.getMatchHistory([options], callback) + +```javascript + fd.readlines(filePath, function(err, lines){ + console.log(lines); + }); +``` + + +## License +[The MIT License](https://github.com/booxood/node-dota2-api/blob/master/LICENSE) diff --git a/index.js b/index.js new file mode 100644 index 0000000..769ddbd --- /dev/null +++ b/index.js @@ -0,0 +1 @@ +module.exports = require('./lib/dota2-api'); diff --git a/lib/dota2-api.js b/lib/dota2-api.js new file mode 100644 index 0000000..42348bd --- /dev/null +++ b/lib/dota2-api.js @@ -0,0 +1,59 @@ +/*! + * dota2-api - lib/dota2-api.js + * + * Copyright(c) 2014 Liucw + * MIT Licensed + */ + +"use strict"; + +var url = require('url'); + +var request = require('request'); +var _ = require('underscore') + +var API_URL = 'https://api.steampowered.com/'; + +function Dota2Api(key) { + if (!key) { + throw new TypeError('required key (Get key from http://steamcommunity.com/dev/apikey)') + } + + this.key = key; +} + +Dota2Api.API_URL = API_URL; + +Dota2Api.create = function(key) { + return new Dota2Api(key); +}; + + +Dota2Api.prototype.getMatchHistory = function(options, cb) { + var validOptions = ['hero_id', 'game_mode', 'skill', 'min_players', 'account_id', + 'league_id', 'start_at_match_id', 'matches_requested', 'tournament_games_only' + ]; + var path = 'IDOTA2Match_570/GetMatchHistory/V001'; + + if (typeof options == 'function') { + cb = options; + options = {}; + } + + options = _.pick(options, validOptions); + options['key'] = this.key; + + var uri = url.format({ + pathname: API_URL + path, + query: options + }); + + request.get(uri, function(err, res, body) { + return cb(err, body); + }); + + +}; + + +module.exports = Dota2Api; \ No newline at end of file diff --git a/package.json b/package.json index 8fcd49a..e0fb02d 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "node-dota2-api", + "name": "dota2-api", "version": "0.0.1", "description": "Dota2 web api node.js version", "main": "index.js", @@ -21,5 +21,23 @@ "bugs": { "url": "https://github.com/booxood/node-dota2-api/issues" }, - "homepage": "https://github.com/booxood/node-dota2-api" + "homepage": "https://github.com/booxood/node-dota2-api", + "config": { + "blanket": { + "pattern": "//^((?!(node_modules|test)).)*$/" + }, + "travis-cov": { + "threshold": 90 + } + }, + "dependencies": { + "request": "^2.47.0", + "underscore": "^1.7.0" + }, + "devDependencies": { + "blanket": "^1.1.6", + "mocha": "^2.0.1", + "should": "^4.2.1", + "travis-cov": "^0.2.5" + } } diff --git a/test/dota2-api.js b/test/dota2-api.js new file mode 100644 index 0000000..38d9049 --- /dev/null +++ b/test/dota2-api.js @@ -0,0 +1,43 @@ +'use strict' + +var should = require('should'); +var Dota2Api = require('../index.js'); + +describe('Dota2Api test case', function() { + + describe('Dota2Api.create', function() { + before(function() {}); + + it('should throw TypeError, create no key', function() { + (function() { + Dota2Api.create(); + }).should.throw(/^required key/); + }); + + it('should return Dota2Api instance, create no key', function() { + var da = Dota2Api.create('a key'); + da.should.be.an.instanceof(Dota2Api); + }); + }); + + describe('Dota2Api prototype function', function() { + var da + before(function() { + da = Dota2Api.create('a key'); + }); + + it('should return , getMatchHistory', function(done) { + da.getMatchHistory(function(err, result){ + should.not.exist(err); + should.exist(result); + done(); + }); + }); + + it('should return Dota2Api instance, create no key', function() { + var da = Dota2Api.create('a key'); + da.should.be.an.instanceof(Dota2Api); + }); + }); + +}); \ No newline at end of file