Skip to content

Commit baa3158

Browse files
author
Montana Flynn
committed
Add Ruby target language
1 parent 45a0e36 commit baa3158

16 files changed

Lines changed: 225 additions & 0 deletions

‎src/targets/ruby/index.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict'
2+
3+
module.exports = require('require-directory')(module)

‎src/targets/ruby/info.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict'
2+
3+
module.exports = {
4+
key: 'ruby',
5+
title: 'Ruby',
6+
extname: '.rb',
7+
default: 'native'
8+
}

‎src/targets/ruby/native.js‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
'use strict'
2+
3+
var util = require('util')
4+
5+
module.exports = function (source, options) {
6+
var self = this
7+
var code = []
8+
code.push('require \'uri\'')
9+
code.push('require \'net/http\'')
10+
code.push(null)
11+
12+
// To support custom methods we check for the supported methods
13+
// and if doesn't exist then we build a custom class for it
14+
var method = source.method.toUpperCase()
15+
var methods = ['GET', 'POST', 'HEAD', 'DELETE', 'PATCH', 'PUT', 'OPTIONS', 'COPY', 'LOCK', 'UNLOCK', 'MOVE', 'TRACE']
16+
method = method.toLowerCase()
17+
method = method.charAt(0).toUpperCase() + method.substring(1)
18+
if (methods.indexOf(this.source.method.toLowerCase()) === 0) {
19+
var hasBody = self.source.postData.text ? 'true' : 'false'
20+
code.push('class Net::HTTP::Checkout < Net::HTTPRequest')
21+
code.push(util.format(' METHOD = \'%s\'', method))
22+
code.push(util.format(' REQUEST_HAS_BODY = \'%s\'', hasBody))
23+
code.push(' RESPONSE_HAS_BODY = true')
24+
code.push('end')
25+
code.push(null)
26+
}
27+
28+
code.push(util.format('url = URI("%s")', source.fullUrl))
29+
30+
code.push(null)
31+
32+
code.push('conn = Net::HTTP.new(url.host, url.port)')
33+
34+
if (source.uriObj.protocol === 'https:') {
35+
code.push('http.use_ssl = true')
36+
code.push('http.verify_mode = OpenSSL::SSL::VERIFY_NONE')
37+
}
38+
39+
code.push(null)
40+
41+
code.push(util.format('request = Net::HTTP::%s.new(url)', method))
42+
43+
var headers = Object.keys(self.source.allHeaders)
44+
if (headers.length) {
45+
headers.map(function (key) {
46+
code.push(util.format('request["%s"] = \'%s\'', key, self.source.allHeaders[key]))
47+
})
48+
}
49+
50+
if (self.source.postData.text) {
51+
code.push(util.format('request.body = %s', JSON.stringify(self.source.postData.text)))
52+
}
53+
54+
code.push(null)
55+
56+
code.push('response = conn.request(request)')
57+
code.push('puts response.read_body')
58+
59+
return code.join('\n')
60+
}
61+
62+
module.exports.info = {
63+
key: 'native',
64+
title: 'net::http',
65+
link: 'http://ruby-doc.org/stdlib-2.2.1/libdoc/net/http/rdoc/Net/HTTPGenericRequest.html',
66+
description: 'Ruby request client'
67+
}

‎test/fixtures/available-targets.json‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,5 +146,19 @@
146146
"description": "Lightweight HTTP Request Client Library"
147147
}
148148
]
149+
},
150+
{
151+
"key": "ruby",
152+
"title": "Ruby",
153+
"extname": ".rb",
154+
"default": "native",
155+
"clients": [
156+
{
157+
"key": "native",
158+
"title": "net::http",
159+
"link": "http://ruby-doc.org/stdlib-2.2.1/libdoc/net/http/rdoc/Net/HTTPGenericRequest.html",
160+
"description": "Ruby request client"
161+
}
162+
]
149163
}
150164
]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require 'uri'
2+
require 'net/http'
3+
4+
url = URI("http://mockbin.com/har")
5+
6+
conn = Net::HTTP.new(url.host, url.port)
7+
8+
request = Net::HTTP::Post.new(url)
9+
request["content-type"] = 'application/x-www-form-urlencoded'
10+
request.body = "foo=bar&hello=world"
11+
12+
response = conn.request(request)
13+
puts response.read_body
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require 'uri'
2+
require 'net/http'
3+
4+
url = URI("http://mockbin.com/har")
5+
6+
conn = Net::HTTP.new(url.host, url.port)
7+
8+
request = Net::HTTP::Post.new(url)
9+
request["content-type"] = 'application/json'
10+
request.body = "{\"number\": 1, \"string\": \"f\\\"oo\", \"arr\": [1, 2, 3], \"nested\": {\"a\": \"b\"}, \"arr_mix\": [1, \"a\", {\"arr_mix_nested\": {}}] }"
11+
12+
response = conn.request(request)
13+
puts response.read_body
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
require 'uri'
2+
require 'net/http'
3+
4+
url = URI("http://mockbin.com/har")
5+
6+
conn = Net::HTTP.new(url.host, url.port)
7+
8+
request = Net::HTTP::Post.new(url)
9+
request["cookie"] = 'foo=bar; bar=baz'
10+
11+
response = conn.request(request)
12+
puts response.read_body
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require 'uri'
2+
require 'net/http'
3+
4+
url = URI("http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value")
5+
6+
conn = Net::HTTP.new(url.host, url.port)
7+
8+
request = Net::HTTP::Post.new(url)
9+
request["cookie"] = 'foo=bar; bar=baz'
10+
request["accept"] = 'application/json'
11+
request["content-type"] = 'application/x-www-form-urlencoded'
12+
request.body = "foo=bar"
13+
14+
response = conn.request(request)
15+
puts response.read_body
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require 'uri'
2+
require 'net/http'
3+
4+
url = URI("http://mockbin.com/har")
5+
6+
conn = Net::HTTP.new(url.host, url.port)
7+
8+
request = Net::HTTP::Get.new(url)
9+
request["accept"] = 'application/json'
10+
request["x-foo"] = 'Bar'
11+
12+
response = conn.request(request)
13+
puts response.read_body
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require 'uri'
2+
require 'net/http'
3+
4+
url = URI("http://mockbin.com/har")
5+
6+
conn = Net::HTTP.new(url.host, url.port)
7+
8+
request = Net::HTTP::Post.new(url)
9+
request["content-type"] = 'multipart/form-data; boundary=---011000010111000001101001'
10+
request.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--"
11+
12+
response = conn.request(request)
13+
puts response.read_body

0 commit comments

Comments
 (0)