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_duration, #cache_id, #categories_id, #category_combos, #category_combos_id, #category_id, #combinatored_category_array, #no_query_id, #redis, #results_hashes_id, #set_category_combos_array, #sorted_category_array
Methods included from Helpers
#normalize, #prefixes_for_phrase
Constructor Details
#initialize(params = {}) ⇒ Matcher
Returns a new instance of Matcher.
4
5
6
|
# File 'lib/soulheart/matcher.rb', line 4
def initialize(params = {})
set_clean_opts(params)
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
50
51
52
|
# File 'lib/soulheart/matcher.rb', line 50
def cache_id_from_opts
"#{cache_id(categories_string)}#{@opts['q'].join(':')}"
end
|
#cache_it_because ⇒ Object
59
60
61
62
|
# File 'lib/soulheart/matcher.rb', line 59
def cache_it_because
redis.zinterstore(@cachekey, interkeys_from_opts)
redis.expire(@cachekey, cache_duration)
end
|
#categories_string ⇒ Object
42
43
44
|
# File 'lib/soulheart/matcher.rb', line 42
def categories_string
@opts['categories'].empty? ? 'all' : @opts['categories'].join('')
end
|
#category_id_from_opts ⇒ Object
46
47
48
|
# File 'lib/soulheart/matcher.rb', line 46
def category_id_from_opts
category_id(categories_string)
end
|
#clean_opts ⇒ Object
28
29
30
31
32
33
|
# File 'lib/soulheart/matcher.rb', line 28
def clean_opts
@opts['categories'] = sort_categories(@opts['categories'])
@opts['q'] = normalize(@opts['q']).split(' ') unless @opts['q'].is_a?(Array)
@opts
end
|
#interkeys_from_opts ⇒ Object
54
55
56
57
|
# File 'lib/soulheart/matcher.rb', line 54
def interkeys_from_opts
@opts['q'].empty? ? [no_query_id(@cid)] : @opts['q'].map { |w| "#{@cid}#{w}" }
end
|
#matches ⇒ Object
71
72
73
74
75
76
77
78
79
|
# File 'lib/soulheart/matcher.rb', line 71
def matches
cache_it_because if !@opts['cache'] || !redis.exists(@cachekey) || redis.exists(@cachekey) == 0
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)
matching_hashes(ids)
end
|
#matching_hashes(ids) ⇒ Object
64
65
66
67
68
69
|
# File 'lib/soulheart/matcher.rb', line 64
def matching_hashes(ids)
return [] unless ids.size > 0
results = redis.hmget(results_hashes_id, *ids)
results = results.reject(&:nil?)
results.map { |r| MultiJson.decode(r) }
end
|
#set_clean_opts(params) ⇒ Object
35
36
37
38
39
40
|
# File 'lib/soulheart/matcher.rb', line 35
def set_clean_opts(params)
@opts = self.class.default_params_hash.merge params
clean_opts
@cachekey = cache_id_from_opts
@cid = category_id_from_opts
end
|
#sort_categories(categories) ⇒ Object
20
21
22
23
24
25
26
|
# File 'lib/soulheart/matcher.rb', line 20
def sort_categories(categories)
return [] if categories.empty?
categories = categories.split(/,|\+/) unless categories.is_a?(Array)
categories = categories.map { |s| normalize(s) }.uniq.sort
categories = [] if categories.length == redis.scard(categories_id)
categories
end
|