Skip to content

Instantly share code, notes, and snippets.

@mchail
Created August 18, 2012 00:20
Show Gist options
  • Save mchail/3383602 to your computer and use it in GitHub Desktop.
Save mchail/3383602 to your computer and use it in GitHub Desktop.
getclever.com puzzle solution
class CleverSolver
require 'rest-client'
require 'json'
def initialize
@root = "https://DEMO_KEY:@api.getclever.com"
@next_url = "/v1.1/students"
@vcount = 0
end
def run
while not @next_url.nil?
puts "getting next page: #{@root + @next_url}"
response = RestClient.get(@root + @next_url)
json = JSON.parse(response)
@next_url = get_next_url(json)
students = json['data']
@vcount += students.select{|s| s['data']['name']['last'].match(/^v/i)}.count
end
return @vcount
end
def get_next_url(json)
links = json['links']
next_url_data = links.select{|l| l['rel'] == 'next'}.first
return next_url_data['uri'] rescue nil
end
end
answer = CleverSolver.new.run
puts "Found #{answer} students whose last names start with 'v'."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment