Skip to content
This repository was archived by the owner on Oct 24, 2025. It is now read-only.

Commit c7c768c

Browse files
committed
Migrate from iamnapo/cbm-api
0 parents  commit c7c768c

File tree

18 files changed

+6345
-0
lines changed

18 files changed

+6345
-0
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lib/

.gitignore

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
8+
# Runtime data
9+
pids
10+
*.pid
11+
*.seed
12+
*.pid.lock
13+
14+
# Directory for instrumented libs generated by jscoverage/JSCover
15+
lib-cov
16+
17+
# Coverage directory used by tools like istanbul
18+
coverage
19+
20+
# nyc test coverage
21+
.nyc_output
22+
23+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
24+
.grunt
25+
26+
# Bower dependency directory (https://bower.io/)
27+
bower_components
28+
29+
# node-waf configuration
30+
.lock-wscript
31+
32+
# Compiled binary addons (http://nodejs.org/api/addons.html)
33+
build/Release
34+
35+
# Dependency directories
36+
node_modules/
37+
jspm_packages/
38+
39+
# Typescript v1 declaration files
40+
typings/
41+
42+
# Optional npm cache directory
43+
.npm
44+
45+
# Optional eslint cache
46+
.eslintcache
47+
48+
# Optional REPL history
49+
.node_repl_history
50+
51+
# Output of 'npm pack'
52+
*.tgz
53+
54+
# Yarn Integrity file
55+
.yarn-integrity
56+
57+
# dotenv environment variables file
58+
.env

.npmignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.travis.yml
2+
codecov.yml

.travis.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
before_install:
2+
- npm update -g npm
3+
cache:
4+
directories:
5+
- node_modules
6+
language: node_js
7+
node_js:
8+
- node
9+
notifications:
10+
email: false
11+
sudo: false

LICENSE

Lines changed: 661 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
# cbm-api [![Build Status](https://travis-ci.org/cbmjs/cbm-api.svg?branch=master)](https://travis-ci.org/cbmjs/cbm-api) [![codecov](https://codecov.io/gh/cbmjs/cbm-api/branch/master/graph/badge.svg)](https://codecov.io/gh/cbmjs/cbm-api) [![npm version](https://badge.fury.io/js/cbm-api.svg)](https://badge.fury.io/js/cbm-api)
2+
3+
Node.js interface to the CallByMeaning network server. For further information, consult the website of the server-side project: [CallByMeaning](https://github.com/cbmjs/CallByMeaning).
4+
5+
## Introduction
6+
7+
To require the module in a project, we can use the expression:
8+
9+
```javascript
10+
const CallByMeaning = require('cbm-api');
11+
```
12+
13+
## Getting Started
14+
15+
The module exports a single constructor which can be used to open an API connection. Simply call it and store the expression result in a variable:
16+
17+
``` javascript
18+
const cbm = new CallByMeaning();
19+
```
20+
21+
In case that you are running your own copy of the CallByMeaning server, the constructor takes the hostname of the server as an optional argument. The default option evaluates to "[https://call-by-meaning.herokuapp.com](https://call-by-meaning.herokuapp.com/)".
22+
23+
```javascript
24+
CallByMeaning(host);
25+
```
26+
27+
Example:
28+
29+
```javascript
30+
const cbm = new CallByMeaning('http://localhost:3000');
31+
```
32+
33+
We can then use the following six methods to query the CallByMeaning API:
34+
35+
## Methods
36+
37+
### `.lookup(uri[, type])`
38+
39+
This method expects a valid CallByMeaning URI as its first argument.
40+
`type` is an (optional) string that specifies the type of the GET request. It can have the keys `c`, `f` or `r`. This method is asynchronous and returns a promise that, when fulfilled, returns an object with two properties.`statusCode` which contains the status code of the request and `body` that holds the result set from the query.
41+
42+
Example code:
43+
44+
```javascript
45+
cbm.lookup('time', 'c').then((result) => {
46+
if (result.statusCode === 200) console.log('Success!');
47+
// insert code here
48+
}).catch((error) => console.error(error));
49+
```
50+
51+
### `.getURI(text)`
52+
53+
This method finds out what the CallByMeaning URI is for a given text, applying steps such as reducing English words to their root form and removing special characters.
54+
55+
Example code:
56+
57+
```javascript
58+
cbm.getURI('a (big) dog!'); //-> big_dog
59+
```
60+
61+
### `.search(...args)`
62+
63+
This method finds all the functions that correspond to given nodes and returns an array containing them. It can be called with two different ways. Either by providing only an object containing the search parameters or by providing the parameters themselves as arguments. This method is asynchronous and returns a promise that, when fulfilled, returns an object with two properties.`statusCode` which contains the status code of the request and `body` that holds the result set from the query. For a full overview of search parameters, check the [documentation](https://github.com/cbmjs/CallByMeaning/blob/master/docs/GETBYMEANING.md).
64+
65+
Example code:
66+
67+
```javascript
68+
cbm.search({'inputNodes': 'date', 'outputNodes': 'time'}).then((result) => {
69+
if (result.statusCode === 200) console.log('Success!');
70+
// insert code here
71+
}).catch((error) => console.error(error));
72+
73+
cbm.search('date', 'time'}).then((result) => {
74+
if (result.statusCode === 200) console.log('Success!');
75+
// insert code here
76+
}).catch((error) => console.error(error));
77+
```
78+
79+
### `.call(...args)`
80+
81+
This method takes the search parameters and after finding an appropriate function - a function with the same concepts as inputs and outputs, but (maybe) in different units, that is - executes it and returns the result. If the (optional) argument `returnCode` is set to true, it instead returns the .js file's name and the description of the function. It can be called with two different ways. Either by providing only an object containing the search parameters (and maybe the optional returnCode as a second argument) or by providing the parameters themselves as arguments. This method is asynchronous and returns a promise that, when fulfilled, returns an object with two properties.`statusCode` which contains the status code of the request and `body` that holds the result set from the query. For a full overview of search parameters, check the [documentation](https://github.com/cbmjs/CallByMeaning/blob/master/docs/CALLBYMEANING.md).
82+
83+
Example code:
84+
85+
```javascript
86+
const bday = new Date(1994, 2, 24);
87+
88+
cbm.call({
89+
'inputNodes': 'date',
90+
// 'date' doesn't have a unit, so we can omit it, or pass {'inputUnits': null} or {'inputUnits': []} or {'inputUnits: '-'} or {'inputUnits': 'date'}
91+
'inputVars': bday,
92+
'outputNodes': 'time',
93+
'outputUnits': 'seconds'
94+
}.then((result) => {
95+
if (result.statusCode === 200) console.log('Success!');
96+
// insert code here
97+
}).catch((error) => console.error(error));
98+
99+
cbm.call('date', null, 'time', 'seconds').then(...);
100+
cbm.call('date', null, 'time', 'seconds', true).then(...); // If we want the source code
101+
```
102+
103+
### `.getCode(fileName)`
104+
105+
This method acts as a small helper to the usage of `.search` and `.call` methods. It takes the `name` of a .js file in the server and returns its code in plain text.
106+
107+
Example code:
108+
109+
```javascript
110+
let code = cbm.getCode('getTime.js');
111+
const getTime = eval(code);
112+
getTime();
113+
```
114+
115+
## `.create(params[, type])`
116+
117+
This method creates a document in the server if it doesn't exist or modifies it, if it does. It accepts a [params](https://github.com/cbmjs/CallByMeaning/blob/master/docs/MODELS.md) object with the document parameters as its first argument and a string containing the type of the document. It can be one of `node`, `function`, `relation`. If it isn't provided, it defaults to `node`. This method is asynchronous and returns a promise that, when fulfilled, returns a boolean, depending of its success.
118+
119+
Example code:
120+
121+
```javascript
122+
let params = {
123+
name: 'aNode',
124+
desc: 'aDescription',
125+
};
126+
cbm.create(params);
127+
```
128+
129+
```javascript
130+
let params = {
131+
name: 'aFunction',
132+
desc: 'aDescription',
133+
argsNames: 'someArg',
134+
argsUnits: 'someUnit',
135+
returnsNames: 'someReturn',
136+
returnsUnits: 'someUnit',
137+
};
138+
cbm.create(params, 'function');
139+
140+
params.codeFile = __dirname.concat('/someFile.js');
141+
(async () => {
142+
let res = await cbm.create(params, 'function');
143+
return res;
144+
})().then((res) => console.log(res));
145+
```
146+
147+
```javascript
148+
let params = {
149+
name: 'unitConversion',
150+
start: 'meters',
151+
end: 'feet',
152+
mathRelation: '0.3 * start'
153+
}
154+
cbm.create(params, 'relation')
155+
```
156+
157+
## Unit Tests
158+
159+
Run tests via the command `npm test`
160+
161+
---
162+
163+
## License
164+
165+
[AGPL-3.0 license](./LICENSE).

codecov.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ignore:
2+
- lib/.*

index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class CallByMeaning {
2+
constructor(host) {
3+
this.host = 'https://call-by-meaning.herokuapp.com';
4+
if (host) this.host = String(host);
5+
}
6+
fullAddress_(path) {
7+
return this.host.concat(path);
8+
}
9+
}
10+
11+
CallByMeaning.prototype.lookup = require('./src/lookup');
12+
CallByMeaning.prototype.getURI = require('./src/getURI');
13+
CallByMeaning.prototype.search = require('./src/search');
14+
CallByMeaning.prototype.call = require('./src/call');
15+
CallByMeaning.prototype.getCode = require('./src/getCode');
16+
CallByMeaning.prototype.create = require('./src/create');
17+
18+
module.exports = CallByMeaning;

lib/jsonfn.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
* JSONfn - javascript (both node.js and browser) plugin to stringify,
3+
* parse and clone objects with Functions, Regexp and Date.
4+
*
5+
* Version - 1.1.0
6+
* Copyright (c) Vadim Kiryukhin
7+
* vkiryukhin @ gmail.com
8+
* http://www.eslinstructor.net/jsonfn/
9+
*
10+
* Licensed under the MIT license ( http://www.opensource.org/licenses/mit-license.php )
11+
*
12+
* USAGE:
13+
* browser:
14+
* JSONfn.stringify(obj);
15+
* JSONfn.parse(str[, date2obj]);
16+
* JSONfn.clone(obj[, date2obj]);
17+
*
18+
* nodejs:
19+
* var JSONfn = require('path/to/json-fn');
20+
* JSONfn.stringify(obj);
21+
* JSONfn.parse(str[, date2obj]);
22+
* JSONfn.clone(obj[, date2obj]);
23+
*
24+
*
25+
* @obj - Object;
26+
* @str - String, which is returned by JSONfn.stringify() function;
27+
* @date2obj - Boolean (optional); if true, date string in ISO8061 format
28+
* is converted into a Date object; otherwise, it is left as a String.
29+
*/
30+
31+
(function (exports) {
32+
'use strict';
33+
34+
exports.stringify = function (obj) {
35+
36+
return JSON.stringify(obj, function (key, value) {
37+
var fnBody;
38+
if (value instanceof Function || typeof value == 'function') {
39+
40+
41+
fnBody = value.toString();
42+
43+
if (fnBody.length < 8 || fnBody.substring(0, 8) !== 'function') { //this is ES6 Arrow Function
44+
return '_NuFrRa_' + fnBody;
45+
}
46+
return fnBody;
47+
}
48+
if (value instanceof RegExp) {
49+
return '_PxEgEr_' + value;
50+
}
51+
return value;
52+
});
53+
};
54+
55+
exports.parse = function (str, date2obj) {
56+
57+
var iso8061 = date2obj ? /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/ : false;
58+
59+
return JSON.parse(str, function (key, value) {
60+
var prefix;
61+
62+
if (typeof value != 'string') {
63+
return value;
64+
}
65+
if (value.length < 8) {
66+
return value;
67+
}
68+
69+
prefix = value.substring(0, 8);
70+
71+
if (iso8061 && value.match(iso8061)) {
72+
return new Date(value);
73+
}
74+
if (prefix === 'function') {
75+
return eval('(' + value + ')');
76+
}
77+
if (prefix === '_PxEgEr_') {
78+
return eval(value.slice(8));
79+
}
80+
if (prefix === '_NuFrRa_') {
81+
return eval(value.slice(8));
82+
}
83+
84+
return value;
85+
});
86+
};
87+
88+
exports.clone = function (obj, date2obj) {
89+
return exports.parse(exports.stringify(obj), date2obj);
90+
};
91+
92+
}(exports));

0 commit comments

Comments
 (0)