Skip to content

Commit fc4cb6e

Browse files
committed
0.1.1
0 parents  commit fc4cb6e

39 files changed

+1603
-0
lines changed

.editorconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
insert_final_newline = true
9+
10+
[*.md]
11+
trim_trailing_whitespace = false

.eslintrc.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
module.exports = {
2+
"env": {
3+
"node": true,
4+
"es6": true
5+
},
6+
"extends": "eslint:recommended",
7+
"rules": {
8+
"indent": [
9+
"error",
10+
2,
11+
{
12+
"SwitchCase": 1
13+
}
14+
],
15+
"linebreak-style": [
16+
"error",
17+
"unix"
18+
],
19+
"quotes": [
20+
"error",
21+
"single"
22+
],
23+
"semi": [
24+
"error",
25+
"always"
26+
],
27+
"eqeqeq": 2,
28+
"space-before-function-paren": 2,
29+
"keyword-spacing": 2,
30+
"eol-last": 2,
31+
"no-trailing-spaces": 2,
32+
"padded-blocks": [
33+
2,
34+
"never"],
35+
36+
// set to 1 for finding errors?
37+
"no-console": 0,
38+
"no-unused-vars": 0
39+
40+
41+
42+
}
43+
};

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
coverage

.travis.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
language: node_js
2+
node_js:
3+
- v7
4+
- v6
5+
- v5
6+
- v4
7+
- '0.12'

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 CodingNavi <codingnavi@gmail.com> (https://codingnavi.github.io)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# nitrapi [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][daviddm-image]][daviddm-url]
2+
> NodeJs based SDK for the Nitrapi
3+
4+
## Installation
5+
6+
```sh
7+
$ npm install --save nitrapi
8+
```
9+
10+
## Usage
11+
12+
```js
13+
var Nitrapi = require('nitrapi');
14+
15+
var api = new Nitrapi("<access token>");
16+
17+
api.getServices(function (services) {
18+
// successfully got our service list
19+
console.log(services);
20+
}, function (error) {
21+
// an error occured
22+
console.error(error);
23+
});
24+
```
25+
26+
## Documentation
27+
28+
This SDK works very similar to the [Nitrapi-PHP SDK](https://github.com/nitrado/Nitrapi-PHP). You can read the comments there to find out which parameters most methods expect.
29+
30+
For more information on the capabilities of the Nitrapi, consult [the official documentation](https://nitrado.github.io/Nitrapi/).
31+
32+
## License
33+
34+
MIT © [CodingNavi](https://codingnavi.github.io)
35+
36+
37+
[npm-image]: https://badge.fury.io/js/nitrapi.svg
38+
[npm-url]: https://npmjs.org/package/nitrapi
39+
[travis-image]: https://travis-ci.org/codingnavi/nitrapi-node.svg?branch=master
40+
[travis-url]: https://travis-ci.org/codingnavi/nitrapi-node
41+
[daviddm-image]: https://david-dm.org/codingnavi/nitrapi-node.svg?theme=shields.io
42+
[daviddm-url]: https://david-dm.org/codingnavi/nitrapi-node

gulpfile.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'use strict';
2+
var path = require('path');
3+
var gulp = require('gulp');
4+
var eslint = require('gulp-eslint');
5+
var excludeGitignore = require('gulp-exclude-gitignore');
6+
var mocha = require('gulp-mocha');
7+
var istanbul = require('gulp-istanbul');
8+
var nsp = require('gulp-nsp');
9+
var plumber = require('gulp-plumber');
10+
11+
gulp.task('static', function () {
12+
return gulp.src('lib/**/*.js')
13+
.pipe(excludeGitignore())
14+
.pipe(eslint())
15+
.pipe(eslint.format())
16+
.pipe(eslint.failAfterError());
17+
});
18+
19+
gulp.task('nsp', function (cb) {
20+
nsp({package: path.resolve('package.json')}, cb);
21+
});
22+
23+
gulp.task('pre-test', function () {
24+
return gulp.src('lib/**/*.js')
25+
.pipe(excludeGitignore())
26+
.pipe(istanbul({
27+
includeUntested: true
28+
}))
29+
.pipe(istanbul.hookRequire());
30+
});
31+
32+
gulp.task('test', ['pre-test'], function (cb) {
33+
var mochaErr;
34+
35+
gulp.src('test/**/*.js')
36+
.pipe(plumber())
37+
.pipe(mocha({reporter: 'spec'}))
38+
.on('error', function (err) {
39+
mochaErr = err;
40+
})
41+
.pipe(istanbul.writeReports())
42+
.on('end', function () {
43+
cb(mochaErr);
44+
});
45+
});
46+
47+
gulp.task('watch', function () {
48+
gulp.watch(['lib/**/*.js', 'test/**'], ['test']);
49+
});
50+
51+
gulp.task('prepublish', ['nsp']);
52+
gulp.task('default', ['static', 'test']);

lib/customer/customer.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
class Customer {
4+
constructor (nitrapi, data) {
5+
this.nitrapi = nitrapi;
6+
this.loadData(data);
7+
}
8+
9+
loadData (data) {
10+
for (var name in data) { // copy old data attributes
11+
if (data.hasOwnProperty(name)) {
12+
this[name] = data[name];
13+
}
14+
}
15+
}
16+
17+
getWebinterfaceToken (success, failure) {
18+
this.nitrapi.dataGet('user/webinterface_token', {}, function (data) {
19+
success(data.token.token);
20+
}, failure);
21+
}
22+
23+
deleteWebinterfaceTokens (success, failure) {
24+
this.nitrapi.dataDelete('user/webinterface_token', {}, success, failure);
25+
}
26+
}
27+
28+
module.exports = Customer;

lib/index.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
'use strict';
2+
3+
var Client = require('node-rest-client').Client;
4+
var ServiceFactory = require('./services/servicefactory.js');
5+
var Customer = require('./customer/customer.js');
6+
7+
var NITRAPI_LIVE_URL = 'https://api.nitrado.net/';
8+
9+
class Nitrapi {
10+
constructor (accessToken, locale = null, url = NITRAPI_LIVE_URL) {
11+
this.client = new Client();
12+
this.nitrapiUrl = url;
13+
this.options = {
14+
access_token: accessToken
15+
};
16+
if (locale !== null) {
17+
this.options.locale = locale;
18+
}
19+
}
20+
21+
ping (success, failure) {
22+
this.dataGet('ping', {}, success, failure);
23+
}
24+
25+
getService (success, failure, id) {
26+
var api = this;
27+
this.dataGet('services/' + id, {}, function (data) {
28+
ServiceFactory.buildService(api, data.service, success, failure);
29+
}, failure);
30+
}
31+
32+
getServices (success, failure) {
33+
var api = this;
34+
this.dataGet('services', {}, function (data) {
35+
ServiceFactory.buildServices(api, data.services, success, failure);
36+
}, failure);
37+
}
38+
39+
getAdmin (success, failure) {
40+
// TODO
41+
failure('not yet implemented');
42+
}
43+
44+
getCustomer (success, failure) {
45+
this.dataGet('user', {}, function (data) {
46+
success(new Customer(this, data.user));
47+
}, failure);
48+
}
49+
50+
dataGet (endpoint, params, success, failure) {
51+
this.req('get', endpoint, params, success, failure);
52+
}
53+
dataPost (endpoint, params, success, failure) {
54+
this.req('post', endpoint, params, success, failure);
55+
}
56+
dataDelete (endpoint, params, success, failure) {
57+
this.req('delete', endpoint, params, success, failure);
58+
}
59+
req (method, endpoint, params, success, failure) {
60+
for (var name in this.options) {
61+
if (this.options.hasOwnProperty(name)) {
62+
params[name] = this.options[name];
63+
}
64+
}
65+
var args = {
66+
parameters: params
67+
};
68+
69+
var func = this.client.get;
70+
if (method === 'post') {
71+
func = this.client.post;
72+
} else if (method === 'delete') {
73+
func = this.client.delete;
74+
}
75+
76+
var req = func(this.nitrapiUrl + endpoint, args, function (data) {
77+
if (data.status === 'success') {
78+
if (typeof data.data === 'undefined') {
79+
success(data.message);
80+
} else {
81+
success(data.data);
82+
}
83+
} else {
84+
failure(data.message);
85+
}
86+
});
87+
req.on('requestTimeout', function (req) {
88+
console.log('request has expired');
89+
failure('request has expired');
90+
req.abort();
91+
});
92+
93+
req.on('responseTimeout', function () {
94+
console.log('response has expired');
95+
failure('response has expired');
96+
});
97+
98+
req.on('error', function (err) {
99+
console.log('ERROR: ' + err);
100+
failure(err);
101+
});
102+
}
103+
}
104+
105+
module.exports = Nitrapi;

0 commit comments

Comments
 (0)