Skip to content

Commit b04c0ad

Browse files
committed
Merge pull request #40 from montanaflynn/master
Add Ruby target language
2 parents 45a0e36 + 6b9dd5a commit b04c0ad

File tree

16 files changed

+224
-0
lines changed

16 files changed

+224
-0
lines changed

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: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
var capMethod = method.charAt(0) + method.substring(1).toLowerCase()
17+
if (methods.indexOf(method) < 0) {
18+
code.push('class Net::HTTP::%s < Net::HTTPRequest')
19+
code.push(util.format('class Net::HTTP::%s < Net::HTTPRequest', capMethod))
20+
code.push(util.format(' METHOD = \'%s\'', method.toUpperCase()))
21+
code.push(util.format(' REQUEST_HAS_BODY = \'%s\'', self.source.postData.text ? 'true' : 'false'))
22+
code.push(' RESPONSE_HAS_BODY = true')
23+
code.push('end')
24+
code.push(null)
25+
}
26+
27+
code.push(util.format('url = URI("%s")', source.fullUrl))
28+
29+
code.push(null)
30+
31+
code.push('conn = Net::HTTP.new(url.host, url.port)')
32+
33+
if (source.uriObj.protocol === 'https:') {
34+
code.push('http.use_ssl = true')
35+
code.push('http.verify_mode = OpenSSL::SSL::VERIFY_NONE')
36+
}
37+
38+
code.push(null)
39+
40+
code.push(util.format('request = Net::HTTP::%s.new(url)', capMethod))
41+
42+
var headers = Object.keys(self.source.allHeaders)
43+
if (headers.length) {
44+
headers.map(function (key) {
45+
code.push(util.format('request["%s"] = \'%s\'', key, self.source.allHeaders[key]))
46+
})
47+
}
48+
49+
if (self.source.postData.text) {
50+
code.push(util.format('request.body = %s', JSON.stringify(self.source.postData.text)))
51+
}
52+
53+
code.push(null)
54+
55+
code.push('response = conn.request(request)')
56+
code.push('puts response.read_body')
57+
58+
return code.join('\n')
59+
}
60+
61+
module.exports.info = {
62+
key: 'native',
63+
title: 'net::http',
64+
link: 'http://ruby-doc.org/stdlib-2.2.1/libdoc/net/http/rdoc/Net/HTTPGenericRequest.html',
65+
description: 'Ruby request client'
66+
}

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)