soulheart 0.0.10 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b7614d5a25688e37c96f1934f0e431048603771d
4
- data.tar.gz: 7bede6c0cd14ddb287636f8194d641eb1842625c
3
+ metadata.gz: 5c3a5a5f5c2e5fdce0a5270fa703ab31a72196a1
4
+ data.tar.gz: 96bad6202e435d62d9b25b32b1a7c96b333f8fe3
5
5
  SHA512:
6
- metadata.gz: 6380d4fa61e9101397e254afc09b4fe8b765f396728f2b68ad358224bf6ac09f36dad401c0079cf0dc7b28bccd22b4ab0a1c2fa9bc0e7f7868aa47a7d16714eb
7
- data.tar.gz: 5c1700b08f56ef30e12fdef6520f1d5b89641b2e6480a2489edb75ba67b8b6e786b15f9ff20f2a63dccca12766db6981253ecc15ba0854fed6f7ea5e5cb13da9
6
+ metadata.gz: c8a67e56e9c167a820f0bba16d1437e5f84be7f971629f548939a17598cdbf32b07ff15542bb4d5b869448d0539634bbacc9502a781e2ce4d0c547dbdd541b89
7
+ data.tar.gz: 24d1d651a927629d63d06a1ea4d3038d056d9555d2baf6820dd4292d505d4174009495856801c69b76af9b9e17cb0d049a5d4227e32b07a4ef6a92708e5956a2
@@ -8,7 +8,7 @@ module Soulheart
8
8
  Soulheart.redis
9
9
  end
10
10
 
11
- def cache_length
11
+ def cache_duration
12
12
  10 * 60 # Setting to 10 minutes, but making it possible to edit down the line
13
13
  end
14
14
 
@@ -16,13 +16,22 @@ module Soulheart
16
16
  ENV['RACK_ENV'] != 'test' ? 'soulheart:' : 'soulheart_test:'
17
17
  end
18
18
 
19
+ def sorted_category_array
20
+ redis.smembers(categories_id).map { |c| normalize(c) }.uniq.sort
21
+ end
22
+
23
+ def combinatored_category_array
24
+ 1.upto(sorted_category_array.size).
25
+ flat_map { |n| sorted_category_array.combination(n).
26
+ map { |el| el.join('') } }
27
+ end
28
+
19
29
  def set_category_combos_array
20
30
  redis.expire category_combos_id, 0
21
- ar = redis.smembers(categories_id).map { |c| normalize(c) }.uniq.sort
22
- ar = 1.upto(ar.size).flat_map { |n| ar.combination(n).map { |el| el.join('') } }
23
- ar.last.replace('all')
24
- redis.sadd category_combos_id, ar
25
- ar
31
+ array = combinatored_category_array
32
+ array.last.replace('all')
33
+ redis.sadd category_combos_id, array
34
+ array
26
35
  end
27
36
 
28
37
  def category_combos_id
@@ -67,7 +67,7 @@ module Soulheart
67
67
  end
68
68
  end
69
69
 
70
- def clean(item)
70
+ def clean_hash(item)
71
71
  fail ArgumentError, 'Items must have text' unless item['text']
72
72
  default_items_hash(item.delete('text'), item.delete('category'))
73
73
  .tap { |i| i['data'].merge!(item.delete('data')) if item['data'] }
@@ -75,17 +75,21 @@ module Soulheart
75
75
  .merge item
76
76
  end
77
77
 
78
- def add_item(item, category_base_id=nil, cleaned=false)
79
- unless cleaned
80
- item = clean(item)
81
- category_base_id ||= category_id(item['category'])
82
- item.keys.select{ |k| !%w(category priority term aliases data).include?(k) }.each do |key|
83
- item['data'].merge!({"#{key}" => item.delete(key)})
84
- end
85
- unless redis.smembers(categories_id).include?(item['category'])
86
- redis.sadd categories_id, item['category']
87
- end
78
+ def clean(item)
79
+ item = clean_hash(item)
80
+ item.keys.select{ |k| !%w(category priority term aliases data).include?(k) }.each do |key|
81
+ item['data'].merge!({"#{key}" => item.delete(key)})
88
82
  end
83
+ unless redis.smembers(categories_id).include?(item['category'])
84
+ redis.sadd categories_id, item['category']
85
+ end
86
+ item
87
+ end
88
+
89
+ def add_item(item, category_base_id=nil, cleaned=false)
90
+ item = clean(item) unless cleaned
91
+ category_base_id ||= category_id(item['category'])
92
+
89
93
  priority = (-item['priority'])
90
94
  redis.pipelined do
91
95
  redis.zadd(no_query_id(category_base_id), priority, item['term']) # Add to master set for queryless searches
@@ -1,8 +1,7 @@
1
1
  module Soulheart
2
2
  class Matcher < Base
3
3
  def initialize(params = {})
4
- @opts = self.class.default_params_hash.merge params
5
- clean_opts
4
+ set_clean_opts(params)
6
5
  end
7
6
 
8
7
  attr_accessor :opts
@@ -17,17 +16,28 @@ module Soulheart
17
16
  }
18
17
  end
19
18
 
19
+ def sort_categories(categories)
20
+ return [] if categories.empty?
21
+ categories = categories.split(/,|\+/) unless categories.is_a?(Array)
22
+ categories = categories.map { |s| normalize(s) }.uniq.sort
23
+ categories = [] if categories.length == redis.scard(categories_id)
24
+ categories
25
+ end
26
+
20
27
  def clean_opts
21
- unless @opts['categories'] == '' || @opts['categories'] == []
22
- @opts['categories'] = @opts['categories'].split(/,|\+/) unless @opts['categories'].is_a?(Array)
23
- @opts['categories'] = @opts['categories'].map { |s| normalize(s) }.uniq.sort
24
- @opts['categories'] = [] if @opts['categories'].length == redis.scard(categories_id)
25
- end
28
+ @opts['categories'] = sort_categories(@opts['categories'])
26
29
  @opts['q'] = normalize(@opts['q']).split(' ') unless @opts['q'].is_a?(Array)
27
30
  # .reject{ |i| i && i.length > 0 } .split(' ').reject{ Soulmate.stop_words.include?(w) }
28
31
  @opts
29
32
  end
30
33
 
34
+ def set_clean_opts(params)
35
+ @opts = self.class.default_params_hash.merge params
36
+ clean_opts
37
+ @cachekey = cache_id_from_opts
38
+ @cid = category_id_from_opts
39
+ end
40
+
31
41
  def categories_string
32
42
  @opts['categories'].empty? ? 'all' : @opts['categories'].join('')
33
43
  end
@@ -40,32 +50,31 @@ module Soulheart
40
50
  "#{cache_id(categories_string)}#{@opts['q'].join(':')}"
41
51
  end
42
52
 
43
- def interkeys_from_opts(cid)
53
+ def interkeys_from_opts
44
54
  # If there isn't a query, we use a special key in redis
45
- @opts['q'].empty? ? [no_query_id(cid)] : @opts['q'].map { |w| "#{cid}#{w}" }
55
+ @opts['q'].empty? ? [no_query_id(@cid)] : @opts['q'].map { |w| "#{@cid}#{w}" }
46
56
  end
47
57
 
48
- def matches
49
- cachekey = cache_id_from_opts
50
- cid = category_id_from_opts
58
+ def cache_it_because
59
+ redis.zinterstore(@cachekey, interkeys_from_opts)
60
+ redis.expire(@cachekey, cache_duration) # cache_duration is set in base.rb
61
+ end
51
62
 
52
- if !@opts['cache'] || !redis.exists(cachekey) || redis.exists(cachekey) == 0
53
- interkeys = interkeys_from_opts(cid)
54
- redis.zinterstore(cachekey, interkeys)
55
- redis.expire(cachekey, cache_length) # cache_length is set in base.rb
56
- end
63
+ def matching_hashes(ids)
64
+ return [] unless ids.size > 0
65
+ results = redis.hmget(results_hashes_id, *ids)
66
+ results = results.reject(&:nil?) # handle cached results for ids which have since been deleted
67
+ results.map { |r| MultiJson.decode(r) }
68
+ end
69
+
70
+ def matches
71
+ cache_it_because if !@opts['cache'] || !redis.exists(@cachekey) || redis.exists(@cachekey) == 0
57
72
  offset = (@opts['page'].to_i - 1) * @opts['per_page'].to_i
58
73
  limit = @opts['per_page'].to_i + offset - 1
59
74
 
60
75
  limit = 0 if limit < 0
61
- ids = redis.zrange(cachekey, offset, limit) # Using 'ids', even though keys are now terms
62
- if ids.size > 0
63
- results = redis.hmget(results_hashes_id, *ids)
64
- results = results.reject(&:nil?) # handle cached results for ids which have since been deleted
65
- results.map { |r| MultiJson.decode(r) }
66
- else
67
- []
68
- end
76
+ ids = redis.zrange(@cachekey, offset, limit) # Using 'ids', even though keys are now terms
77
+ matching_hashes(ids)
69
78
  end
70
79
  end
71
80
  end
@@ -1,3 +1,3 @@
1
1
  module Soulheart
2
- VERSION = '0.0.10'
2
+ VERSION = '0.0.11'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: soulheart
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Seth Herr