This repository was archived by the owner on Aug 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 962
/
Copy pathrequestblock.rb
150 lines (136 loc) · 3.67 KB
/
requestblock.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#
# Author: Nick Quinlan
#
# Outputs request blocks, describing GET/POST Requests and their data
#
# {% requestblock %}
# {% requesturl POST %}
# https://api.sendgrid.com/api/mail.send.json
# {% endrequesturl %}
# {% requestdata data POST %}
# api_user=your_sendgrid_username&api_key=your_sendgrid_password
# {% endrequestdata %}
# {% endrequestblock %}
#
# ...will output...
#
# <table class="table table-bordered request-block">
# <tr>
# <td>POST</td>
# <td>
# <code>https://api.sendgrid.com/api/mail.send.json</code>
# </td>
# </tr>
# <tr>
# <td>POST Data</td>
# <td>
# <code class="wrap">api_user=your_sendgrid_username&api_key=your_sendgrid_password&to=destination@example.com&toname=Destination&subject=Example_Subject&text=testingtextbody&from=info@domain.com</code>
# </td>
# </tr>
# </table>
#
#
# USAGE:
#
# {% requestblock %}
# {% requesturl (<METHOD>) %}
# <URL>
# {% endrequesturl %}
# {% requestdata (<TYPE>) (<METHOD>) %}
# <DATA>
# {% endrequestdata %}
# {% endrequestblock %}
#
# METHOD - HTTP Verb (e.g. GET, POST, PUT...), defaults to POST
# TYPE - Data communication type (e.g. Headers, Data), defaults to Data
# URL - The url the request describes
# DATA - Data passed through the request, both headers and query strings will be formatted
#
# As many {% requestdata %} blocks as necessary can be included,
# e.g. you can make a request with no data, or data passed through both headers and query string.
module Jekyll
class RequestBlockTag < Liquid::Block
def initialize(tag_name, markup, tokens)
super
end
def render(context)
output = super
return <<HTML
<table class="table table-bordered request-block">
#{output}
</table>
HTML
end
end
class RequestBlockUrl < Liquid::Block
def initialize(tag_name, markup, tokens)
parameters = markup.split(" ")
if parameters[0]
@method = parameters[0].upcase
else
@method = "POST"
end
super
end
def render(context)
output = super
output.gsub!(/^\s*(.+)\s*$/, '\1')
return <<HTML
<tr>
<td>#{@method}</td>
<td>
<code>#{output}</code>
</td>
</tr>
HTML
end
end
class RequestBlockData < Liquid::Block
def initialize(tag_name, markup, tokens)
parameters = markup.split(" ")
if parameters[0]
case parameters[0].downcase
when "data"
@type = "Data"
when "headers"
@type = "Headers"
else
@type = "Data"
end
else
@type = "Data"
end
if parameters[1]
@method = parameters[1].upcase
else
@method = "POST"
end
super
end
def render(context)
output = super
output.gsub!(/^\s*(.+)\s*$/, '\1')
if @type == "Data"
output.gsub!('&', '&')
output.gsub!('&', '&<wbr>')
output.gsub!(/([^&=]+)=([^&=]+)/, '<span class="data-key">\1</span>=<span class="data-value">\2</span>')
elsif @type == "Headers"
output.gsub!(/^([^:]+):(.+)$/m, '<span class="data-key">\1</span>:<span class="data-value">\2</span><br />')
end
return <<HTML
<tr>
<td>#{@method} #{@type}</td>
<td>
<code class="requestdata requestdata-#{@type.downcase}">#{output}</code>
</td>
</tr>
HTML
end
def render_all(list, context)
super
end
end
end
Liquid::Template.register_tag('requestblock', Jekyll::RequestBlockTag)
Liquid::Template.register_tag('requesturl', Jekyll::RequestBlockUrl)
Liquid::Template.register_tag('requestdata', Jekyll::RequestBlockData)