Class: QiitaMail::Selector

Inherits:
Object
  • Object
show all
Defined in:
lib/qiita_mail/selector.rb

Defined Under Namespace

Classes: ScoredItem

Constant Summary collapse

TOP_BONUS =

パラメータ

30
PER_PAGE =
40
USE_CACHE =

デバッグ用

false

Instance Method Summary collapse

Constructor Details

#initialize(settings, storage) ⇒ Selector




27
28
29
30
# File 'lib/qiita_mail/selector.rb', line 27

def initialize(settings, storage)
  @settings = settings
  @storage  = storage
end

Instance Method Details

#pickup(num = 5) ⇒ Object



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
# File 'lib/qiita_mail/selector.rb', line 32

def pickup(num = 5)
  # 候補記事をピックアップ
  container = pickup_in

  puts "Calculate Score .."

  # 既読記事は除外
  container.each_with_index do |items, index|
    container[index].delete_if do |item|
      @storage.reading? item.uuid
    end
  end

  # ScoredItemに変換、スコア順にソート
  container = container.map {|items| items.map {|item| ScoredItem.new item}.sort.reverse }

  # ボーナス加算
  container.each do |items|
    items[0].add_score(TOP_BONUS) unless items.empty?
  end

  # 合算して、重複は削除、スコア順にソート
  results = container.reduce([]) {|r,i| r += i}.uniq{|i| i.item.uuid}.sort.reverse

  # スコアの高い物をピックアップ、Itemに戻して返す
  results[0..num-1].map {|scored_item| scored_item.item}
end