Skip to content

Commit 109b9c4

Browse files
gabrielakoreedadarrenjennings
authored andcommitted
feat(target) Add R statistical computing lang (Kong#131)
1 parent 85abe59 commit 109b9c4

21 files changed

+339
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"har",
1717
"http",
1818
"httpie",
19+
"httr",
1920
"java",
2021
"javascript",
2122
"jquery",

src/targets/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module.exports = {
1414
php: require('./php'),
1515
powershell: require('./powershell'),
1616
python: require('./python'),
17+
r: require('./r'),
1718
ruby: require('./ruby'),
1819
shell: require('./shell'),
1920
swift: require('./swift')

src/targets/r/httr.js

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/**
2+
* @description
3+
* HTTP code snippet generator for R using httr
4+
*
5+
* @author
6+
* @gabrielakoreeda
7+
*
8+
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
9+
*/
10+
11+
'use strict'
12+
13+
var util = require('util')
14+
var CodeBuilder = require('../../helpers/code-builder')
15+
16+
module.exports = function (source, options) {
17+
// Start snippet
18+
var code = new CodeBuilder()
19+
20+
// Import httr
21+
code.push('library(httr)')
22+
.blank()
23+
24+
// Set URL
25+
code.push('url <- "%s"', source.url)
26+
.blank()
27+
28+
// Construct query string
29+
var query
30+
var qs = source.queryObj
31+
var queryCount = Object.keys(qs).length
32+
delete source.queryObj['key']
33+
34+
if (source.queryString.length === 1) {
35+
code.push('queryString <- list(%s = "%s")', Object.keys(qs), Object.values(qs).toString())
36+
.blank()
37+
} else if (source.queryString.length > 1) {
38+
var count = 1
39+
40+
code.push('queryString <- list(')
41+
42+
for (query in qs) {
43+
if (count++ !== queryCount - 1) {
44+
code.push(' %s = "%s",', query, qs[query].toString())
45+
} else {
46+
code.push(' %s = "%s"', query, qs[query].toString())
47+
}
48+
}
49+
50+
code.push(')')
51+
.blank()
52+
}
53+
54+
// Construct payload
55+
var payload = JSON.stringify(source.postData.text)
56+
57+
if (payload) {
58+
code.push('payload <- %s', payload)
59+
.blank()
60+
}
61+
62+
// Define encode
63+
if (source.postData.text || source.postData.jsonObj || source.postData.params) {
64+
switch (source.postData.mimeType) {
65+
case 'application/x-www-form-urlencoded':
66+
code.push('encode <- "form"')
67+
.blank()
68+
break
69+
70+
case 'application/json':
71+
code.push('encode <- "json"')
72+
.blank()
73+
break
74+
75+
case 'multipart/form-data':
76+
code.push('encode <- "multipart"')
77+
.blank()
78+
break
79+
80+
default:
81+
code.push('encode <- "raw"')
82+
.blank()
83+
break
84+
}
85+
}
86+
87+
// Construct headers
88+
var head
89+
var headers = source.allHeaders
90+
var headerCount = Object.keys(headers).length
91+
var header = ''
92+
var cookies
93+
var accept
94+
95+
for (head in headers) {
96+
if (head === 'accept') {
97+
accept = ', accept("' + headers[head] + '")'
98+
headerCount = headerCount - 1
99+
} else if (head === 'cookie') {
100+
cookies = ', set_cookies(`' + headers[head].replace(/;/g, '", `').replace(/` /g, '`').replace(/=/g, '` = "') + '")'
101+
headerCount = headerCount - 1
102+
} else if (head !== 'content-type') {
103+
header = header + head.replace('-', '_') + " = '" + headers[head]
104+
if (headerCount > 1) { header = header + "', " }
105+
}
106+
}
107+
108+
// Construct request
109+
var method = source.method
110+
var request = util.format('response <- VERB("%s", url', method)
111+
112+
if (payload) {
113+
request += ', body = payload'
114+
}
115+
116+
if (header !== '') {
117+
request += ', add_headers(' + header + "')"
118+
}
119+
120+
if (source.queryString.length) {
121+
request += ', query = queryString'
122+
}
123+
124+
request += ', content_type("' + source.postData.mimeType + '")'
125+
126+
if (typeof accept !== 'undefined') {
127+
request += accept
128+
}
129+
130+
if (typeof cookies !== 'undefined') {
131+
request += cookies
132+
}
133+
134+
if (source.postData.text || source.postData.jsonObj || source.postData.params) {
135+
request += ', encode = encode'
136+
}
137+
138+
request += ')'
139+
140+
code.push(request)
141+
.blank()
142+
143+
// Print response
144+
.push('content(response, "text")')
145+
146+
return code.join()
147+
}
148+
149+
module.exports.info = {
150+
key: 'httr',
151+
title: 'httr',
152+
link: 'https://cran.r-project.org/web/packages/httr/vignettes/quickstart.html',
153+
description: 'httr: Tools for Working with URLs and HTTP'
154+
}

src/targets/r/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict'
2+
3+
module.exports = {
4+
info: {
5+
key: 'r',
6+
title: 'R',
7+
extname: '.r',
8+
default: 'httr'
9+
},
10+
11+
httr: require('./httr')
12+
}

test/fixtures/available-targets.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,20 @@
255255
}
256256
]
257257
},
258+
{
259+
"key": "r",
260+
"title": "R",
261+
"extname": ".r",
262+
"default": "httr",
263+
"clients": [
264+
{
265+
"key": "httr",
266+
"title": "httr",
267+
"link": "https://cran.r-project.org/web/packages/httr/vignettes/quickstart.html",
268+
"description": "httr: Tools for Working with URLs and HTTP"
269+
}
270+
]
271+
},
258272
{
259273
"default": "webrequest",
260274
"extname": ".ps1",
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
library(httr)
2+
3+
url <- "http://mockbin.com/har"
4+
5+
payload <- "foo=bar&hello=world"
6+
7+
encode <- "form"
8+
9+
response <- VERB("POST", url, body = payload, content_type("application/x-www-form-urlencoded"), encode = encode)
10+
11+
content(response, "text")
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
library(httr)
2+
3+
url <- "http://mockbin.com/har"
4+
5+
payload <- "{\"number\":1,\"string\":\"f\\\"oo\",\"arr\":[1,2,3],\"nested\":{\"a\":\"b\"},\"arr_mix\":[1,\"a\",{\"arr_mix_nested\":{}}],\"boolean\":false}"
6+
7+
encode <- "json"
8+
9+
response <- VERB("POST", url, body = payload, content_type("application/json"), encode = encode)
10+
11+
content(response, "text")
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
library(httr)
2+
3+
url <- "http://mockbin.com/har"
4+
5+
response <- VERB("POST", url, content_type("application/octet-stream"), set_cookies(`foo` = "bar", `bar` = "baz"))
6+
7+
content(response, "text")
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
library(httr)
2+
3+
url <- "http://mockbin.com/har"
4+
5+
response <- VERB("PROPFIND", url, content_type("application/octet-stream"))
6+
7+
content(response, "text")

test/fixtures/output/r/httr/full.r

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
library(httr)
2+
3+
url <- "http://mockbin.com/har"
4+
5+
queryString <- list(
6+
foo = "bar,baz",
7+
baz = "abc"
8+
)
9+
10+
payload <- "foo=bar"
11+
12+
encode <- "form"
13+
14+
response <- VERB("POST", url, body = payload, query = queryString, content_type("application/x-www-form-urlencoded"), accept("application/json"), set_cookies(`foo` = "bar", `bar` = "baz"), encode = encode)
15+
16+
content(response, "text")

0 commit comments

Comments
 (0)