httpsnippet
Advanced tools
Comparing version 1.13.0 to 1.14.0
{ | ||
"version": "1.13.0", | ||
"version": "1.14.0", | ||
"name": "httpsnippet", | ||
@@ -4,0 +4,0 @@ "description": "HTTP Request snippet generator for *most* languages", |
'use strict' | ||
var util = require('util') | ||
/** | ||
@@ -19,10 +21,25 @@ * Helper object to format and aggragate lines of code. | ||
/** | ||
* Add given indentation level to given string | ||
* @param {number} [indentationLevel=0] Desired level of indentation for this line | ||
* @param {string} line Line of code | ||
* Add given indentation level to given string and format the string (variadic) | ||
* @param {number} [indentationLevel=0] - Desired level of indentation for this line | ||
* @param {string} line - Line of code. Can contain formatting placeholders | ||
* @param {...anyobject} - Parameter to bind to `line`'s formatting placeholders | ||
* @return {string} | ||
* | ||
* @example | ||
* var builder = CodeBuilder('\t') | ||
* | ||
* builder.buildLine('console.log("hello world")') | ||
* // returns: 'console.log("hello world")' | ||
* | ||
* builder.buildLine(2, 'console.log("hello world")') | ||
* // returns: 'console.log("\t\thello world")' | ||
* | ||
* builder.buildLine(2, 'console.log("%s %s")', 'hello', 'world') | ||
* // returns: 'console.log("\t\thello world")' | ||
*/ | ||
CodeBuilder.prototype.buildLine = function (indentationLevel, line) { | ||
var lineIndentation = '' | ||
var slice = 2 | ||
if (Object.prototype.toString.call(indentationLevel) === '[object String]') { | ||
slice = 1 | ||
line = indentationLevel | ||
@@ -39,7 +56,10 @@ indentationLevel = 0 | ||
return lineIndentation + line | ||
var format = Array.prototype.slice.call(arguments, slice, arguments.length) | ||
format.unshift(lineIndentation + line) | ||
return util.format.apply(this, format) | ||
} | ||
/** | ||
* Add a line at the top of current lines with given indentation level | ||
* Invoke buildLine() and add the line at the top of current lines | ||
* @param {number} [indentationLevel=0] Desired level of indentation for this line | ||
@@ -49,4 +69,4 @@ * @param {string} line Line of code | ||
*/ | ||
CodeBuilder.prototype.unshift = function (indentationLevel, str) { | ||
this.code.unshift(this.buildLine(indentationLevel, str)) | ||
CodeBuilder.prototype.unshift = function () { | ||
this.code.unshift(this.buildLine.apply(this, arguments)) | ||
@@ -57,3 +77,3 @@ return this | ||
/** | ||
* Add a line at the end of current lines with given indentation level | ||
* Invoke buildLine() and add the line at the bottom of current lines | ||
* @param {number} [indentationLevel=0] Desired level of indentation for this line | ||
@@ -63,4 +83,4 @@ * @param {string} line Line of code | ||
*/ | ||
CodeBuilder.prototype.push = function (indentationLevel, str) { | ||
this.code.push(this.buildLine(indentationLevel, str)) | ||
CodeBuilder.prototype.push = function () { | ||
this.code.push(this.buildLine.apply(this, arguments)) | ||
@@ -67,0 +87,0 @@ return this |
@@ -67,3 +67,3 @@ /** | ||
code.push(1, 'client := http.Client{') | ||
.push(2, util.format('Timeout: time.Duration(%s * time.Second),', opts.timeout)) | ||
.push(2, 'Timeout: time.Duration(%s * time.Second),', opts.timeout) | ||
.push(1, '}') | ||
@@ -75,3 +75,3 @@ .blank() | ||
code.push(1, util.format('url := "%s"', source.fullUrl)) | ||
code.push(1, 'url := "%s"', source.fullUrl) | ||
.blank() | ||
@@ -81,8 +81,8 @@ | ||
if (source.postData.text) { | ||
code.push(1, util.format('payload := strings.NewReader(%s)', JSON.stringify(source.postData.text))) | ||
code.push(1, 'payload := strings.NewReader(%s)', JSON.stringify(source.postData.text)) | ||
.blank() | ||
.push(1, util.format('req, %s := http.NewRequest("%s", url, payload)', errorPlaceholder, source.method)) | ||
.push(1, 'req, %s := http.NewRequest("%s", url, payload)', errorPlaceholder, source.method) | ||
.blank() | ||
} else { | ||
code.push(1, util.format('req, %s := http.NewRequest("%s", url, nil)', errorPlaceholder, source.method)) | ||
code.push(1, 'req, %s := http.NewRequest("%s", url, nil)', errorPlaceholder, source.method) | ||
.blank() | ||
@@ -96,3 +96,3 @@ } | ||
Object.keys(source.allHeaders).map(function (key) { | ||
code.push(1, util.format('req.Header.Add("%s", "%s")', key, source.allHeaders[key])) | ||
code.push(1, 'req.Header.Add("%s", "%s")', key, source.allHeaders[key]) | ||
}) | ||
@@ -103,3 +103,3 @@ code.blank() | ||
// Make request | ||
code.push(1, util.format('res, %s := %s.Do(req)', errorPlaceholder, client)) | ||
code.push(1, 'res, %s := %s.Do(req)', errorPlaceholder, client) | ||
errorCheck() | ||
@@ -111,3 +111,3 @@ | ||
.push(1, 'defer res.Body.Close()') | ||
.push(1, util.format('body, %s := ioutil.ReadAll(res.Body)', errorPlaceholder)) | ||
.push(1, 'body, %s := ioutil.ReadAll(res.Body)', errorPlaceholder) | ||
errorCheck() | ||
@@ -114,0 +114,0 @@ } |
@@ -26,5 +26,5 @@ /** | ||
if (methods.indexOf(source.method.toUpperCase()) === -1) { | ||
code.push(util.format('HttpResponse<String> response = Unirest.customMethod("%s","%s")', source.method.toUpperCase(), source.fullUrl)) | ||
code.push('HttpResponse<String> response = Unirest.customMethod("%s","%s")', source.method.toUpperCase(), source.fullUrl) | ||
} else { | ||
code.push(util.format('HttpResponse<String> response = Unirest.%s("%s")', source.method.toLowerCase(), source.fullUrl)) | ||
code.push('HttpResponse<String> response = Unirest.%s("%s")', source.method.toLowerCase(), source.fullUrl) | ||
} | ||
@@ -38,3 +38,3 @@ | ||
headers.map(function (key) { | ||
code.push(1, util.format('.header("%s", "%s")', key, source.allHeaders[key])) | ||
code.push(1, '.header("%s", "%s")', key, source.allHeaders[key]) | ||
}) | ||
@@ -44,3 +44,3 @@ } | ||
if (source.postData.text) { | ||
code.push(1, util.format('.body(%s)', JSON.stringify(source.postData.text))) | ||
code.push(1, '.body(%s)', JSON.stringify(source.postData.text)) | ||
} | ||
@@ -47,0 +47,0 @@ |
@@ -45,3 +45,3 @@ /** | ||
source.postData.params.map(function (param) { | ||
code.push(util.format('form.append(%s, %s);', JSON.stringify(param.name), JSON.stringify(param.value || param.fileName || ''))) | ||
code.push('form.append(%s, %s);', JSON.stringify(param.name), JSON.stringify(param.value || param.fileName || '')) | ||
}) | ||
@@ -48,0 +48,0 @@ |
@@ -26,3 +26,3 @@ /** | ||
case 'application/json': | ||
code.push(util.format('var data = JSON.stringify(%s);', JSON.stringify(source.postData.jsonObj, null, opts.indent))) | ||
code.push('var data = JSON.stringify(%s);', JSON.stringify(source.postData.jsonObj, null, opts.indent)) | ||
.push(null) | ||
@@ -35,3 +35,3 @@ break | ||
source.postData.params.map(function (param) { | ||
code.push(util.format('data.append(%s, %s);', JSON.stringify(param.name), JSON.stringify(param.value || param.fileName || ''))) | ||
code.push('data.append(%s, %s);', JSON.stringify(param.name), JSON.stringify(param.value || param.fileName || '')) | ||
}) | ||
@@ -48,3 +48,3 @@ | ||
default: | ||
code.push(util.format('var data = %s;', JSON.stringify(source.postData.text || null))) | ||
code.push('var data = %s;', JSON.stringify(source.postData.text || null)) | ||
.blank() | ||
@@ -66,6 +66,6 @@ } | ||
.blank() | ||
.push(util.format('xhr.open(%s, %s);', JSON.stringify(source.method), JSON.stringify(source.fullUrl))) | ||
.push('xhr.open(%s, %s);', JSON.stringify(source.method), JSON.stringify(source.fullUrl)) | ||
Object.keys(source.allHeaders).map(function (key) { | ||
code.push(util.format('xhr.setRequestHeader(%s, %s);', JSON.stringify(key), JSON.stringify(source.allHeaders[key]))) | ||
code.push('xhr.setRequestHeader(%s, %s);', JSON.stringify(key), JSON.stringify(source.allHeaders[key])) | ||
}) | ||
@@ -72,0 +72,0 @@ |
@@ -31,6 +31,6 @@ /** | ||
code.push(util.format('var http = require("%s");', source.uriObj.protocol.replace(':', ''))) | ||
code.push('var http = require("%s");', source.uriObj.protocol.replace(':', '')) | ||
code.blank() | ||
.push(util.format('var options = %s;', JSON.stringify(reqOpts, null, opts.indent))) | ||
.push('var options = %s;', JSON.stringify(reqOpts, null, opts.indent)) | ||
.blank() | ||
@@ -55,5 +55,5 @@ .push('var req = http.request(options, function (res) {') | ||
code.unshift('var qs = require("querystring");') | ||
code.push(util.format('req.write(qs.stringify(%s));', util.inspect(source.postData.paramsObj, { | ||
code.push('req.write(qs.stringify(%s));', util.inspect(source.postData.paramsObj, { | ||
depth: null | ||
}))) | ||
})) | ||
} | ||
@@ -64,5 +64,5 @@ break | ||
if (source.postData.jsonObj) { | ||
code.push(util.format('req.write(JSON.stringify(%s));', util.inspect(source.postData.jsonObj, { | ||
code.push('req.write(JSON.stringify(%s));', util.inspect(source.postData.jsonObj, { | ||
depth: null | ||
}))) | ||
})) | ||
} | ||
@@ -73,3 +73,3 @@ break | ||
if (source.postData.text) { | ||
code.push(util.format('req.write(%s);', JSON.stringify(source.postData.text, null, opts.indent))) | ||
code.push('req.write(%s);', JSON.stringify(source.postData.text, null, opts.indent)) | ||
} | ||
@@ -76,0 +76,0 @@ } |
@@ -97,3 +97,3 @@ /** | ||
source.cookies.map(function (cookie) { | ||
code.push(util.format('jar.setCookie(request.cookie("%s=%s"), "%s");', encodeURIComponent(cookie.name), encodeURIComponent(cookie.value), url)) | ||
code.push('jar.setCookie(request.cookie("%s=%s"), "%s");', encodeURIComponent(cookie.name), encodeURIComponent(cookie.value), url) | ||
}) | ||
@@ -107,8 +107,7 @@ code.blank() | ||
code.push(util.format('var options = %s;', util.inspect(reqOpts, { | ||
depth: null | ||
}))) | ||
code.push('var options = %s;', util.inspect(reqOpts, { depth: null })) | ||
.blank() | ||
code.push(util.format('request(options, %s', 'function (error, response, body) {')) | ||
.push(1, 'if (error) throw new Error(error);') | ||
@@ -115,0 +114,0 @@ .blank() |
@@ -26,3 +26,3 @@ /** | ||
.blank() | ||
.push(util.format('var req = unirest("%s", "%s");', source.method, source.url)) | ||
.push('var req = unirest("%s", "%s");', source.method, source.url) | ||
.blank() | ||
@@ -34,3 +34,3 @@ | ||
source.cookies.forEach(function (cookie) { | ||
code.push(util.format('CookieJar.add("%s=%s","%s");', encodeURIComponent(cookie.name), encodeURIComponent(cookie.value), source.url)) | ||
code.push('CookieJar.add("%s=%s","%s");', encodeURIComponent(cookie.name), encodeURIComponent(cookie.value), source.url) | ||
}) | ||
@@ -43,3 +43,3 @@ | ||
if (Object.keys(source.queryObj).length) { | ||
code.push(util.format('req.query(%s);', JSON.stringify(source.queryObj, null, opts.indent))) | ||
code.push('req.query(%s);', JSON.stringify(source.queryObj, null, opts.indent)) | ||
.blank() | ||
@@ -49,3 +49,3 @@ } | ||
if (Object.keys(source.headersObj).length) { | ||
code.push(util.format('req.headers(%s);', JSON.stringify(source.headersObj, null, opts.indent))) | ||
code.push('req.headers(%s);', JSON.stringify(source.headersObj, null, opts.indent)) | ||
.blank() | ||
@@ -57,3 +57,3 @@ } | ||
if (source.postData.paramsObj) { | ||
code.push(util.format('req.form(%s);', JSON.stringify(source.postData.paramsObj, null, opts.indent))) | ||
code.push('req.form(%s);', JSON.stringify(source.postData.paramsObj, null, opts.indent)) | ||
} | ||
@@ -65,3 +65,3 @@ break | ||
code.push('req.type("json");') | ||
.push(util.format('req.send(%s);', JSON.stringify(source.postData.jsonObj, null, opts.indent))) | ||
.push('req.send(%s);', JSON.stringify(source.postData.jsonObj, null, opts.indent)) | ||
} | ||
@@ -93,3 +93,3 @@ break | ||
code.push(util.format('req.multipart(%s);', JSON.stringify(multipart, null, opts.indent))) | ||
code.push('req.multipart(%s);', JSON.stringify(multipart, null, opts.indent)) | ||
break | ||
@@ -99,3 +99,3 @@ | ||
if (source.postData.text) { | ||
code.push(opts.indent + util.format('req.send(%s);', JSON.stringify(source.postData.text, null, opts.indent))) | ||
code.push(opts.indent + 'req.send(%s);', JSON.stringify(source.postData.text, null, opts.indent)) | ||
} | ||
@@ -102,0 +102,0 @@ } |
@@ -49,7 +49,7 @@ /** | ||
code.blank() | ||
.push(util.format('NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"%s=%s" dataUsingEncoding:NSUTF8StringEncoding]];', | ||
source.postData.params[0].name, source.postData.params[0].value)) | ||
.push('NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"%s=%s" dataUsingEncoding:NSUTF8StringEncoding]];', | ||
source.postData.params[0].name, source.postData.params[0].value) | ||
for (var i = 1, len = source.postData.params.length; i < len; i++) { | ||
code.push(util.format('[postData appendData:[@"&%s=%s" dataUsingEncoding:NSUTF8StringEncoding]];', | ||
source.postData.params[i].name, source.postData.params[i].value)) | ||
code.push('[postData appendData:[@"&%s=%s" dataUsingEncoding:NSUTF8StringEncoding]];', | ||
source.postData.params[i].name, source.postData.params[i].value) | ||
} | ||
@@ -71,3 +71,3 @@ break | ||
code.push(helpers.nsDeclaration('NSArray', 'parameters', source.postData.params, opts.pretty)) | ||
.push(util.format('NSString *boundary = @"%s";', source.postData.boundary)) | ||
.push('NSString *boundary = @"%s";', source.postData.boundary) | ||
.blank() | ||
@@ -74,0 +74,0 @@ .push('NSError *error;') |
@@ -28,3 +28,3 @@ /** | ||
.blank() | ||
.push(util.format('let uri = Uri.of_string "%s" in', source.fullUrl)) | ||
.push('let uri = Uri.of_string "%s" in', source.fullUrl) | ||
@@ -38,3 +38,3 @@ // Add headers, including the cookies | ||
headers.map(function (key) { | ||
code.push(1, util.format('|> fun h -> Header.add h "%s" "%s"', key, source.allHeaders[key])) | ||
code.push(1, '|> fun h -> Header.add h "%s" "%s"', key, source.allHeaders[key]) | ||
}) | ||
@@ -48,3 +48,3 @@ | ||
// Just text | ||
code.push(util.format('let body = Cohttp_lwt_body.of_string %s in', JSON.stringify(source.postData.text))) | ||
code.push('let body = Cohttp_lwt_body.of_string %s in', JSON.stringify(source.postData.text)) | ||
} | ||
@@ -55,7 +55,7 @@ | ||
code.push(util.format('Client.call %s%s%s uri', | ||
code.push('Client.call %s%s%s uri', | ||
headers.length ? '~headers ' : '', | ||
source.postData.text ? '~body ' : '', | ||
(methods.indexOf(source.method.toLowerCase()) >= 0 ? ('`' + source.method.toUpperCase()) : '(Code.method_of_string "' + source.method + '")') | ||
)) | ||
) | ||
@@ -62,0 +62,0 @@ // Catch result |
@@ -33,12 +33,12 @@ /** | ||
if (!~helpers.methods.indexOf(source.method.toUpperCase())) { | ||
code.push(util.format('HttpRequest::methodRegister(\'%s\');', source.method)) | ||
code.push('HttpRequest::methodRegister(\'%s\');', source.method) | ||
} | ||
code.push('$request = new HttpRequest();') | ||
.push(util.format('$request->setUrl(%s);', helpers.convert(source.url))) | ||
.push('$request->setUrl(%s);', helpers.convert(source.url)) | ||
if (~helpers.methods.indexOf(source.method.toUpperCase())) { | ||
code.push(util.format('$request->setMethod(HTTP_METH_%s);', source.method.toUpperCase())) | ||
code.push('$request->setMethod(HTTP_METH_%s);', source.method.toUpperCase()) | ||
} else { | ||
code.push(util.format('$request->setMethod(HttpRequest::HTTP_METH_%s);', source.method.toUpperCase())) | ||
code.push('$request->setMethod(HttpRequest::HTTP_METH_%s);', source.method.toUpperCase()) | ||
} | ||
@@ -49,3 +49,3 @@ | ||
if (Object.keys(source.queryObj).length) { | ||
code.push(util.format('$request->setQueryData(%s);', helpers.convert(source.queryObj, opts.indent))) | ||
code.push('$request->setQueryData(%s);', helpers.convert(source.queryObj, opts.indent)) | ||
.blank() | ||
@@ -55,3 +55,3 @@ } | ||
if (Object.keys(source.headersObj).length) { | ||
code.push(util.format('$request->setHeaders(%s);', helpers.convert(source.headersObj, opts.indent))) | ||
code.push('$request->setHeaders(%s);', helpers.convert(source.headersObj, opts.indent)) | ||
.blank() | ||
@@ -61,3 +61,3 @@ } | ||
if (Object.keys(source.cookiesObj).length) { | ||
code.push(util.format('$request->setCookies(%s);', helpers.convert(source.cookiesObj, opts.indent))) | ||
code.push('$request->setCookies(%s);', helpers.convert(source.cookiesObj, opts.indent)) | ||
.blank() | ||
@@ -68,4 +68,4 @@ } | ||
case 'application/x-www-form-urlencoded': | ||
code.push(util.format('$request->setContentType(%s);', helpers.convert(source.postData.mimeType))) | ||
.push(util.format('$request->setPostFields(%s);', helpers.convert(source.postData.paramsObj, opts.indent))) | ||
code.push('$request->setContentType(%s);', helpers.convert(source.postData.mimeType)) | ||
.push('$request->setPostFields(%s);', helpers.convert(source.postData.paramsObj, opts.indent)) | ||
.blank() | ||
@@ -76,3 +76,3 @@ break | ||
if (source.postData.text) { | ||
code.push(util.format('$request->setBody(%s);', helpers.convert(source.postData.text))) | ||
code.push('$request->setBody(%s);', helpers.convert(source.postData.text)) | ||
.blank() | ||
@@ -79,0 +79,0 @@ } |
@@ -40,3 +40,3 @@ /** | ||
code.push('$body = new http\\Message\\Body;') | ||
.push(util.format('$body->append(new http\\QueryString(%s));', helpers.convert(source.postData.paramsObj, opts.indent))) | ||
.push('$body->append(new http\\QueryString(%s));', helpers.convert(source.postData.paramsObj, opts.indent)) | ||
.blank() | ||
@@ -64,6 +64,6 @@ hasBody = true | ||
code.push('$body = new http\\Message\\Body;') | ||
.push(util.format('$body->addForm(%s, %s);', | ||
.push('$body->addForm(%s, %s);', | ||
Object.keys(fields).length ? helpers.convert(fields, opts.indent) : 'NULL', | ||
files.length ? helpers.convert(files, opts.indent) : 'NULL' | ||
)) | ||
) | ||
@@ -83,3 +83,3 @@ // remove the contentType header | ||
code.push('$body = new http\\Message\\Body;') | ||
.push(util.format('$body->append(%s);', helpers.convert(source.postData.text))) | ||
.push('$body->append(%s);', helpers.convert(source.postData.text)) | ||
.blank() | ||
@@ -90,7 +90,7 @@ hasBody = true | ||
code.push(util.format('$request->setRequestUrl(%s);', helpers.convert(source.url))) | ||
.push(util.format('$request->setRequestMethod(%s);', helpers.convert(source.method))) | ||
code.push('$request->setRequestUrl(%s);', helpers.convert(source.url)) | ||
.push('$request->setRequestMethod(%s);', helpers.convert(source.method)) | ||
if (hasBody) { | ||
code.push(util.format('$request->setBody($body);')) | ||
code.push('$request->setBody($body);') | ||
.blank() | ||
@@ -100,3 +100,3 @@ } | ||
if (Object.keys(source.queryObj).length) { | ||
code.push(util.format('$request->setQuery(new http\\QueryString(%s));', helpers.convert(source.queryObj, opts.indent))) | ||
code.push('$request->setQuery(new http\\QueryString(%s));', helpers.convert(source.queryObj, opts.indent)) | ||
.blank() | ||
@@ -106,3 +106,3 @@ } | ||
if (Object.keys(source.headersObj).length) { | ||
code.push(util.format('$request->setHeaders(%s);', helpers.convert(source.headersObj, opts.indent))) | ||
code.push('$request->setHeaders(%s);', helpers.convert(source.headersObj, opts.indent)) | ||
.blank() | ||
@@ -113,3 +113,3 @@ } | ||
code.blank() | ||
.push(util.format('$client->setCookies(%s);', helpers.convert(source.cookiesObj, opts.indent))) | ||
.push('$client->setCookies(%s);', helpers.convert(source.cookiesObj, opts.indent)) | ||
.blank() | ||
@@ -116,0 +116,0 @@ } |
@@ -13,3 +13,2 @@ /** | ||
var util = require('util') | ||
var CodeBuilder = require('../../helpers/code-builder') | ||
@@ -26,6 +25,6 @@ | ||
if (protocol === 'https:') { | ||
code.push(util.format('conn = http.client.HTTPSConnection("%s")', source.uriObj.host)) | ||
code.push('conn = http.client.HTTPSConnection("%s")', source.uriObj.host) | ||
.blank() | ||
} else { | ||
code.push(util.format('conn = http.client.HTTPConnection("%s")', source.uriObj.host)) | ||
code.push('conn = http.client.HTTPConnection("%s")', source.uriObj.host) | ||
.blank() | ||
@@ -37,3 +36,3 @@ } | ||
if (payload) { | ||
code.push(util.format('payload = %s', payload)) | ||
code.push('payload = %s', payload) | ||
.blank() | ||
@@ -48,17 +47,18 @@ } | ||
for (header in headers) { | ||
code.push(util.format('headers = { \'%s\': "%s" }', header, headers[header])) | ||
code.push('headers = { \'%s\': "%s" }', header, headers[header]) | ||
.blank() | ||
} | ||
} else if (headerCount > 1) { | ||
var headerLine | ||
var count = 1 | ||
code.push('headers = {') | ||
for (header in headers) { | ||
if (count++ !== headerCount) { | ||
headerLine = util.format(' \'%s\': "%s",', header, headers[header]) | ||
code.push(' \'%s\': "%s",', header, headers[header]) | ||
} else { | ||
headerLine = util.format(' \'%s\': "%s"', header, headers[header]) | ||
code.push(' \'%s\': "%s"', header, headers[header]) | ||
} | ||
code.push(headerLine) | ||
} | ||
code.push(' }') | ||
@@ -72,9 +72,9 @@ .blank() | ||
if (payload && headerCount) { | ||
code.push(util.format('conn.request("%s", "%s", payload, headers)', method, path)) | ||
code.push('conn.request("%s", "%s", payload, headers)', method, path) | ||
} else if (payload && !headerCount) { | ||
code.push(util.format('conn.request("%s", "%s", payload)', method, path)) | ||
code.push('conn.request("%s", "%s", payload)', method, path) | ||
} else if (!payload && headerCount) { | ||
code.push(util.format('conn.request("%s", "%s", headers=headers)', method, path)) | ||
code.push('conn.request("%s", "%s", headers=headers)', method, path) | ||
} else { | ||
code.push(util.format('conn.request("%s", "%s")', method, path)) | ||
code.push('conn.request("%s", "%s")', method, path) | ||
} | ||
@@ -81,0 +81,0 @@ |
@@ -25,3 +25,3 @@ /** | ||
// Set URL | ||
code.push(util.format('url = "%s"', source.url)) | ||
code.push('url = "%s"', source.url) | ||
.blank() | ||
@@ -41,3 +41,3 @@ | ||
if (payload) { | ||
code.push(util.format('payload = %s', payload)) | ||
code.push('payload = %s', payload) | ||
} | ||
@@ -52,7 +52,6 @@ | ||
for (header in headers) { | ||
code.push(util.format('headers = {\'%s\': \'%s\'}', header, headers[header])) | ||
code.push('headers = {\'%s\': \'%s\'}', header, headers[header]) | ||
.blank() | ||
} | ||
} else if (headerCount > 1) { | ||
var headerLine | ||
var count = 1 | ||
@@ -64,8 +63,6 @@ | ||
if (count++ !== headerCount) { | ||
headerLine = util.format('\'%s\': "%s",', header, headers[header]) | ||
code.push(1, '\'%s\': "%s",', header, headers[header]) | ||
} else { | ||
headerLine = util.format('\'%s\': "%s"', header, headers[header]) | ||
code.push(1, '\'%s\': "%s"', header, headers[header]) | ||
} | ||
code.push(1, headerLine) | ||
} | ||
@@ -72,0 +69,0 @@ |
'use strict' | ||
var util = require('util') | ||
var CodeBuilder = require('../../helpers/code-builder') | ||
@@ -19,5 +18,5 @@ | ||
if (methods.indexOf(method) < 0) { | ||
code.push(util.format('class Net::HTTP::%s < Net::HTTPRequest', capMethod)) | ||
.push(util.format(' METHOD = \'%s\'', method.toUpperCase())) | ||
.push(util.format(' REQUEST_HAS_BODY = \'%s\'', source.postData.text ? 'true' : 'false')) | ||
code.push('class Net::HTTP::%s < Net::HTTPRequest', capMethod) | ||
.push(' METHOD = \'%s\'', method.toUpperCase()) | ||
.push(' REQUEST_HAS_BODY = \'%s\'', source.postData.text ? 'true' : 'false') | ||
.push(' RESPONSE_HAS_BODY = true') | ||
@@ -28,3 +27,3 @@ .push('end') | ||
code.push(util.format('url = URI("%s")', source.fullUrl)) | ||
code.push('url = URI("%s")', source.fullUrl) | ||
.blank() | ||
@@ -39,3 +38,3 @@ .push('http = Net::HTTP.new(url.host, url.port)') | ||
code.blank() | ||
.push(util.format('request = Net::HTTP::%s.new(url)', capMethod)) | ||
.push('request = Net::HTTP::%s.new(url)', capMethod) | ||
@@ -45,3 +44,3 @@ var headers = Object.keys(source.allHeaders) | ||
headers.map(function (key) { | ||
code.push(util.format('request["%s"] = \'%s\'', key, source.allHeaders[key])) | ||
code.push('request["%s"] = \'%s\'', key, source.allHeaders[key]) | ||
}) | ||
@@ -51,3 +50,3 @@ } | ||
if (source.postData.text) { | ||
code.push(util.format('request.body = %s', JSON.stringify(source.postData.text))) | ||
code.push('request.body = %s', JSON.stringify(source.postData.text)) | ||
} | ||
@@ -54,0 +53,0 @@ |
@@ -25,3 +25,3 @@ /** | ||
code.push(util.format('curl %s %s', opts.short ? '-X' : '--request', source.method)) | ||
code.push('curl %s %s', opts.short ? '-X' : '--request', source.method) | ||
.push(util.format('%s%s', opts.short ? '' : '--url ', helpers.quote(source.fullUrl))) | ||
@@ -36,7 +36,7 @@ | ||
var header = util.format('%s: %s', key, source.headersObj[key]) | ||
code.push(util.format('%s %s', opts.short ? '-H' : '--header', helpers.quote(header))) | ||
code.push('%s %s', opts.short ? '-H' : '--header', helpers.quote(header)) | ||
}) | ||
if (source.allHeaders.cookie) { | ||
code.push(util.format('%s %s', opts.short ? '-b' : '--cookie', helpers.quote(source.allHeaders.cookie))) | ||
code.push('%s %s', opts.short ? '-b' : '--cookie', helpers.quote(source.allHeaders.cookie)) | ||
} | ||
@@ -54,3 +54,3 @@ | ||
code.push(util.format('%s %s', opts.short ? '-F' : '--form', helpers.quote(post))) | ||
code.push('%s %s', opts.short ? '-F' : '--form', helpers.quote(post)) | ||
}) | ||
@@ -62,3 +62,3 @@ break | ||
if (source.postData.text) { | ||
code.push(util.format('%s %s', opts.short ? '-d' : '--data', helpers.escape(helpers.quote(source.postData.text)))) | ||
code.push('%s %s', opts.short ? '-d' : '--data', helpers.escape(helpers.quote(source.postData.text))) | ||
} | ||
@@ -65,0 +65,0 @@ } |
@@ -83,6 +83,6 @@ /** | ||
value.map(function (val) { | ||
code.push(util.format('%s==%s', name, helpers.quote(val))) | ||
code.push('%s==%s', name, helpers.quote(val)) | ||
}) | ||
} else { | ||
code.push(util.format('%s==%s', name, helpers.quote(value))) | ||
code.push('%s==%s', name, helpers.quote(value)) | ||
} | ||
@@ -94,3 +94,3 @@ }) | ||
Object.keys(source.allHeaders).sort().map(function (key) { | ||
code.push(util.format('%s:%s', key, helpers.quote(source.allHeaders[key]))) | ||
code.push('%s:%s', key, helpers.quote(source.allHeaders[key])) | ||
}) | ||
@@ -104,3 +104,3 @@ | ||
source.postData.params.map(function (param) { | ||
code.push(util.format('%s=%s', param.name, helpers.quote(param.value))) | ||
code.push('%s=%s', param.name, helpers.quote(param.value)) | ||
}) | ||
@@ -112,6 +112,6 @@ } | ||
code.unshift(util.format('http %s%s %s', flags.length ? flags.join(' ') + ' ' : '', source.method, helpers.quote(opts.queryParams ? source.url : source.fullUrl))) | ||
code.unshift('http %s%s %s', flags.length ? flags.join(' ') + ' ' : '', source.method, helpers.quote(opts.queryParams ? source.url : source.fullUrl)) | ||
if (raw && source.postData.text) { | ||
code.unshift(util.format('echo %s | ', helpers.quote(source.postData.text))) | ||
code.unshift('echo %s | ', helpers.quote(source.postData.text)) | ||
} | ||
@@ -118,0 +118,0 @@ |
@@ -27,12 +27,12 @@ /** | ||
if (opts.verbose) { | ||
code.push(util.format('wget %s', opts.short ? '-v' : '--verbose')) | ||
code.push('wget %s', opts.short ? '-v' : '--verbose') | ||
} else { | ||
code.push(util.format('wget %s', opts.short ? '-q' : '--quiet')) | ||
code.push('wget %s', opts.short ? '-q' : '--quiet') | ||
} | ||
code.push(util.format('--method %s', helpers.quote(source.method))) | ||
code.push('--method %s', helpers.quote(source.method)) | ||
Object.keys(source.allHeaders).map(function (key) { | ||
var header = util.format('%s: %s', key, source.allHeaders[key]) | ||
code.push(util.format('--header %s', helpers.quote(header))) | ||
code.push('--header %s', helpers.quote(header)) | ||
}) | ||
@@ -45,3 +45,3 @@ | ||
code.push(opts.short ? '-O' : '--output-document') | ||
.push(util.format('- %s', helpers.quote(source.fullUrl))) | ||
.push('- %s', helpers.quote(source.fullUrl)) | ||
@@ -48,0 +48,0 @@ return code.join() |
@@ -50,5 +50,5 @@ /** | ||
code.blank() | ||
.push(util.format('var postData = NSMutableData(data: "%s=%s".dataUsingEncoding(NSUTF8StringEncoding)!)', source.postData.params[0].name, source.postData.params[0].value)) | ||
.push('var postData = NSMutableData(data: "%s=%s".dataUsingEncoding(NSUTF8StringEncoding)!)', source.postData.params[0].name, source.postData.params[0].value) | ||
for (var i = 1, len = source.postData.params.length; i < len; i++) { | ||
code.push(util.format('postData.appendData("&%s=%s".dataUsingEncoding(NSUTF8StringEncoding)!)', source.postData.params[i].name, source.postData.params[i].value)) | ||
code.push('postData.appendData("&%s=%s".dataUsingEncoding(NSUTF8StringEncoding)!)', source.postData.params[i].name, source.postData.params[i].value) | ||
} | ||
@@ -73,3 +73,3 @@ break | ||
.blank() | ||
.push(util.format('let boundary = "%s"', source.postData.boundary)) | ||
.push('let boundary = "%s"', source.postData.boundary) | ||
.blank() | ||
@@ -99,3 +99,3 @@ .push('var body = ""') | ||
code.blank() | ||
.push(util.format('let postData = NSData(data: "%s".dataUsingEncoding(NSUTF8StringEncoding)!)', source.postData.text)) | ||
.push('let postData = NSData(data: "%s".dataUsingEncoding(NSUTF8StringEncoding)!)', source.postData.text) | ||
} | ||
@@ -106,6 +106,6 @@ } | ||
// NSURLRequestUseProtocolCachePolicy is the default policy, let's just always set it to avoid confusion. | ||
.push(util.format('var request = NSMutableURLRequest(URL: NSURL(string: "%s")!,', source.fullUrl)) | ||
.push('var request = NSMutableURLRequest(URL: NSURL(string: "%s")!,', source.fullUrl) | ||
.push(' cachePolicy: .UseProtocolCachePolicy,') | ||
.push(util.format(' timeoutInterval: %s)', parseInt(opts.timeout, 10).toFixed(1))) | ||
.push(util.format('request.HTTPMethod = "%s"', source.method)) | ||
.push(' timeoutInterval: %s)', parseInt(opts.timeout, 10).toFixed(1)) | ||
.push('request.HTTPMethod = "%s"', source.method) | ||
@@ -112,0 +112,0 @@ if (req.hasHeaders) { |
86238
57
2219