Skip to content

Commit 7eb4866

Browse files
committed
Add Ruby Faraday target
Based on upstream
1 parent 3d3c0d0 commit 7eb4866

23 files changed

+396
-1
lines changed

src/targets/ruby/faraday.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
const CodeBuilder = require('../../helpers/code-builder')
2+
3+
module.exports = ({ uriObj, queryObj, method: rawMethod, postData, allHeaders }) => {
4+
const code = new CodeBuilder()
5+
6+
// To support custom methods we check for the supported methods
7+
// and if doesn't exist then we build a custom class for it
8+
const method = rawMethod.toUpperCase()
9+
const methods = [
10+
'GET',
11+
'POST',
12+
'HEAD',
13+
'DELETE',
14+
'PATCH',
15+
'PUT',
16+
'OPTIONS',
17+
'COPY',
18+
'LOCK',
19+
'UNLOCK',
20+
'MOVE',
21+
'TRACE'
22+
]
23+
24+
if (!methods.includes(method)) {
25+
code.push(`# Faraday cannot currently run ${method} requests. Please use another client.`)
26+
return code.join()
27+
}
28+
29+
code.push("require 'faraday'")
30+
code.blank()
31+
32+
// Write body to beginning of script
33+
if (postData.mimeType === 'application/x-www-form-urlencoded') {
34+
if (postData.params) {
35+
code.push('data = {')
36+
postData.params.forEach(param => {
37+
code.push(` :${param.name} => ${JSON.stringify(param.value)},`)
38+
})
39+
code.push('}')
40+
code.blank()
41+
}
42+
}
43+
44+
code.push('conn = Faraday.new(')
45+
code.push(` url: '${uriObj.protocol}//${uriObj.host}',`)
46+
if (allHeaders['content-type'] || allHeaders['Content-Type']) {
47+
code.push(` headers: {'Content-Type' => '${allHeaders['content-type'] || allHeaders['Content-Type']}'}`)
48+
}
49+
code.push(')')
50+
51+
code.blank()
52+
code.push(`response = conn.${method.toLowerCase()}('${uriObj.pathname}') do |req|`)
53+
54+
const headers = Object.keys(allHeaders)
55+
if (headers.length) {
56+
headers.forEach(key => {
57+
if (key.toLowerCase() !== 'content-type') {
58+
code.push(" req.headers['%qs'] = '%qs'", key, allHeaders[key])
59+
}
60+
})
61+
}
62+
63+
Object.keys(queryObj).forEach(name => {
64+
const value = queryObj[name]
65+
if (Array.isArray(value)) {
66+
code.push(` req.params['%qs'] = ${JSON.stringify(value)}`, name)
67+
} else {
68+
code.push(" req.params['%qs'] = '%qs'", name, value)
69+
}
70+
})
71+
72+
switch (postData.mimeType) {
73+
case 'application/x-www-form-urlencoded':
74+
if (postData.params) {
75+
code.push(' req.body = URI.encode_www_form(data)')
76+
}
77+
break
78+
79+
case 'application/json':
80+
if (postData.jsonObj) {
81+
code.push(` req.body = ${JSON.stringify(postData.text)}`)
82+
}
83+
break
84+
85+
default:
86+
if (postData.text) {
87+
code.push(` req.body = ${JSON.stringify(postData.text)}`)
88+
}
89+
}
90+
91+
code.push('end')
92+
code.blank()
93+
code.push('puts response.status')
94+
code.push('puts response.body')
95+
96+
return code.join()
97+
}
98+
99+
module.exports.info = {
100+
key: 'faraday',
101+
title: 'faraday',
102+
link: 'https://github.com/lostisland/faraday',
103+
description: 'Faraday HTTP client'
104+
}

src/targets/ruby/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ module.exports = {
88
default: 'native'
99
},
1010

11-
native: require('./native')
11+
native: require('./native'),
12+
faraday: require('./faraday')
1213
}

test/fixtures/available-targets.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,12 @@
330330
"title": "net::http",
331331
"link": "http://ruby-doc.org/stdlib-2.2.1/libdoc/net/http/rdoc/Net/HTTP.html",
332332
"description": "Ruby HTTP client"
333+
},
334+
{
335+
"key": "faraday",
336+
"title": "faraday",
337+
"link": "https://github.com/lostisland/faraday",
338+
"description": "Faraday HTTP client"
333339
}
334340
]
335341
},
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
require 'faraday'
2+
3+
data = {
4+
:foo => "bar",
5+
:hello => "world",
6+
}
7+
8+
conn = Faraday.new(
9+
url: 'http://mockbin.com',
10+
headers: {'Content-Type' => 'application/x-www-form-urlencoded'}
11+
)
12+
13+
response = conn.post('/har') do |req|
14+
req.body = URI.encode_www_form(data)
15+
end
16+
17+
puts response.status
18+
puts response.body
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require 'faraday'
2+
3+
conn = Faraday.new(
4+
url: 'http://mockbin.com',
5+
headers: {'Content-Type' => 'application/json'}
6+
)
7+
8+
response = conn.post('/har') do |req|
9+
req.body = "{\"number\":1,\"string\":\"f\\\"oo\",\"arr\":[1,2,3],\"nested\":{\"a\":\"b\"},\"arr_mix\":[1,\"a\",{\"arr_mix_nested\":{}}],\"boolean\":false}"
10+
end
11+
12+
puts response.status
13+
puts response.body
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
require 'faraday'
2+
3+
conn = Faraday.new(
4+
url: 'http://mockbin.com',
5+
)
6+
7+
response = conn.get('/har') do |req|
8+
req.headers['accept-encoding'] = 'deflate, gzip, br'
9+
end
10+
11+
puts response.status
12+
puts response.body
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
require 'faraday'
2+
3+
conn = Faraday.new(
4+
url: 'http://mockbin.com',
5+
)
6+
7+
response = conn.post('/har') do |req|
8+
req.headers['cookie'] = 'foo=bar; bar=baz'
9+
end
10+
11+
puts response.status
12+
puts response.body
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Faraday cannot currently run PROPFIND requests. Please use another client.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require 'faraday'
2+
3+
data = {
4+
:foo => "bar",
5+
}
6+
7+
conn = Faraday.new(
8+
url: 'http://mockbin.com',
9+
headers: {'Content-Type' => 'application/x-www-form-urlencoded'}
10+
)
11+
12+
response = conn.post('/har') do |req|
13+
req.headers['cookie'] = 'foo=bar; bar=baz'
14+
req.headers['accept'] = 'application/json'
15+
req.params['foo'] = ["bar","baz"]
16+
req.params['baz'] = 'abc'
17+
req.params['key'] = 'value'
18+
req.body = URI.encode_www_form(data)
19+
end
20+
21+
puts response.status
22+
puts response.body
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require 'faraday'
2+
3+
conn = Faraday.new(
4+
url: 'http://mockbin.com',
5+
)
6+
7+
response = conn.get('/har') do |req|
8+
req.headers['accept'] = 'application/json'
9+
req.headers['x-foo'] = 'Bar'
10+
req.headers['quoted-value'] = '"quoted" \'string\''
11+
end
12+
13+
puts response.status
14+
puts response.body

0 commit comments

Comments
 (0)