Skip to content

Commit 59d1f35

Browse files
committed
use jsjsdoc for some documentation
1 parent 9392617 commit 59d1f35

File tree

4 files changed

+242
-54
lines changed

4 files changed

+242
-54
lines changed

.gitignore

+30-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,33 @@
1+
# Logs
2+
logs
3+
*.log
4+
5+
# Runtime data
6+
pids
7+
*.pid
8+
*.seed
9+
10+
# Directory for instrumented libs generated by jscoverage/JSCover
11+
lib-cov
12+
13+
# Coverage directory used by tools like istanbul
14+
coverage
15+
16+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
17+
.grunt
18+
19+
# Compiled binary addons (http://nodejs.org/api/addons.html)
20+
build/Release
21+
22+
# Dependency directory
23+
# Deployed apps should consider commenting this line out:
24+
# see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git
125
node_modules
2-
.*.swp
26+
27+
# Swap files
328
.*.swo
29+
.*.swp
430
.*.swn
31+
32+
# OS Files
33+
.DS_Store

gruntfile.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module.exports = function(grunt) {
2+
3+
grunt.initConfig({
4+
pkg: grunt.file.readJSON('package.json'),
5+
jshint: {
6+
files: ['*.js'],
7+
options: {
8+
// options here to override JSHint defaults
9+
globals: {
10+
jQuery: true,
11+
console: true,
12+
module: true,
13+
document: true
14+
}
15+
}
16+
},
17+
jsjsdoc: {
18+
main: {
19+
files: {'docs/main.md': ['*.js']},
20+
}
21+
},
22+
watch: {
23+
main: {
24+
files: ['*.js'],
25+
tasks: ['jshint', 'jsjsdoc']
26+
}
27+
}
28+
});
29+
30+
grunt.loadNpmTasks('grunt-contrib-watch');
31+
grunt.loadNpmTasks('grunt-contrib-jshint');
32+
grunt.loadNpmTasks('grunt-jsjsdoc');
33+
34+
grunt.registerTask('default', ['jshint', 'jsjsdoc', 'watch']);
35+
36+
};
37+
38+

lib.js

+147-39
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
1+
/**
2+
* # lib.js
3+
*
4+
* Define the Coggle API classes
5+
*
6+
*/
7+
8+
/*! imports */
19
var unirest = require('unirest');
210

11+
/**
12+
* Create a new Coggle API Node object
13+
*
14+
* @constructor
15+
* @class CoggleApiNode
16+
*
17+
* @param {CoggleApiDiagram} diagram of this node
18+
* @param {Object} resource representing this node
19+
*
20+
* @return constructs an instance of CoggleApi
21+
* @api public
22+
*/
323
var CoggleApiNode = function CoggleApiNode(coggle_api_diagram, node_resource){
424
this.diagram = coggle_api_diagram;
525
this.id = node_resource._id;
@@ -13,13 +33,13 @@ var CoggleApiNode = function CoggleApiNode(coggle_api_diagram, node_resource){
1333

1434
this.children = [];
1535
if('children' in node_resource){
16-
node_resource.children.forEach(function(child){
17-
var child = new CoggleApiNode(coggle_api_diagram, child);
36+
node_resource.children.forEach(function(child_resource){
37+
var child = new CoggleApiNode(coggle_api_diagram, child_resource);
1838
child.parent_id = this.id;
1939
this.children.push_back(child);
2040
});
2141
}
22-
}
42+
};
2343
CoggleApiNode.prototype = {
2444
addChild: function addChild(text, offset, callback){
2545
var self = this;
@@ -42,11 +62,26 @@ CoggleApiNode.prototype = {
4262
}
4363
};
4464

65+
/**
66+
* Create a new instance of a Coggle API Diagram object.
67+
*
68+
* @constructor
69+
* @class CoggleApi
70+
*
71+
* @param {CoggleApi} api client to be used by this diagram
72+
* @param {Object} resource describing this diagram. Should have at least the
73+
* following fields:
74+
* * _id: ID of the diagram
75+
* * title: title of the diagram
76+
*
77+
* @return constructs an instance of CoggleApi
78+
* @api public
79+
*/
4580
var CoggleApiDiagram = function CoggleApiDiagram(coggle_api, diagram_resource){
4681
this.apiclient = coggle_api;
4782
this.id = diagram_resource._id;
4883
this.title = diagram_resource.title;
49-
}
84+
};
5085
CoggleApiDiagram.prototype = {
5186
webUrl: function webUrl(){
5287
return this.replaceId(this.apiclient.baseurl+'/diagram/:diagram');
@@ -71,43 +106,116 @@ CoggleApiDiagram.prototype = {
71106
};
72107

73108

109+
/**!jsjsdoc
110+
*
111+
* doc.CoggleApi = {
112+
* $brief: "The Coggle API client.",
113+
* }
114+
*
115+
* doc.CoggleApi.$constructor = {
116+
* $brief: "Create a new instance of the Coggle API client.",
117+
* $parameters: {
118+
* options: "Options: {}"
119+
* }
120+
* }
121+
*
122+
*/
74123
var CoggleApi = function CoggleApi(options){
75-
this.baseurl = options.base_url || 'http://localdev.coggle.it';
76-
this.token = options.token;
77-
}
78-
CoggleApi.prototype = {
79-
post: function post(endpoint, body, callback){
80-
unirest.post(this.baseurl + endpoint + '?access_token=' + this.token)
81-
.type('json')
82-
.send(body)
83-
.end(function(response){
84-
if(!response.ok)
85-
return callback(new Error('POST ' + endpoint + ' failed:' + (response.body && response.body.details) || response.error));
86-
return callback(false, response.body);
87-
});
88-
},
89-
get: function get(endpoint, callback){
90-
unirest.get(this.baseurl + endpoint + '?access_token=' + this.token)
91-
.type('json')
92-
.end(function(response){
93-
if(!response.ok)
94-
return callback(new Error('GET ' + endpoint + ' failed:' + (response.body && response.body.details) || response.error));
95-
return callback(false, response.body);
96-
});
97-
},
124+
this.baseurl = options.base_url || 'https://coggle.it';
125+
this.token = options.token;
126+
if(!options.token)
127+
throw new Error("you must provide a user's authentication token");
98128

99-
createDiagram: function createCoggle(title, callback){
100-
// callback should accept(error, diagram)
101-
var self = this;
102-
var body = {
103-
title:title
104-
};
105-
this.post('/api/1/diagrams', body, function(err, body){
106-
if(err)
107-
return callback(new Error('failed to create coggle: ' + err.message));
108-
return callback(false, new CoggleApiDiagram(self, body));
109-
});
110-
},
129+
CoggleApi.prototype = {
130+
/**!jsjsdoc
131+
*
132+
* doc.CoggleApi.post = {
133+
* $api: "private",
134+
* $brief: "POST to an endpoint on the Coggle API",
135+
* $parameters: {
136+
* endpoint: {
137+
* $type: "String",
138+
* $brief: "URL of endpoint to post to (relative to the domain)",
139+
* $example: "Use `/api/1/diagrams` to create a new diagram."
140+
* },
141+
* body: "The body to post. Will be converted to JSON.",
142+
* callback: {
143+
* $type: "Function",
144+
* $brief: "Callback accepting (error, body) that will be called "+
145+
* "with the result. The response returned from the server "+
146+
* "is parsed as JSON and returned as `body`.",
147+
* }
148+
* }
149+
* }
150+
*/
151+
post: function post(endpoint, body, callback){
152+
unirest.post(this.baseurl + endpoint + '?access_token=' + this.token)
153+
.type('json')
154+
.send(body)
155+
.end(function(response){
156+
if(!response.ok)
157+
return callback(new Error('POST ' + endpoint + ' failed:' + (response.body && response.body.details) || response.error));
158+
return callback(false, response.body);
159+
});
160+
},
161+
/**!jsjsdoc
162+
*
163+
* doc.CoggleApi.get = {
164+
* $api: "private",
165+
* $brief: "GET from an endpoint on the Coggle API",
166+
* $parameters: {
167+
* endpoint: {
168+
* $type: "String",
169+
* $brief: "URL of endpoint to get from (relative to the domain)"
170+
* },
171+
* callback: {
172+
* $type: "Function",
173+
* $brief: "Callback accepting (error, body) that will be called "+
174+
* "with the result. The response returned from the server "+
175+
* "is parsed as JSON and returned as `body`.",
176+
* }
177+
* }
178+
* }
179+
*/
180+
get: function get(endpoint, callback){
181+
unirest.get(this.baseurl + endpoint + '?access_token=' + this.token)
182+
.type('json')
183+
.end(function(response){
184+
if(!response.ok)
185+
return callback(new Error('GET ' + endpoint + ' failed:' + (response.body && response.body.details) || response.error));
186+
return callback(false, response.body);
187+
});
188+
},
189+
190+
/**!jsjsdoc
191+
*
192+
* doc.CoggleApi.createDiagram = {
193+
* $brief: "Create a new Coggle diagram.",
194+
* $parameters: {
195+
* title: {
196+
* $type: "String",
197+
* $brief: "Title for the created diagram."
198+
* },
199+
* callback: {
200+
* $type: "Function",
201+
* $brief: "Callback accepting (error, CoggleApiDiagram)",
202+
* }
203+
* }
204+
* }
205+
*/
206+
createDiagram: function createCoggle(title, callback){
207+
// callback should accept(error, diagram)
208+
var self = this;
209+
var body = {
210+
title:title
211+
};
212+
this.post('/api/1/diagrams', body, function(err, body){
213+
if(err)
214+
return callback(new Error('failed to create coggle: ' + err.message));
215+
return callback(false, new CoggleApiDiagram(self, body));
216+
});
217+
},
218+
};
111219
};
112220

113221
module.exports = CoggleApi;

readme.md

+27-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
## Coggle API Javascript Client
1+
# Coggle API Javascript Client
22

3-
### Basic Use
3+
## Basic Use
44
See [coggle-issue-importer](http://github.com/coggle/coggle-issue-importer) for
55
a complete example application, including authentication.
66

@@ -15,25 +15,38 @@ coggle.createDiagram(
1515
"My New Coggle"
1616
function(err, diagram){
1717
if(err)
18-
raise err;
18+
throw err;
1919
console.log("created diagram!", diagram);
2020
}
2121
);
2222
```
2323

2424

25-
### API Documentation
25+
# API Documentation
26+
##Class `CoggleApi`
27+
The Coggle API client.
2628

27-
#### CoggleApi
28-
##### constructor
29-
##### createDiagram
29+
###Constructor
30+
Create a new instance of the Coggle API client.
3031

31-
### CoggleApiDiagram
32-
##### constructor
33-
##### getNodes
34-
##### webUrl
32+
###Method `post`
33+
POST to an endpoint on the Coggle API
3534

36-
#### CoggleApiNode
37-
##### constructor
38-
##### addChild
35+
Parameters:
36+
* **`endpoint`** type: `String`URL of endpoint to post to (relative to the domain)
37+
* **`body`**The body to post. Will be converted to JSON.
38+
* **`callback`** type: `Function`Callback accepting (error, body) that will be called with the result. The response returned from the server is parsed as JSON and returned as `body`.
3939

40+
###Method `get`
41+
GET from an endpoint on the Coggle API
42+
43+
Parameters:
44+
* **`endpoint`** type: `String`URL of endpoint to get from (relative to the domain)
45+
* **`callback`** type: `Function`Callback accepting (error, body) that will be called with the result. The response returned from the server is parsed as JSON and returned as `body`.
46+
47+
###Method `createDiagram`
48+
Create a new Coggle diagram.
49+
50+
Parameters:
51+
* **`title`** type: `String`Title for the created diagram.
52+
* **`callback`** type: `Function`Callback accepting (error, CoggleApiDiagram)

0 commit comments

Comments
 (0)