diff --git a/package.json b/package.json index dd9b9dc6c..effa8766d 100644 --- a/package.json +++ b/package.json @@ -19,9 +19,11 @@ "java", "javascript", "jquery", + "lwp", "objc", "objective-c", "ocaml", + "perl5", "php", "python", "request", diff --git a/src/targets/index.js b/src/targets/index.js index d4369529d..250d19c9a 100644 --- a/src/targets/index.js +++ b/src/targets/index.js @@ -10,6 +10,7 @@ module.exports = { node: require('./node'), objc: require('./objc'), ocaml: require('./ocaml'), + perl5: require('./perl5'), php: require('./php'), python: require('./python'), ruby: require('./ruby'), diff --git a/src/targets/perl5/index.js b/src/targets/perl5/index.js new file mode 100644 index 000000000..fc6ea9ae7 --- /dev/null +++ b/src/targets/perl5/index.js @@ -0,0 +1,12 @@ +'use strict' + +module.exports = { + info: { + key: 'perl5', + title: 'Perl 5', + extname: '.pl', + default: 'perl5' + }, + + lwp: require('./lwp') +} diff --git a/src/targets/perl5/lwp.js b/src/targets/perl5/lwp.js new file mode 100644 index 000000000..53f405390 --- /dev/null +++ b/src/targets/perl5/lwp.js @@ -0,0 +1,79 @@ +/** + * @description + * HTTP code snippet generator for Perl using LWP + * + * @author + * @ashleyhindmarsh + * + * for any questions or issues regarding the generated code snippet, please open an issue mentioning the author. + */ + +'use strict' + +var util = require('util') +var CodeBuilder = require('../../helpers/code-builder') + +module.exports = function (source, options) { + // Start snippet + var code = new CodeBuilder(' ') + + // Dependencies + code.push('use strict; use warnings;') + .push('use LWP::UserAgent;') + .push('use HTTP::Request;') + .push('use URI;') + .blank() + + code.push('my $ua = LWP::UserAgent->new;'); + + // Set URL + code.push('my $url = URI->new("%s");', source.url) + .blank() + + // Construct query string + if (source.queryString.length) { + code.push( '$url->query(%s);', JSON.stringify(source.queryObj) ) + .blank + } + + // Construct payload + var payload = JSON.stringify(source.postData.text) + + if (payload) { + code.push('my $payload = %s;', payload) + } + + // Construct request + var method = source.method + var request = util.format('my $request = HTTP::Request->new( %s => $url );', method); + code.push(request) + .blank() + + if (payload) { + code.push( '$request->content( $payload );' ) + } + + // Construct headers + var header + var headers = source.allHeaders + for (header in headers) { + code.push( '$request->header( \'%s\' => \'%s\' );', header, headers[header] ) + .blank; + } + + // execute request + code.push('my $response = $ua->request($request);') + + // print response + code.push('print($response->decoded_content);') + + return code.join() +} + +module.exports.info = { + key: 'lwp', + title: 'LWP', + link: 'https://metacpan.org/pod/LWP', + description: 'LWP library' +} + diff --git a/test/fixtures/available-targets.json b/test/fixtures/available-targets.json index effe17ef6..c2053fb5e 100644 --- a/test/fixtures/available-targets.json +++ b/test/fixtures/available-targets.json @@ -248,5 +248,19 @@ "description": "Simple REST and HTTP API Client for C" } ] + }, + { + "key": "perl5", + "title": "Perl 5", + "extname": ".pl", + "default": "lwp", + "clients": [ + { + "key": "lwp", + "title": "LWP", + "link": "https://metacpan.org/pod/LWP", + "description": "LWP WWW library" + } + ] } ] diff --git a/test/targets/perl5/lwp.js b/test/targets/perl5/lwp.js new file mode 100644 index 000000000..ce4678eb9 --- /dev/null +++ b/test/targets/perl5/lwp.js @@ -0,0 +1,6 @@ +'use strict' + +require('should') + +module.exports = function (snippet, fixtures) { +}