-
Notifications
You must be signed in to change notification settings - Fork 3
/
ranking.rb
30 lines (25 loc) · 834 Bytes
/
ranking.rb
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
class Ranker
attr_reader :word_score, :hash
def initialize(hash, word)
@hash = hash
@word_score = 0
@word = word
end
def calculate_total_score
@hash.each do|key_hash, value_hash|
calculate_points_attribute(key_hash, value_hash, :urls, 5)
calculate_points_attribute(key_hash, value_hash, :keywords, 4)
calculate_points_attribute(key_hash, value_hash, :description, 3)
calculate_points_attribute(key_hash, value_hash, :headers, 2)
calculate_points_attribute(key_hash, value_hash, :text, 1)
end
end
private
def calculate_points_attribute(key_hash, value_hash, attribute, points)
if key_hash == attribute
value_hash.each do |key_attr, value_attr|
@word_score += (points * value_attr) if key_attr.include?(@word)
end
end
end
end