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
|
# Fetch the specified URL and print the response.
# @param url [String] the URL to parse and fetch.
# @param method [String] the HTTP method to use.
def fetch(url, method:)
require 'async/http/internet'
require 'kernel/sync'
terminal = Console::Terminal.for($stdout)
terminal[:request] = terminal.style(:blue, nil, :bold)
terminal[:response] = terminal.style(:green, nil, :bold)
terminal[:length] = terminal.style(nil, nil, :bold)
terminal[:key] = terminal.style(nil, nil, :bold)
terminal[:chunk_0] = terminal.style(:blue)
terminal[:chunk_1] = terminal.style(:cyan)
align = 20
format_body = proc do |body, terminal|
if body
if length = body.length
terminal.print(:body, "body with length ", :length, length, "B")
else
terminal.print(:body, "body without length")
end
else
terminal.print(:body, "no body")
end
end.curry
Sync do
internet = Async::HTTP::Internet.new
response = internet.send(method.downcase.to_sym, url)
terminal.print_line(
:request, method.rjust(align), :reset, ": ", url
)
terminal.print_line(
:response, "version".rjust(align), :reset, ": ", response.version
)
terminal.print_line(
:response, "status".rjust(align), :reset, ": ", response.status,
)
terminal.print_line(
:response, "body".rjust(align), :reset, ": ", format_body[response.body],
)
response.headers.each do |key, value|
terminal.print_line(
:key, key.rjust(align), :reset, ": ", :value, value.inspect
)
end
if body = response.body
index = 0
style = [:chunk_0, :chunk_1]
response.body.each do |chunk|
terminal.print(style[index % 2], chunk)
index += 1
end
end
response.finish
if trailer = response.headers.trailer
trailer.each do |key, value|
terminal.print_line(
:key, key.rjust(align), :reset, ": ", :value, value.inspect
)
end
end
internet.close
end
end
# GET the specified URL and print the response.
def get(url)
self.fetch(url, method: "GET")
end
# HEAD the specified URL and print the response.
def head(url)
self.fetch(url, method: "HEAD")
end
|