forked from joevartuli/POSTManToCurl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PostManRequest.rb
73 lines (58 loc) · 1.3 KB
/
PostManRequest.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
require 'rubygems'
require 'json'
class PostManRequest
attr_accessor :name, :url, :method, :headers, :data, :data_mode
def initialize(request)
@name = request['name']
@url = request['url']
@method = request['method']
@headers = request['headers']
@data = request['data']
@data_mode = request['dataMode']
end
def to_curl
puts "# #{@name}"
puts "curl" << headers << method << data << url
puts
end
def headers
curlHeaders = ""
requestHeaders = @headers.split("\n")
requestHeaders.each {|requestHeader| curlHeaders << " -H #{requestHeader.sub(" ", "")}"}
end
return "#{curlHeaders}"
end
def data
modifiedData = @data.gsub("\n", "")
return " -d '#{modifiedData.gsub(/ +/, " ")}'"
end
return ""
end
def method
return " -X #{@method}"
end
def url
return " \"#{@url}\""
end
end
def help
puts 'Invalid argument'
puts 'Usage:'
puts '$ ruby PostManRequest.rb fileToParse'
puts
end
def main
if ARGV.length != 1
help
exit 1
end
file = File.open(ARGV[0], "r")
contents = file.read
parsedContent = JSON.parse contents
postManRequest = []
parsedContent["requests"].each { |request| postManRequest.push(PostManRequest.new(request)) }
postManRequest.each {|request| request.to_curl }
end
main