Skip to content

Instantly share code, notes, and snippets.

@octosteve
Last active December 15, 2015 10:29
Show Gist options
  • Save octosteve/5245726 to your computer and use it in GitHub Desktop.
Save octosteve/5245726 to your computer and use it in GitHub Desktop.
Fetch by category from Rubeque. You need the rest-client gem
#!/usr/bin/env ruby
require 'rest-client'
require 'json'
class Rubeque
def initialize
@questions = categorize
end
def categories
puts @questions.keys
end
def questions_for(category)
@questions[category].sort_by {|q| q["difficulty"].to_i}.each do |question|
puts "Difficulty #{question["difficulty"]}"
puts question["instructions"]
puts question["code"]
puts "-" * 40
puts
end
end
private
def categorize
result = JSON.load(RestClient.get("http://www.rubeque.com/problems.json"))
questions = Hash.new {|k,v| k[v] = [] }
result.each_with_object(questions) do |r|
if r["tags"]
r["tags"].each do |t|
questions[t["name"].strip] << r
end
else
questions["untagged"] << r
end
end
end
end
rbq = Rubeque.new
rbq.questions_for 'enumerables'
rbq.categories
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment