Skip to content

Commit

Permalink
Add generator for Ruby's Faraday client (#362)
Browse files Browse the repository at this point in the history
Co-authored-by: Lukas_Skywalker <[email protected]>
  • Loading branch information
filfreire and LukasSkywalker authored Jul 12, 2024
1 parent 147eb98 commit 8739d73
Show file tree
Hide file tree
Showing 20 changed files with 335 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/helpers/__snapshots__/utils.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,12 @@ Array [
"link": "http://ruby-doc.org/stdlib-2.2.1/libdoc/net/http/rdoc/Net/HTTP.html",
"title": "net::http",
},
Object {
"description": "Faraday HTTP client",
"key": "faraday",
"link": "https://github.com/lostisland/faraday",
"title": "faraday",
},
],
"default": "native",
"extname": ".rb",
Expand Down
107 changes: 107 additions & 0 deletions src/targets/ruby/faraday/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { CodeBuilder } from '../../../helpers/code-builder';
import { escapeForSingleQuotes } from '../../../helpers/escape';
import { Client } from '../../targets';

export const faraday: Client = {
info: {
key: 'faraday',
title: 'faraday',
link: 'https://github.com/lostisland/faraday',
description: 'Faraday HTTP client',
},
convert: ({ uriObj, queryObj, method: rawMethod, fullUrl, postData, allHeaders }, options = {}) => {
const { push, blank, join } = new CodeBuilder();

Check warning on line 13 in src/targets/ruby/faraday/client.ts

View workflow job for this annotation

GitHub Actions / build (16)

'fullUrl' is defined but never used

Check warning on line 13 in src/targets/ruby/faraday/client.ts

View workflow job for this annotation

GitHub Actions / build (18)

'fullUrl' is defined but never used

Check warning on line 13 in src/targets/ruby/faraday/client.ts

View workflow job for this annotation

GitHub Actions / build (20)

'fullUrl' is defined but never used

Check warning on line 14 in src/targets/ruby/faraday/client.ts

View workflow job for this annotation

GitHub Actions / build (16)

'options' is assigned a value but never used

Check warning on line 14 in src/targets/ruby/faraday/client.ts

View workflow job for this annotation

GitHub Actions / build (18)

'options' is assigned a value but never used

Check warning on line 14 in src/targets/ruby/faraday/client.ts

View workflow job for this annotation

GitHub Actions / build (20)

'options' is assigned a value but never used
// To support custom methods we check for the supported methods
// and if doesn't exist then we build a custom class for it
const method = rawMethod.toUpperCase();
const methods = [
'GET',
'POST',
'HEAD',
'DELETE',
'PATCH',
'PUT',
'OPTIONS',
'COPY',
'LOCK',
'UNLOCK',
'MOVE',
'TRACE',
];

if(!methods.includes(method)) {
push(`# Faraday cannot currently run ${method} requests. Please use another client.`)
return join();
}

push("require 'faraday'");
blank();

// Write body to beginning of script
if(postData.mimeType === 'application/x-www-form-urlencoded') {
if (postData.params) {
push(`data = {`);
postData.params.forEach(param => {
push(` :${param.name} => ${JSON.stringify(param.value)},`);
});
push(`}`);
blank();
}
}

push(`conn = Faraday.new(`);
push(` url: '${uriObj.protocol}//${uriObj.host}',`);
if(allHeaders['content-type'] || allHeaders['Content-Type']) {
push(` headers: {'Content-Type' => '${allHeaders['content-type'] || allHeaders['Content-Type']}'}`);
}
push(`)`);

blank();
push(`response = conn.${method.toLowerCase()}('${uriObj.pathname}') do |req|`);

const headers = Object.keys(allHeaders);
if (headers.length) {
headers.forEach(key => {
if(key.toLowerCase() !== 'content-type') {
push(` req.headers['${key}'] = '${escapeForSingleQuotes(allHeaders[key])}'`);
}
});
}

Object.keys(queryObj).forEach(name => {
const value = queryObj[name];
if (Array.isArray(value)) {
push(` req.params['${name}'] = ${JSON.stringify(value)}`)
} else {
push(` req.params['${name}'] = '${value}'`)
}
});

switch (postData.mimeType) {
case 'application/x-www-form-urlencoded':
if (postData.params) {
push(` req.body = URI.encode_www_form(data)`);
}
break;

case 'application/json':
if (postData.jsonObj) {
push(` req.body = ${JSON.stringify(postData.text)}`);
}
break;

default:
if (postData.text) {
push(` req.body = ${JSON.stringify(postData.text)}`);
}
}

push('end');
blank()
push('puts response.status');
push('puts response.body');

return join();
},
};
18 changes: 18 additions & 0 deletions src/targets/ruby/faraday/fixtures/application-form-encoded.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'faraday'

data = {
:foo => "bar",
:hello => "world",
}

conn = Faraday.new(
url: 'http://mockbin.com',
headers: {'Content-Type' => 'application/x-www-form-urlencoded'}
)

response = conn.post('/har') do |req|
req.body = URI.encode_www_form(data)
end

puts response.status
puts response.body
13 changes: 13 additions & 0 deletions src/targets/ruby/faraday/fixtures/application-json.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'faraday'

conn = Faraday.new(
url: 'http://mockbin.com',
headers: {'Content-Type' => 'application/json'}
)

response = conn.post('/har') do |req|
req.body = "{\"number\":1,\"string\":\"f\\\"oo\",\"arr\":[1,2,3],\"nested\":{\"a\":\"b\"},\"arr_mix\":[1,\"a\",{\"arr_mix_nested\":{}}],\"boolean\":false}"
end

puts response.status
puts response.body
12 changes: 12 additions & 0 deletions src/targets/ruby/faraday/fixtures/cookies.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'faraday'

conn = Faraday.new(
url: 'http://mockbin.com',
)

response = conn.post('/har') do |req|
req.headers['cookie'] = 'foo=bar; bar=baz'
end

puts response.status
puts response.body
1 change: 1 addition & 0 deletions src/targets/ruby/faraday/fixtures/custom-method.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Faraday cannot currently run PROPFIND requests. Please use another client.
22 changes: 22 additions & 0 deletions src/targets/ruby/faraday/fixtures/full.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'faraday'

data = {
:foo => "bar",
}

conn = Faraday.new(
url: 'http://mockbin.com',
headers: {'Content-Type' => 'application/x-www-form-urlencoded'}
)

response = conn.post('/har') do |req|
req.headers['cookie'] = 'foo=bar; bar=baz'
req.headers['accept'] = 'application/json'
req.params['foo'] = ["bar","baz"]
req.params['baz'] = 'abc'
req.params['key'] = 'value'
req.body = URI.encode_www_form(data)
end

puts response.status
puts response.body
14 changes: 14 additions & 0 deletions src/targets/ruby/faraday/fixtures/headers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'faraday'

conn = Faraday.new(
url: 'http://mockbin.com',
)

response = conn.get('/har') do |req|
req.headers['accept'] = 'application/json'
req.headers['x-foo'] = 'Bar'
req.headers['quoted-value'] = '"quoted" \'string\''
end

puts response.status
puts response.body
11 changes: 11 additions & 0 deletions src/targets/ruby/faraday/fixtures/https.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'faraday'

conn = Faraday.new(
url: 'https://mockbin.com',
)

response = conn.get('/har') do |req|
end

puts response.status
puts response.body
13 changes: 13 additions & 0 deletions src/targets/ruby/faraday/fixtures/jsonObj-multiline.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'faraday'

conn = Faraday.new(
url: 'http://mockbin.com',
headers: {'Content-Type' => 'application/json'}
)

response = conn.post('/har') do |req|
req.body = "{\n \"foo\": \"bar\"\n}"
end

puts response.status
puts response.body
13 changes: 13 additions & 0 deletions src/targets/ruby/faraday/fixtures/jsonObj-null-value.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'faraday'

conn = Faraday.new(
url: 'http://mockbin.com',
headers: {'Content-Type' => 'application/json'}
)

response = conn.post('/har') do |req|
req.body = "{\"foo\":null}"
end

puts response.status
puts response.body
13 changes: 13 additions & 0 deletions src/targets/ruby/faraday/fixtures/multipart-data.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'faraday'

conn = Faraday.new(
url: 'http://mockbin.com',
headers: {'Content-Type' => 'multipart/form-data; boundary=---011000010111000001101001'}
)

response = conn.post('/har') do |req|
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"
end

puts response.status
puts response.body
13 changes: 13 additions & 0 deletions src/targets/ruby/faraday/fixtures/multipart-file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'faraday'

conn = Faraday.new(
url: 'http://mockbin.com',
headers: {'Content-Type' => 'multipart/form-data; boundary=---011000010111000001101001'}
)

response = conn.post('/har') do |req|
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"
end

puts response.status
puts response.body
12 changes: 12 additions & 0 deletions src/targets/ruby/faraday/fixtures/multipart-form-data-no-params.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'faraday'

conn = Faraday.new(
url: 'http://mockbin.com',
headers: {'Content-Type' => 'multipart/form-data'}
)

response = conn.post('/har') do |req|
end

puts response.status
puts response.body
13 changes: 13 additions & 0 deletions src/targets/ruby/faraday/fixtures/multipart-form-data.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'faraday'

conn = Faraday.new(
url: 'http://mockbin.com',
headers: {'Content-Type' => 'multipart/form-data; boundary=---011000010111000001101001'}
)

response = conn.post('/har') do |req|
req.body = "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\nbar\r\n-----011000010111000001101001--\r\n"
end

puts response.status
puts response.body
14 changes: 14 additions & 0 deletions src/targets/ruby/faraday/fixtures/nested.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'faraday'

conn = Faraday.new(
url: 'http://mockbin.com',
)

response = conn.get('/har') do |req|
req.params['foo[bar]'] = 'baz,zap'
req.params['fiz'] = 'buz'
req.params['key'] = 'value'
end

puts response.status
puts response.body
14 changes: 14 additions & 0 deletions src/targets/ruby/faraday/fixtures/query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'faraday'

conn = Faraday.new(
url: 'http://mockbin.com',
)

response = conn.get('/har') do |req|
req.params['foo'] = ["bar","baz"]
req.params['baz'] = 'abc'
req.params['key'] = 'value'
end

puts response.status
puts response.body
11 changes: 11 additions & 0 deletions src/targets/ruby/faraday/fixtures/short.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'faraday'

conn = Faraday.new(
url: 'http://mockbin.com',
)

response = conn.get('/har') do |req|
end

puts response.status
puts response.body
13 changes: 13 additions & 0 deletions src/targets/ruby/faraday/fixtures/text-plain.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'faraday'

conn = Faraday.new(
url: 'http://mockbin.com',
headers: {'Content-Type' => 'text/plain'}
)

response = conn.post('/har') do |req|
req.body = "Hello World"
end

puts response.status
puts response.body
2 changes: 2 additions & 0 deletions src/targets/ruby/target.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Target } from '../targets';
import { native } from './native/client';
import { faraday } from './faraday/client';

export const ruby: Target = {
info: {
Expand All @@ -10,5 +11,6 @@ export const ruby: Target = {
},
clientsById: {
native,
faraday
},
};

0 comments on commit 8739d73

Please sign in to comment.