Class: Soulheart::Matcher
- Inherits:
-
Base
- Object
- Base
- Soulheart::Matcher
show all
- Defined in:
- lib/soulheart/matcher.rb
Instance Attribute Summary collapse
Attributes inherited from Base
#type
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Base
#base_id, #cache_id, #cache_length, #categories_id, #category_combos, #category_combos_id, #category_id, #no_query_id, #redis, #results_hashes_id, #set_category_combos_array
Methods included from Helpers
#normalize, #prefixes_for_phrase
Constructor Details
#initialize(params = {}) ⇒ Matcher
3
4
5
6
|
# File 'lib/soulheart/matcher.rb', line 3
def initialize(params = {})
@opts = self.class.default_params_hash.merge params
clean_opts
end
|
Instance Attribute Details
#opts ⇒ Object
Returns the value of attribute opts.
8
9
10
|
# File 'lib/soulheart/matcher.rb', line 8
def opts
@opts
end
|
Class Method Details
.default_params_hash ⇒ Object
10
11
12
13
14
15
16
17
18
|
# File 'lib/soulheart/matcher.rb', line 10
def self.default_params_hash
{
'page' => 1,
'per_page' => 5,
'categories' => [],
'q' => '', 'cache' => true
}
end
|
Instance Method Details
#cache_id_from_opts ⇒ Object
39
40
41
|
# File 'lib/soulheart/matcher.rb', line 39
def cache_id_from_opts
"#{cache_id(categories_string)}#{@opts['q'].join(':')}"
end
|
#categories_string ⇒ Object
31
32
33
|
# File 'lib/soulheart/matcher.rb', line 31
def categories_string
@opts['categories'].empty? ? 'all' : @opts['categories'].join('')
end
|
#category_id_from_opts ⇒ Object
35
36
37
|
# File 'lib/soulheart/matcher.rb', line 35
def category_id_from_opts
category_id(categories_string)
end
|
#clean_opts ⇒ Object
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/soulheart/matcher.rb', line 20
def clean_opts
unless @opts['categories'] == '' || @opts['categories'] == []
@opts['categories'] = @opts['categories'].split(/,|\+/) unless @opts['categories'].is_a?(Array)
@opts['categories'] = @opts['categories'].map { |s| normalize(s) }.uniq.sort
@opts['categories'] = [] if @opts['categories'].length == redis.scard(categories_id)
end
@opts['q'] = normalize(@opts['q']).split(' ') unless @opts['q'].is_a?(Array)
@opts
end
|
#interkeys_from_opts(cid) ⇒ Object
43
44
45
46
|
# File 'lib/soulheart/matcher.rb', line 43
def interkeys_from_opts(cid)
@opts['q'].empty? ? [no_query_id(cid)] : @opts['q'].map { |w| "#{cid}#{w}" }
end
|
#matches ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/soulheart/matcher.rb', line 48
def matches
cachekey = cache_id_from_opts
cid = category_id_from_opts
if !@opts['cache'] || !redis.exists(cachekey) || redis.exists(cachekey) == 0
interkeys = interkeys_from_opts(cid)
redis.zinterstore(cachekey, interkeys)
redis.expire(cachekey, cache_length) end
offset = (@opts['page'].to_i - 1) * @opts['per_page'].to_i
limit = @opts['per_page'].to_i + offset - 1
limit = 0 if limit < 0
ids = redis.zrange(cachekey, offset, limit) if ids.size > 0
results = redis.hmget(results_hashes_id, *ids)
results = results.reject(&:nil?) results.map { |r| MultiJson.decode(r) }
else
[]
end
end
|