Created
August 13, 2014 21:32
-
-
Save AvaelKross/012a319d7d7bfb100f93 to your computer and use it in GitHub Desktop.
task - SE api / interesting questions for user
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
module StackExchangeAPI | |
require 'net/http' | |
require 'json' | |
require 'csv' | |
DOMAIN = 'http://api.stackexchange.com/' | |
PARAMS = {site: 'stackoverflow'} | |
class << self | |
def get_user_info(id) | |
path = "users/#{id}" | |
uri = URI(DOMAIN+path) | |
uri.query = URI.encode_www_form(PARAMS) | |
response = Net::HTTP.get(uri) | |
JSON.parse(response)['items'].first | |
end | |
def get_tags_for_user(id) | |
path = "users/#{id}/tags" | |
uri = URI(DOMAIN+path) | |
uri.query = URI.encode_www_form(PARAMS) | |
response = Net::HTTP.get(uri) | |
JSON.parse(response)['items'] | |
end | |
def get_questions_by_tags(tags) | |
path = "search" | |
uri = URI(DOMAIN+path) | |
uri.query = PARAMS.merge({tagged:tags.join(";")}).map {|k,v| "#{k}=#{v}"}.join("&") | |
response = Net::HTTP.get(uri) | |
JSON.parse(response)['items'] | |
end | |
def get_users_ids_by_username(username) | |
response = Net::HTTP.get(URI("http://data.stackexchange.com/stackoverflow/csv/126464?Name="+username.gsub(" ","+"))) | |
parsed_response = CSV.parse(response) | |
parsed_response.shift | |
parsed_response.flatten! | |
raise "User not found" if parsed_response.empty? | |
if parsed_response.size > 1 | |
id = nil | |
until parsed_response.include?(id) | |
p "Specify one of ids (Not one user with this username):" | |
p parsed_response.join(", ") | |
id = gets.chomp | |
end | |
else | |
id = parsed_response.first | |
end | |
return id | |
end | |
end | |
end | |
p "Enter User name" | |
username = gets.chomp | |
id = StackExchangeAPI.get_users_ids_by_username(username) | |
tags = StackExchangeAPI.get_tags_for_user(id).map{|item| item['name'] } | |
questions = StackExchangeAPI.get_questions_by_tags(tags) | |
p "Interesting questions for user:" | |
questions.each do |q| | |
p "#{q['title']} [#{q['link']}]" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment