Class: Lentil::PopularityCalculator

Inherits:
Object
  • Object
show all
Defined in:
lib/lentil/popularity_calculator.rb

Instance Method Summary collapse

Instance Method Details

#calculate_popularity(image) ⇒ integer

Takes image object and returns a popularity score

Parameters:

  • image (object)

    object with image data

Returns:

  • (integer)

    popularity score of image



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
# File 'lib/lentil/popularity_calculator.rb', line 9

def calculate_popularity(image)

  # A staff like is worth 10 points
  if (image.staff_like === false)
    staff_like_points = 0
  else
    staff_like_points = 10
  end

  # likes have diminishing returns
  # 10 likes is 13 points
  # 100 likes is 25 points
  if image.like_votes_count > 0
    like_vote_points = Math::log(image.like_votes_count, 1.5).round
  else
    like_vote_points = 0
  end

  # if an image has fewer than 5 battle rounds this is 0
  # 5 or more and the points awarded is the win_pct/2
  if image.win_pct and image.wins_count + image.losses_count > 4
    battle_points = (image.win_pct/1.5).round
  else
    battle_points = 0
  end

  battle_points + like_vote_points + staff_like_points

end

#get_score_write_to_db(image) ⇒ Object



46
47
48
49
# File 'lib/lentil/popularity_calculator.rb', line 46

def get_score_write_to_db(image)
  popularity_score = calculate_popularity(image)
  image.update_attribute(:popular_score, popularity_score)
end

#update_image_popularity_score(image_to_update = :all) ⇒ boolean

Takes an image id and updates its popularity score

Parameters:

  • id (integer)

    of image to update defaults to :all

Returns:

  • (boolean)

    success/failure of update



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/lentil/popularity_calculator.rb', line 44

def update_image_popularity_score(image_to_update = :all)

  def get_score_write_to_db(image)
    popularity_score = calculate_popularity(image)
    image.update_attribute(:popular_score, popularity_score)
  end

  if image_to_update == :all
    images = Lentil::Image.find(image_to_update)
    images.each  do |image|
      get_score_write_to_db(image)
    end
  elsif image_to_update.kind_of?(Integer)
      image = Lentil::Image.find(image_to_update)
      get_score_write_to_db(image)
  end
end