Skip to content

Instantly share code, notes, and snippets.

@davidpaulhunt
Created February 5, 2018 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidpaulhunt/b2d70397bd6c64e0d77f039e6f6cc223 to your computer and use it in GitHub Desktop.
Save davidpaulhunt/b2d70397bd6c64e0d77f039e6f6cc223 to your computer and use it in GitHub Desktop.
You should try JSON
const request = require('request');
const INIT_URL = 'http://letsrevolutionizetesting.com/challenge.json';
function requestChallenge(url) {
return new Promise((resolve, reject) => {
request.get(url, { json: true }, (error, response, body) => {
if (error) {
reject(error);
} else {
resolve(body);
}
});
});
}
function followChallenge(url = INIT_URL) {
return requestChallenge(url)
.then((challenge) => {
if (Object.prototype.hasOwnProperty.call(challenge, 'follow')) {
return followChallenge(challenge.follow.replace('challenge?', 'challenge.json?'));
}
return challenge;
});
}
followChallenge()
.then((result) => {
console.log('final result is', result);
process.exit(0);
})
.catch((err) => {
console.error('oops an error', err);
process.exit(0);
});
// I also wanted to take a few minutes and try transferring it to ruby sooo...
/**
#!/usr/bin/env ruby
require 'json'
require 'net/http'
result = nil
uri = URI('http://letsrevolutionizetesting.com/challenge.json')
while result == nil
res = Net::HTTP.get_response(uri)
challenge = JSON.parse(res.body)
if challenge.has_key?("follow")
uri = URI(challenge["follow"].sub("challenge?", "challenge.json?"))
else
result = challenge
end
end
puts result
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment