Created
August 10, 2023 05:26
-
-
Save tlarevo/dc97fac91f2155c16bc594d120972fba to your computer and use it in GitHub Desktop.
Solving a challenge
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
require 'uri' | |
require 'net/http' | |
require 'json' | |
require 'cgi' | |
module Challenge | |
# Evaluate the uri, parse the payload and return updated uri or nil if | |
# payload does not have 'follow' key | |
def self.check_uri(uri) | |
response = Net::HTTP.get_response(uri) | |
payload = JSON.parse(response.body) | |
follow = payload.is_a?(Hash) && payload.key?('follow') ? payload['follow'] : nil | |
if follow.nil? | |
message = payload.is_a?(Hash) && payload.key?('message') ? payload['message'] : 'Empty output' | |
puts message | |
return nil | |
end | |
puts "Follow: #{follow}" | |
params = CGI.parse(URI.parse(follow).query) | |
uri.query = URI.encode_www_form(params) | |
puts "URI: #{uri}" | |
uri | |
end | |
# Check the uri has 'follow' key and continue the loop if not return nil | |
def self.solve(url) | |
uri = URI(url) | |
loop do | |
uri = check_uri(uri) | |
break if uri.nil? | |
end | |
end | |
end | |
Challenge.solve('https://www.letsrevolutionizetesting.com/challenge.json') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment