Skip to content

Commit bdd310d

Browse files
committed
groupping by family name and new convert() method
1 parent 7e0b647 commit bdd310d

12 files changed

Lines changed: 124 additions & 34 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ console.log(snippet.curl({
113113
}));
114114

115115
// generate nodeJS output
116-
console.log(snippet.nodejs());
116+
console.log(snippet.convert('node'));
117117
```
118118

119119
## Documentation

‎src/index.js‎

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,47 @@ HTTPSnippet.prototype.getSource = function () {
6060
return this.source;
6161
};
6262

63-
// add each target as a prototype
64-
for (var lang in targets) {
65-
HTTPSnippet.prototype[lang] = targets[lang];
66-
}
63+
HTTPSnippet.prototype.convert = function (familyName, target, opts) {
64+
if (!opts && target) {
65+
opts = target;
66+
}
67+
68+
// does it exist?
69+
if (targets[familyName] === undefined) {
70+
return false;
71+
}
72+
73+
// isolate the family
74+
var family = targets[familyName];
75+
76+
// childless targets
77+
if (typeof family === 'function') {
78+
return family.call(this, opts);
79+
}
80+
81+
// find the first possibel target
82+
var firstTarget = Object.keys(family).pop();
83+
84+
// shorthand
85+
if (typeof target === 'object') {
86+
target = firstTarget;
87+
}
88+
89+
// asking for a particular target
90+
if (typeof target === 'string') {
91+
// attempt to call the first one we find
92+
if (typeof family[target] !== 'function') {
93+
target = firstTarget;
94+
}
95+
96+
// last chance
97+
if (typeof family[target] === 'function') {
98+
return family[target].call(this, opts);
99+
}
100+
}
101+
102+
return false;
103+
};
67104

68105
// exports
69106

‎src/targets/node/index.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
module.exports = require('requireindex')(__dirname);
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ module.exports = function (options) {
6565

6666
module.exports.info = function () {
6767
return {
68-
key: 'node',
68+
family: 'node',
69+
key: 'native',
6970
ext: '.js',
7071
title: 'Node.JS',
7172
link: 'http://nodejs.org/api/http.html#http_http_request_options_callback',
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ module.exports = function (options) {
104104

105105
module.exports.info = function () {
106106
return {
107-
key: 'php',
107+
family: 'php',
108+
key: 'curl',
108109
ext: '.php',
109110
title: 'PHP',
110111
link: 'http://php.net/manual/en/book.curl.php',

‎src/targets/php/index.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
module.exports = require('requireindex')(__dirname);

‎test/info.js‎

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,58 @@ var async = require('async');
44
var should = require('should');
55
var targets = require('../src/targets');
66

7-
async.each(Object.keys(targets), function (target) {
8-
describe(target + '.info()', function () {
9-
it('should have info method', function (done) {
10-
targets[target].should.have.property('info');
11-
targets[target].info.should.be.a.Function;
7+
var isSingleTarget = function (key, cb) {
8+
cb(typeof targets[key] === 'function');
9+
};
1210

13-
done();
11+
var isTargetFamily = function (key, cb) {
12+
cb(typeof targets[key] !== 'function');
13+
};
14+
15+
async.filter(Object.keys(targets), isSingleTarget, function (results) {
16+
async.each(results, function (key) {
17+
describe(key + '.info()', function () {
18+
it('should have info method', function (done) {
19+
var target = targets[key];
20+
21+
target.should.have.property('info');
22+
target.info.should.be.a.Function;
23+
24+
var info = target.info();
25+
26+
info.key.should.be.a.String.and.equal(key);
27+
info.should.not.have.property('family');
28+
29+
info.ext.should.be.a.String;
30+
info.title.should.be.a.String;
31+
32+
done();
33+
});
34+
});
35+
});
36+
});
37+
38+
async.filter(Object.keys(targets), isTargetFamily, function (families) {
39+
async.each(families, function (family) {
40+
async.each(Object.keys(targets[family]), function (key) {
41+
var target = targets[family][key];
42+
43+
describe(family + '[' + key + ']' + '.info()', function () {
44+
it('should have info method', function (done) {
45+
target.should.have.property('info');
46+
target.info.should.be.a.Function;
47+
48+
var info = target.info();
49+
50+
info.key.should.be.a.String.and.equal(key);
51+
52+
info.family.should.be.a.String;
53+
info.ext.should.be.a.String;
54+
info.title.should.be.a.String;
55+
56+
done();
57+
});
58+
});
1459
});
1560
});
1661
});

‎test/targets/curl.js‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var should = require('should');
66

77
describe('cURL', function () {
88
it('should convert simple request to cURL command', function (done) {
9-
var result = new HTTPSnippet(fixtures.simple).curl({
9+
var result = new HTTPSnippet(fixtures.simple).convert('curl', {
1010
indent: false
1111
});
1212

@@ -17,7 +17,7 @@ describe('cURL', function () {
1717
});
1818

1919
it('should use short options', function (done) {
20-
var result = new HTTPSnippet(fixtures.simple).curl({
20+
var result = new HTTPSnippet(fixtures.simple).convert('curl', {
2121
short: true,
2222
indent: false
2323
});
@@ -29,7 +29,7 @@ describe('cURL', function () {
2929
});
3030

3131
it('should use --http1.0 for HTTP/1.0', function (done) {
32-
var result = new HTTPSnippet(fixtures.http1).curl({
32+
var result = new HTTPSnippet(fixtures.http1).convert('curl', {
3333
indent: false
3434
});
3535

@@ -40,7 +40,7 @@ describe('cURL', function () {
4040
});
4141

4242
it('should use custom indentation', function (done) {
43-
var result = new HTTPSnippet(fixtures.simple).curl({
43+
var result = new HTTPSnippet(fixtures.simple).convert('curl', {
4444
indent: '@'
4545
});
4646

‎test/targets/httpie.js‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var should = require('should');
66

77
describe('HTTPie', function () {
88
it('should convert simple request to HTTPie command', function (done) {
9-
var result = new HTTPSnippet(fixtures.simple).httpie({
9+
var result = new HTTPSnippet(fixtures.simple).convert('httpie', {
1010
indent: false
1111
});
1212

@@ -17,7 +17,7 @@ describe('HTTPie', function () {
1717
});
1818

1919
it('should ask for verbose output', function (done) {
20-
var result = new HTTPSnippet(fixtures.simple).httpie({
20+
var result = new HTTPSnippet(fixtures.simple).convert('httpie', {
2121
indent: false,
2222
verbose: true
2323
});
@@ -29,7 +29,7 @@ describe('HTTPie', function () {
2929
});
3030

3131
it('should add flags', function (done) {
32-
var result = new HTTPSnippet(fixtures.short).httpie({
32+
var result = new HTTPSnippet(fixtures.short).convert('httpie', {
3333
indent: false,
3434
cert: 'foo',
3535
verbose: true
@@ -42,7 +42,7 @@ describe('HTTPie', function () {
4242
});
4343

4444
it('should use custom indentation', function (done) {
45-
var result = new HTTPSnippet(fixtures.simple).httpie({
45+
var result = new HTTPSnippet(fixtures.simple).convert('httpie', {
4646
indent: '@'
4747
});
4848

@@ -53,7 +53,7 @@ describe('HTTPie', function () {
5353
});
5454

5555
it('should use queryString parameters', function (done) {
56-
var result = new HTTPSnippet(fixtures.query).httpie({
56+
var result = new HTTPSnippet(fixtures.query).convert('httpie', {
5757
indent: false,
5858
queryParams: true
5959
});
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
'use strict';
22

3-
var fixtures = require('../fixtures');
4-
var HTTPSnippet = require('../../src');
3+
var fixtures = require('../../fixtures');
4+
var HTTPSnippet = require('../../../src');
55
var should = require('should');
66

77
describe('Node.js', function () {
8-
it('should convert full request to cURL command', function (done) {
9-
var result = new HTTPSnippet(fixtures.full).node({
8+
it('should convert full request to a nodejs script', function (done) {
9+
var result = new HTTPSnippet(fixtures.full).convert('node', {
1010
indent: '\t'
1111
});
1212

0 commit comments

Comments
 (0)