Skip to content

Instantly share code, notes, and snippets.

@AvaelKross
Created August 13, 2014 21:32
Show Gist options
  • Save AvaelKross/012a319d7d7bfb100f93 to your computer and use it in GitHub Desktop.
Save AvaelKross/012a319d7d7bfb100f93 to your computer and use it in GitHub Desktop.
task - SE api / interesting questions for user
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