Skip to content

Commit 8739d73

Browse files
Add generator for Ruby's Faraday client (#362)
Co-authored-by: Lukas_Skywalker <[email protected]>
1 parent 147eb98 commit 8739d73

20 files changed

+335
-0
lines changed

src/helpers/__snapshots__/utils.test.ts.snap

+6
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,12 @@ Array [
330330
"link": "http://ruby-doc.org/stdlib-2.2.1/libdoc/net/http/rdoc/Net/HTTP.html",
331331
"title": "net::http",
332332
},
333+
Object {
334+
"description": "Faraday HTTP client",
335+
"key": "faraday",
336+
"link": "https://github.com/lostisland/faraday",
337+
"title": "faraday",
338+
},
333339
],
334340
"default": "native",
335341
"extname": ".rb",

src/targets/ruby/faraday/client.ts

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

src/targets/ruby/target.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Target } from '../targets';
22
import { native } from './native/client';
3+
import { faraday } from './faraday/client';
34

45
export const ruby: Target = {
56
info: {
@@ -10,5 +11,6 @@ export const ruby: Target = {
1011
},
1112
clientsById: {
1213
native,
14+
faraday
1315
},
1416
};

0 commit comments

Comments
 (0)