Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia
TypeScript apiapi-clientapi-restapi-testingdeveloper-toolsgraphqlhttphttp-clientpwarestrest-apispatestingtesting-toolstoolsvuevuejswebsocket
feat:add code generator for Ruby Net::HTTP (#1284)
Bende970484240c
diff --git a/helpers/codegen/__tests__/__snapshots__/codegen.spec.js.snap b/helpers/codegen/__tests__/__snapshots__/codegen.spec.js.snapindex 93ffe69a1..dbfc1a8cf 100644--- a/helpers/codegen/__tests__/__snapshots__/codegen.spec.js.snap+++ b/helpers/codegen/__tests__/__snapshots__/codegen.spec.js.snap@@ -913,6 +913,95 @@ response = requests.request(print(response)"`;+exports[`generate request for Ruby Net::HTTP generate GET request 1`] = `+"require 'net/http'++uri = URI.parse('https://httpbin.org/path/to?a=b')++request = Net::HTTP::Get.new(uri)+request['h1'] = 'h1v'+request['h2'] = 'h2v'+request.basic_auth('mockUser', 'mockPassword')+http = Net::HTTP.new(uri.host, uri.port)+http.use_ssl = uri.is_a?(URI::HTTPS)+response = http.request(request)++unless response.is_a?(Net::HTTPSuccess) then+ raise \\"An error occurred: #{response.code} #{response.message}\\"+else+ puts response.body+end"+`;++exports[`generate request for Ruby Net::HTTP generate POST request for JSON 1`] = `+"require 'net/http'++uri = URI.parse('https://httpbin.org/path/to?a=b')++request = Net::HTTP::Post.new(uri)+request['Content-Type'] = 'application/json'+request['h1'] = 'h1v'+request['h2'] = 'h2v'+request['Authorization'] = 'Bearer abcdefghijklmn'+request.body = '{\\"foo\\": \\"bar\\", \\"baz\\": \\"qux\\"}'++http = Net::HTTP.new(uri.host, uri.port)+http.use_ssl = uri.is_a?(URI::HTTPS)+response = http.request(request)++unless response.is_a?(Net::HTTPSuccess) then+ raise \\"An error occurred: #{response.code} #{response.message}\\"+else+ puts response.body+end"+`;++exports[`generate request for Ruby Net::HTTP generate POST request for XML 1`] = `+"require 'net/http'++uri = URI.parse('https://httpbin.org/path/to?a=b')++request = Net::HTTP::Post.new(uri)+request['Content-Type'] = 'application/xml'+request['h1'] = 'h1v'+request['h2'] = 'h2v'+request['Authorization'] = 'Bearer abcdefghijklmn'+request.body = '<?xml version=\\\\'1.0\\\\' encoding=\\\\'utf-8\\\\'?>+<xml>+ <element foo=\\"bar\\"></element>+</xml>'++http = Net::HTTP.new(uri.host, uri.port)+http.use_ssl = uri.is_a?(URI::HTTPS)+response = http.request(request)++unless response.is_a?(Net::HTTPSuccess) then+ raise \\"An error occurred: #{response.code} #{response.message}\\"+else+ puts response.body+end"+`;++exports[`generate request for Ruby Net::HTTP generate PUT request for www-form-urlencoded 1`] = `+"require 'net/http'++uri = URI.parse('https://httpbin.org/path/to?a=b')++request = Net::HTTP::Put.new(uri)+request['Content-Type'] = 'application/x-www-form-urlencoded'+request.body = 'foo=bar&baz=qux'++http = Net::HTTP.new(uri.host, uri.port)+http.use_ssl = uri.is_a?(URI::HTTPS)+response = http.request(request)++unless response.is_a?(Net::HTTPSuccess) then+ raise \\"An error occurred: #{response.code} #{response.message}\\"+else+ puts response.body+end"+`;+exports[`generate request for cURL generate GET request 1`] = `"curl -X GET \\\\'https://httpbin.org/path/to?a=b' \\\\diff --git a/helpers/codegen/codegen.js b/helpers/codegen/codegen.jsindex 690c4ee8e..a365db8c3 100644--- a/helpers/codegen/codegen.js+++ b/helpers/codegen/codegen.js@@ -12,6 +12,7 @@ import { PythonRequestsCodegen } from "./generators/python-requests"import { PythonHttpClientCodegen } from "./generators/python-http-client"import { WgetCodegen } from "./generators/wget"import { CsRestSharpCodegen } from "./generators/cs-restsharp"+import { RubyNetHttpCodeGen } from "./generators/ruby-net-http"/* Register code generators here.* A code generator is defined as an object with the following structure.@@ -35,6 +36,7 @@ export const codegens = [PowerShellRestMethod,PythonRequestsCodegen,PythonHttpClientCodegen,+ RubyNetHttpCodeGen,WgetCodegen,]diff --git a/helpers/codegen/generators/ruby-net-http.js b/helpers/codegen/generators/ruby-net-http.jsnew file mode 100644index 000000000..c4c0d2836--- /dev/null+++ b/helpers/codegen/generators/ruby-net-http.js@@ -0,0 +1,80 @@+export const RubyNetHttpCodeGen = {+ id: "ruby-net-http",+ name: "Ruby Net::HTTP",+ generator: ({+ url,+ pathName,+ queryString,+ auth,+ httpUser,+ httpPassword,+ bearerToken,+ method,+ rawInput,+ rawParams,+ rawRequestBody,+ contentType,+ headers,+ }) => {+ const requestString = []++ requestString.push(`require 'net/http'\n`)++ // initial request setup+ let requestBody = rawInput ? rawParams : rawRequestBody+ requestBody = requestBody.replace(/'/g, "\\'") // escape single-quotes for single-quoted string compatibility++ const verbs = [+ { verb: "GET", rbMethod: "Get" },+ { verb: "POST", rbMethod: "Post" },+ { verb: "PUT", rbMethod: "Put" },+ { verb: "PATCH", rbMethod: "Patch" },+ { verb: "DELETE", rbMethod: "Delete" },+ ]++ // create URI and request+ const verb = verbs.find((v) => v.verb === method)+ requestString.push(`uri = URI.parse('${url}${pathName}${queryString}')\n`)+ requestString.push(`request = Net::HTTP::${verb.rbMethod}.new(uri)`)++ // content type+ if (contentType) {+ requestString.push(`request['Content-Type'] = '${contentType}'`)+ }++ // custom headers+ if (headers) {+ headers.forEach(({ key, value }) => {+ if (key) {+ requestString.push(`request['${key}'] = '${value}'`)+ }+ })+ }++ // authentication+ if (auth === "Basic Auth") {+ requestString.push(`request.basic_auth('${httpUser}', '${httpPassword}')`)+ } else if (auth === "Bearer Token" || auth === "OAuth 2.0") {+ requestString.push(`request['Authorization'] = 'Bearer ${bearerToken}'`)+ }++ // set body+ if (["POST", "PUT", "PATCH", "DELETE"].includes(method)) {+ requestString.push(`request.body = '${requestBody}'\n`)+ }++ // process+ requestString.push(`http = Net::HTTP.new(uri.host, uri.port)`)+ requestString.push(`http.use_ssl = uri.is_a?(URI::HTTPS)`)+ requestString.push(`response = http.request(request)\n`)++ // analyse result+ requestString.push(`unless response.is_a?(Net::HTTPSuccess) then`)+ requestString.push(` raise "An error occurred: #{response.code} #{response.message}"`)+ requestString.push(`else`)+ requestString.push(` puts response.body`)+ requestString.push(`end`)++ return requestString.join("\n")+ },+}