Skip to content

Commit

Permalink
first commit of v2
Browse files Browse the repository at this point in the history
  • Loading branch information
felipefdl committed Jun 21, 2016
0 parents commit 237341e
Show file tree
Hide file tree
Showing 31 changed files with 464 additions and 0 deletions.
7 changes: 7 additions & 0 deletions !examples/account.js
@@ -0,0 +1,7 @@
'use strict';
const Account = require('tago/account');
const myacc = new Account('250b4030-f519-11e5-8535-970c12895890');

myacc.info()
.then(result => console.log(result))
.catch(error => console.error('Error!', error));
15 changes: 15 additions & 0 deletions !examples/analysis.js
@@ -0,0 +1,15 @@
'use strict';
const Analysis = require('tago/analysis');

function myanalysis(data, environment) {
console.log(data, environment);
}

module.exports = new Analysis(myanalysis, '1345-2332-5674-2354');

/**
* That analysis can run on your machine,
* or on Tago servers,
* if will run in Tago server,
* we will ignore analysis token.
*/
18 changes: 18 additions & 0 deletions !examples/device.js
@@ -0,0 +1,18 @@
'use strict';
const Device = require('tago/device');
const mydevice = new Device('079a01a0-2ec4-11e6-a77d-991b8f63b767');

var mydata = [
{
variable: 'xx',
value: 123
}
];

mydevice.insert(mydata)
.then(result => console.log('added', result))
.catch(error => console.error('Error!', error));

mydevice.find({query: 'count'})
.then(result => console.log('added', result))
.catch(error => console.error('Error!', error));
1 change: 1 addition & 0 deletions !examples/node_modules/tago

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions .eslintrc.yml
@@ -0,0 +1,23 @@
env:
es6: true
node: true
mocha: true
extends: 'eslint:recommended'
rules:
indent:
- error
- 4
- SwitchCase: 1
linebreak-style:
- error
- unix
quotes:
- error
- single
semi:
- error
- always
no-console: 0
comma-dangle: 0
no-unused-vars: 1
no-case-declarations: 1
34 changes: 34 additions & 0 deletions .gitignore
@@ -0,0 +1,34 @@
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed
.DS_Store

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# Deployed apps should consider commenting this line out:
# see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git
node_modules

# Editor
*.sublime-*

# Tago
run_tests.sh
.remote-sync.json
.zsh_history
1 change: 1 addition & 0 deletions .npmrc
@@ -0,0 +1 @@
save-prefix=""
52 changes: 52 additions & 0 deletions Makefile
@@ -0,0 +1,52 @@
# Executables
MOCHA_EXEC = ./node_modules/.bin/mocha
MOCHA__EXEC = ./node_modules/.bin/_mocha
ESLINT_EXEC = ./node_modules/.bin/eslint

test: showtodo countconsolelog eslint test-exec

eslint:
@echo "\n---| ESLINT |---"
@$(ESLINT_EXEC) .

test-exec:
@echo "\n---| Mocha (Unit) |---"
@$(MOCHA_EXEC) \
--reporter spec \
--ui tdd \
--recursive \
--check-leaks \
--bail \
--inline-diffs \
test/

showtodo:
@find . \
-not -path "*node_modules*" \
-not -path "*coverage*" \
-not -path "*.git*" \
-not -path "*Makefile*" \
-not -path "*wercker.yml*" \
-type f \
-exec egrep --color -inH "TODO|FIXME" {} \;

countconsolelog:
@echo "Number of console.log on all code:"
@find . \
-not -path "*node_modules*" \
-not -path "*coverage*" \
-not -path "*.git*" \
-not -path "*Makefile*" \
-type f \
-exec egrep --color -inH "console.log" {} \; | wc -l

showconsolelog:
@find . \
-not -path "*node_modules*" \
-not -path "*coverage*" \
-not -path "*.git*" \
-not -path "*Makefile*" \
-type f \
-exec egrep --color -inH "console.log" {} \;

.PHONY: test eslint test-exec showtodo countconsolelog showconsolelog
49 changes: 49 additions & 0 deletions README.md
@@ -0,0 +1,49 @@
## Tago - Node.JS Lib

Official Node.js lib for Tago

## Code Status

[![wercker status](https://app.wercker.com/status/7eba1fa5503f7f5ad61a15a0a6e63234/m "wercker status")](https://app.wercker.com/project/bykey/7eba1fa5503f7f5ad61a15a0a6e63234)

## Documentation

#### Installation

NODE >= v6
```
$ npm install tago --save
```

NODE < v6
```
$ npm install tago@1.* --save
```

#### Usage
##### Insert Data
**.insert(JSON || Array);**
``` javascript
const Device = require('tago/device');
const mydevice = new Device('079a01a0-2ec4-11e6-a77d-991b8f63b767');

let data_to_insert = {
'variable' : 'temperature',
'location' : {'lat': 42.2974279, 'lng': -85.628292},
'time' : '2014-01-20 03:43:59',
'unit' : 'C',
'value' : 63
};

my_device.insert(data_to_insert)
.then(api_response => {
console.log('Data added');
})
.catch(error => {
console.error('Error:', error);
});
```

## License

Tago lib client for Node.js is released under the [Copyright License](https://github.com/tago-io/tago-nodejs/blob/master/LICENSE.md).
Empty file added account/actions.js
Empty file.
Empty file added account/analysis.js
Empty file.
Empty file added account/buckets.js
Empty file.
Empty file added account/dashboards.js
Empty file.
27 changes: 27 additions & 0 deletions account/devices.js
@@ -0,0 +1,27 @@
'use strict';
const request = require('../comum/tago_request.js');
const config = require('../config.js');
const default_headers = require('../comum/default_headers.js');

class Devices {
constructor(acc_token) {
this.token = acc_token;
this.default_options = {
'json': true,
'headers': default_headers(this)
};
}

/** List Devices
* @return {Promise}
*/
list() {
let uri = `${config.api_uri}/device`;
let method = 'GET';

let options = Object.assign({}, this.default_options, {uri, method});
return request(options);
}
}

module.exports = Devices;
52 changes: 52 additions & 0 deletions account/index.js
@@ -0,0 +1,52 @@
'use strict';
const config = require('../config.js');
const default_headers = require('../comum/default_headers.js');
const request = require('../comum/tago_request.js');

const Actions = require('./actions.js');
const Analysis = require('./analysis.js');
const Buckets = require('./buckets.js');
const Dashboards = require('./dashboards.js');
const Devices = require('./devices.js');

class Account {
constructor(token) {
this.token = token;
this.default_options = {
'json': true,
'headers': default_headers(this)
};
}

// ----------- Account methods -----------

/** Account info
* @return {Promise}
*/
info() {
let uri = `${config.api_uri}/account`;
let method = 'GET';

let options = Object.assign({}, this.default_options, {uri, method});
return request(options);
}

// ----------- Sub-methods -----------
get actions() {
return new Actions(this.token);
}
get analysis() {
return new Analysis(this.token);
}
get buckets() {
return new Buckets(this.token);
}
get dashboards() {
return new Dashboards(this.token);
}
get devices() {
return new Devices(this.token);
}
}

module.exports = Account;
40 changes: 40 additions & 0 deletions analysis/index.js
@@ -0,0 +1,40 @@
'use strict';
const socketio = require('socket.io-client');
const config = require('./../config.js');

class Analysis {
constructor(analysis, token = null) {
const ambient = this.check_ambient();

if (ambient === 'tago-server') {
analysis(this.data, this.env);
}

if (ambient === 'local') {
this.local_analysis(analysis, token);
}
}

check_ambient() {
if (process.env.TAGO_DATA || process.env.TAGO_ENV) {
this.data = process.env.TAGO_DATA || [];
this.env = process.env.TAGO_ENV || {};
return 'tago-server';
}
return 'local';
}

local_analysis(analysis, token) {
if (!token) {
throw 'To run locally, needs a token.';
}
const scon = socketio(config.realtime_uri);
scon.on('connect', () => {
console.log('Connected on Tago.io');
scon.emit('register:analysis', token);
scon.on('run:analysis', analysis);
});
}
}

module.exports = Analysis;
16 changes: 16 additions & 0 deletions comum/default_headers.js
@@ -0,0 +1,16 @@
'use strict';
const config = require('../config.js');

/** default_headers
* Generate default headers
* @private
* @return {JSON}
*/
function default_headers(class_context) {
return {
'Token': class_context.token,
'User-Agent': `Tago-Nodelib-${config.version}`
};
}

module.exports = default_headers;
11 changes: 11 additions & 0 deletions comum/tago_request.js
@@ -0,0 +1,11 @@
'use strict';
const rp = require('request-promise');

module.exports = function tago_request(request_options) {
return rp(request_options).then(result => {
if (!result.status) {
throw result.message || result;
}
return result.result;
});
};
5 changes: 5 additions & 0 deletions config.js
@@ -0,0 +1,5 @@
'use strict';
module.exports = {
'api_uri': process.env.TAGO_API || 'https://api.tago.io',
'realtime_uri': process.env.TAGO_REALTIME || 'https://realtime.tago.io'
};

0 comments on commit 237341e

Please sign in to comment.