Skip to content

Commit 5924fcd

Browse files
committed
Add new Crystal language target
Taken from upstream
1 parent 4b745a2 commit 5924fcd

24 files changed

+280
-0
lines changed

src/targets/crystal/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: 'crystal',
6+
title: 'Crystal',
7+
extname: '.cr',
8+
default: 'native'
9+
},
10+
11+
native: require('./native')
12+
}

src/targets/crystal/native.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* @description
3+
* HTTP code snippet generator for native Crystal
4+
*
5+
* @author
6+
* @yanecc
7+
*
8+
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
9+
*/
10+
11+
const CodeBuilder = require('../../helpers/code-builder')
12+
13+
const { escape } = require('../../helpers/format')
14+
15+
module.exports = function ({ method: rawMethod, fullUrl, postData, allHeaders }, options = {}) {
16+
const { insecureSkipVerify = false } = options
17+
18+
const code = new CodeBuilder()
19+
20+
code.push('require "http/client"')
21+
22+
code.blank()
23+
24+
code.push(`url = "${fullUrl}"`)
25+
26+
const headers = Object.keys(allHeaders)
27+
if (headers.length) {
28+
code.push('headers = HTTP::Headers{')
29+
headers.forEach(key => {
30+
code.push(` "${key}" => "${escape(allHeaders[key])}"`)
31+
})
32+
code.push('}')
33+
}
34+
35+
if (postData.text) {
36+
code.push(`reqBody = ${JSON.stringify(postData.text)}`)
37+
}
38+
39+
code.blank()
40+
41+
const method = rawMethod.toUpperCase()
42+
const methods = ['GET', 'POST', 'HEAD', 'DELETE', 'PATCH', 'PUT', 'OPTIONS']
43+
44+
const headersContext = headers.length ? ', headers: headers' : ''
45+
const bodyContext = postData.text ? ', body: reqBody' : ''
46+
const sslContext = insecureSkipVerify ? ', tls: OpenSSL::SSL::Context::Client.insecure' : ''
47+
48+
if (methods.includes(method)) {
49+
code.push(
50+
`response = HTTP::Client.${method.toLowerCase()} url${headersContext}${bodyContext}${sslContext}`
51+
)
52+
} else {
53+
code.push(
54+
`response = HTTP::Client.exec "${method}", url${headersContext}${bodyContext}${sslContext}`
55+
)
56+
}
57+
58+
code.push('puts response.body')
59+
60+
return code.join()
61+
}
62+
63+
module.exports.info = {
64+
key: 'native',
65+
title: 'http::client',
66+
link: 'https://crystal-lang.org/api/master/HTTP/Client.html',
67+
description: 'Crystal HTTP client'
68+
}

src/targets/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
module.exports = {
44
c: require('./c'),
55
clojure: require('./clojure'),
6+
crystal: require('./crystal'),
67
csharp: require('./csharp'),
78
go: require('./go'),
89
http: require('./http'),

test/fixtures/available-targets.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,5 +358,19 @@
358358
"description": "An HTTP Request Client Library"
359359
}
360360
]
361+
},
362+
{
363+
"key": "crystal",
364+
"title": "Crystal",
365+
"extname": ".cr",
366+
"default": "native",
367+
"clients": [
368+
{
369+
"key": "native",
370+
"title": "http::client",
371+
"link": "https://crystal-lang.org/api/master/HTTP/Client.html",
372+
"description": "Crystal HTTP client"
373+
}
374+
]
361375
}
362376
]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
require "http/client"
2+
3+
url = "http://mockbin.com/har"
4+
headers = HTTP::Headers{
5+
"content-type" => "application/x-www-form-urlencoded"
6+
}
7+
reqBody = "foo=bar&hello=world"
8+
9+
response = HTTP::Client.post url, headers: headers, body: reqBody
10+
puts response.body
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
require "http/client"
2+
3+
url = "http://mockbin.com/har"
4+
headers = HTTP::Headers{
5+
"content-type" => "application/json"
6+
}
7+
reqBody = "{\"number\":1,\"string\":\"f\\\"oo\",\"arr\":[1,2,3],\"nested\":{\"a\":\"b\"},\"arr_mix\":[1,\"a\",{\"arr_mix_nested\":{}}],\"boolean\":false}"
8+
9+
response = HTTP::Client.post url, headers: headers, body: reqBody
10+
puts response.body
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require "http/client"
2+
3+
url = "http://mockbin.com/har"
4+
headers = HTTP::Headers{
5+
"accept-encoding" => "deflate, gzip, br"
6+
}
7+
8+
response = HTTP::Client.get url, headers: headers
9+
puts response.body
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require "http/client"
2+
3+
url = "http://mockbin.com/har"
4+
headers = HTTP::Headers{
5+
"cookie" => "foo=bar; bar=baz"
6+
}
7+
8+
response = HTTP::Client.post url, headers: headers
9+
puts response.body
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require "http/client"
2+
3+
url = "http://mockbin.com/har"
4+
5+
response = HTTP::Client.exec "PROPFIND", url
6+
puts response.body
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
require "http/client"
2+
3+
url = "http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value"
4+
headers = HTTP::Headers{
5+
"cookie" => "foo=bar; bar=baz"
6+
"accept" => "application/json"
7+
"content-type" => "application/x-www-form-urlencoded"
8+
}
9+
reqBody = "foo=bar"
10+
11+
response = HTTP::Client.post url, headers: headers, body: reqBody
12+
puts response.body

0 commit comments

Comments
 (0)