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
|
# File 'lib/opendns-dnsdb/dnsdb/related.rb', line 9
def related_names_with_score(names, &filter)
names_is_array = names.kind_of?(Enumerable)
names = [ names ] unless names_is_array
multi = query_multi
queries_links = { }
queries_coocs = { }
names.each do |name|
url_links = "/links/name/#{name}.json"
query_links = query_handler(url_links)
multi.queue(query_links)
queries_links[name] = query_links
url_coocs = "/recommendations/name/#{name}.json"
query_coocs = query_handler(url_coocs)
multi.queue(query_coocs)
queries_coocs[name] = query_coocs
end
multi.run
responses = { }
queries_coocs.each_pair do |name, query|
if query.response.body.empty?
responses[name] ||= { }
next
end
obj = MultiJson.load(query.response.body)
if pfs2 = Response::Raw.new(obj).pfs2
responses[name] = Hash[*pfs2.flatten]
else
responses[name] = { }
end
end
queries_links.each_pair do |name, query|
responses[name] ||= { }
if query.response.body.empty?
next
end
obj = MultiJson.load(query.response.body)
if tb1 = Response::Raw.new(obj).tb1
upper = [1.0, tb1.map { |x| x[1] }.max].max
responses[name] = Hash[*tb1.map { |x| [x[0], x[1] / upper] }.flatten]
end
end
responses = Response::HashByName[responses]
if block_given?
responses.each_key do |name|
responses[name].select! do |related_name, score|
filter.call(related_name, score)
end
end
end
responses = responses.values.first unless names_is_array
responses
end
|