Class: WinewooCore::Services::Finders::Wines::TopWinesFinder

Inherits:
WinesFinder
  • Object
show all
Defined in:
lib/winewoo_core/services/finders/wines/top_wines_finder.rb

Constant Summary collapse

MAX_WINES =

Max number of wines

25

Instance Method Summary collapse

Methods inherited from WinesFinder

#initialize

Constructor Details

This class inherits a constructor from WinewooCore::Services::Finders::Wines::WinesFinder

Instance Method Details

#findObject



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
# File 'lib/winewoo_core/services/finders/wines/top_wines_finder.rb', line 9

def find
  cached_wines = Redis.current.get("selected_wines") if Redis.current
  max = @top_wines.to_i

  if cached_wines.blank?
    query = WinewooCore::Repositories::Mongo::Queries::WineQuery.new.published.officials.with_label.percented_by(74, 100)
    query = query.from_interpro(@interpro.id) if @interpro

    wines = (0..query.count-1).sort_by{rand}.slice(0, MAX_WINES).collect! do |i|
      query.skip(i).first
    end

    if Redis.current
      wids = wines.map {|w| w.id }
      Redis.current.set("selected_wines", wids.to_json)
      Redis.current.expire "selected_wines", 86400
    end

    return wines.take(max.to_i), nil, nil
  else
    wines = JSON.parse(cached_wines).map do |wid|
      WinewooCore::Repositories::Mongo::Queries::WineQuery.new.find(wid)
    end

    return wines.take(max.to_i), nil, nil
  end
end