Forked from valchonedelchev/gist:a48da9b6050983d56625
Last active
August 29, 2015 14:14
-
-
Save ramigb/51fe1df3d0eb685abab1 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
require 'net/http' | |
require 'json' | |
def http_get(url) | |
puts "http_get: #{url}" | |
return Net::HTTP.get(URI(url)) | |
end | |
def handle_http(response, url) | |
if response['You should try JSON'] | |
return url.sub 'challenge?id', 'challenge.json?id' | |
elsif response =~ /^{"follow":"http:\/\/[\w\.\/]+\?id=\d+"}$/ | |
return JSON.parse(response).fetch('follow').sub 'challenge?id', 'challenge.json?id' | |
else | |
puts response | |
end | |
return nil | |
end | |
def play_with(url) | |
begin | |
response = http_get(url) | |
url = handle_http(response, url) | |
end while not url.nil? | |
end | |
play_with(ARGV.shift) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment