-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathso_docs.rb
More file actions
66 lines (60 loc) · 1.78 KB
/
so_docs.rb
File metadata and controls
66 lines (60 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
require 'json'
require 'httparty'
class SODocs
def initialize()
@topics = []
@examples = []
@doctags = []
@contributors = []
@contributortypes = []
@topichistories = []
@display_names = {}
end
def load_json (file)
# A little meta programming here to simplify things.
eval("@#{file} = JSON.parse(IO.read('#{file}.json')) if @#{file}.empty?")
return eval("@#{file}")
end
def index (array, index)
lookup = {}
Array(load_json(array)).each do | node |
lookup[node[index]] = node
end
return lookup
end
def multi_index (array, index)
lookup = {}
Array(load_json(array)).each do | node |
lookup[node[index]] ||= []
lookup[node[index]].push(node)
end
return lookup
end
def fetch_display_names(user_ids)
user_ids.each do | id |
# Default to userXXXX in case we can't find the user via the API
@display_names[id] ||= "user#{id}"
end
# The API only supports 100 user_ids at a time: http://api.stackexchange.com/docs/users-by-ids
user_ids.each_slice(100) do | a |
ids = a.join(';')
uri = URI("http://api.stackexchange.com/2.2/users/#{ids}")
params = { :site => 'stackoverflow',
:pagesize => 100 }
params[:key] = 'vff6LW*9JQzZOECmU1FK1g((' # This is not considered a secret.
uri.query = URI.encode_www_form(params)
response = HTTParty.get(uri)
case response.code
when 200
users = JSON.parse(response.body)['items']
users.each do | user |
@display_names[user['user_id']] = user['display_name']
end
else
STDERR.puts uri, uri.to_s.length, response.code
STDERR.puts response
end
end
return @display_names
end
end