Skip to content

Instantly share code, notes, and snippets.

@rodrigovirgilio
Last active September 7, 2023 09:11
Show Gist options
  • Save rodrigovirgilio/4ec2140b78b6bfde844bc3ae5f5872d2 to your computer and use it in GitHub Desktop.
Save rodrigovirgilio/4ec2140b78b6bfde844bc3ae5f5872d2 to your computer and use it in GitHub Desktop.
Challenge Rainforest
class Challenge
require "json"
require "net/http"
URL = "https://www.letsrevolutionizetesting.com/challenge.json".freeze
MAX_ITERATIONS = 100
def self.call
# Initialize id_value with nil to start the loop
id_value = nil
MAX_ITERATIONS.times do
begin
response = extract_parameter(id_value)
body = JSON.parse(response.body)
puts body
follow_body = body["follow"]
if follow_body.nil?
puts body["message"] || "not found"
break
end
id_value = extract_id(follow_body)
puts URI("#{URL}?id=#{id_value}")
rescue StandardError => e
puts "An error occurred: #{e.message}"
break
end
end
end
private
# Define a method to extract the id from the follow_body
def self.extract_id(follow_body)
follow_body.match(/id=(\d+)/)[1] if follow_body
end
# Define a method to extract the parameter from the id_value
def self.extract_parameter(id_value)
id_value.nil? ? Net::HTTP.get_response(URI(URL)) : Net::HTTP.get_response(URI("#{URL}?id=#{id_value}"))
end
end
Challenge.call
@rodrigovirgilio
Copy link
Author

Log in local environment

irb(main):001:1* class Challenge
irb(main):002:1*   require "json"
irb(main):003:1*   require "net/http"
irb(main):004:1*
irb(main):005:1*   URL = "https://www.letsrevolutionizetesting.com/challenge.json".freeze
irb(main):006:1*   MAX_ITERATIONS = 100
irb(main):007:1*
irb(main):008:2*   def self.call
irb(main):009:2*     # Initialize id_value with nil to start the loop
irb(main):010:2*     id_value = nil
irb(main):011:2*
irb(main):012:3*     MAX_ITERATIONS.times do
irb(main):013:4*       begin
irb(main):014:4*         response = extract_parameter(id_value)
irb(main):015:4*         body = JSON.parse(response.body)
irb(main):016:4*         puts body
irb(main):017:4*
irb(main):018:4*         follow_body = body["follow"]
irb(main):019:5*         if follow_body.nil?
irb(main):020:5*           puts body["message"] || "not found"
irb(main):021:5*           break
irb(main):022:4*         end
irb(main):023:4*
irb(main):024:4*         id_value = extract_id(follow_body)
irb(main):025:4*         puts URI("#{URL}?id=#{id_value}")
irb(main):026:4*       rescue StandardError => e
irb(main):027:4*         puts "An error occurred: #{e.message}"
irb(main):028:4*         break
irb(main):029:3*       end
irb(main):030:2*     end
irb(main):031:1*   end
irb(main):032:1*
irb(main):033:1*   private
irb(main):034:1*
irb(main):035:1*   # Define a method to extract the id from the follow_body
irb(main):036:2*   def self.extract_id(follow_body)
irb(main):037:2*     follow_body.match(/id=(\d+)/)[1] if follow_body
irb(main):038:1*   end
irb(main):039:1*
irb(main):040:1*   # Define a method to extract the parameter from the id_value
irb(main):041:2*   def self.extract_parameter(id_value)
irb(main):042:2*     id_value.nil? ? Net::HTTP.get_response(URI(URL)) : Net::HTTP.get_response(URI("#{URL}?id=#{id_value}"))
irb(main):043:1*   end
irb(main):044:0> end
=> :extract_parameter
irb(main):045:0>
irb(main):046:0> Challenge.call
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=756775492", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=756775492
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=995287801", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=995287801
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=572744636", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=572744636
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=461736456", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=461736456
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=47526821", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=47526821
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=80296008", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=80296008
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=567724423", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=567724423
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=32758300", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=32758300
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=580396716", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=580396716
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=479167105", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=479167105
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=949971757", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=949971757
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=861310992", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=861310992
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=937651877", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=937651877
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=354271637", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=354271637
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=705630460", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=705630460
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=606216317", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=606216317
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=65584552", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=65584552
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=831657532", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=831657532
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=256000467", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=256000467
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=161184211", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=161184211
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=208550157", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=208550157
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=475613963", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=475613963
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=37577102", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=37577102
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=724114586", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=724114586
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=556333858", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=556333858
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=115660865", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=115660865
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=716571928", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=716571928
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=596834351", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=596834351
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=630823541", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=630823541
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=652202071", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=652202071
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=300716465", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=300716465
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=155015675", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=155015675
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=135205298", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=135205298
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=897873681", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=897873681
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=967454426", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=967454426
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=71656714", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=71656714
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=142868048", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=142868048
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=930312428", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=930312428
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=197119602", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=197119602
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=617265884", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=617265884
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=970138205", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=970138205
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=201907239", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=201907239
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=686630567", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=686630567
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=121457796", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=121457796
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=488888020", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=488888020
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=895507276", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=895507276
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=173103113", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=173103113
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=803136089", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=803136089
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=480810857", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=480810857
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=553833934", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=553833934
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=801238070", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=801238070
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=477183693", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=477183693
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=278432362", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=278432362
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=75367184", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=75367184
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=653620253", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=653620253
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=620822263", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=620822263
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=368201548", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=368201548
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=202982190", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=202982190
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=449699507", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=449699507
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=469802573", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=469802573
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=100196142", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=100196142
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=532212727", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=532212727
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=180103962", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=180103962
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=569247979", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=569247979
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=513897475", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=513897475
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=911918452", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=911918452
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=515668116", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=515668116
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=400102864", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=400102864
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=598053525", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=598053525
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=94928201", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=94928201
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=765856838", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=765856838
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=529200894", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=529200894
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=300886136", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=300886136
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=307895327", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=307895327
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=530146612", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=530146612
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=599920573", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=599920573
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=135743876", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=135743876
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=454110831", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=454110831
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=995478629", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=995478629
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=582208015", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=582208015
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=811715985", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=811715985
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=650890482", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=650890482
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=822042114", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=822042114
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=582600663", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=582600663
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=314974329", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=314974329
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=169280871", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=169280871
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=5136418", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=5136418
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=329594904", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=329594904
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=183208047", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=183208047
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=627597879", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=627597879
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=50576458", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=50576458
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=302722799", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=302722799
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=936408135", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=936408135
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=254043118", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=254043118
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=702496729", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=702496729
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=94985935", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=94985935
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=951438732", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=951438732
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=903777032", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=903777032
{"follow"=>"https://www.letsrevolutionizetesting.com/challenge?id=503047199", "message"=>"This is not the end"}
https://www.letsrevolutionizetesting.com/challenge.json?id=503047199
{"message"=>"Congratulations! You've reached the end! Please add the code (or a link to a repo with the code in it) you used to solve it to your application. We'll be in touch shortly!"}
Congratulations! You've reached the end! Please add the code (or a link to a repo with the code in it) you used to solve it to your application. We'll be in touch shortly!
=> nil
irb(main):047:0>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment