Created
September 27, 2021 15:19
-
-
Save freakflames29/73461074ef95697143236633488189c5 to your computer and use it in GitHub Desktop.
Terminal loading while requesting to an URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#program to show buffering while requesting an api or a webpage | |
require "httparty" | |
module Buffer | |
$flag=true | |
def self.show inp,url#,tim | |
$reqUrl=url | |
def buffer inp | |
while $flag | |
print "#{inp} .\r" | |
sleep(0.3) | |
print "#{inp} ..\r" | |
sleep(0.3) | |
print "#{inp} ...\r" | |
sleep(0.3) | |
print " \r" | |
end | |
end | |
def self.req | |
req=HTTParty.get($reqUrl) | |
if req.code==200 | |
$flag=false | |
return req | |
else | |
$flag=false | |
return req | |
end | |
end | |
a=Thread.new{buffer inp} | |
b=Thread.new{req} | |
a.join | |
b.join | |
end | |
end | |
puts Time.now | |
def test | |
include Buffer | |
Buffer.show "Fetching Posts","https://jsonplaceholder.typicode.com/posts" | |
rs=Buffer.req() | |
if rs.code==200 | |
puts "Oh yeah" | |
else | |
puts "Nope" | |
end | |
end | |
test | |
# puts Time.now |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment