class Soulheart::Matcher

Attributes

opts[RW]

Public Class Methods

default_params_hash() click to toggle source
# File lib/soulheart/matcher.rb, line 10
def self.default_params_hash
  {
    'page' => 1,
    'per_page' => 5,
    'categories' => [],
    'q' => '', # Query
    'cache' => true
  }
end
new(params = {}) click to toggle source
# File lib/soulheart/matcher.rb, line 4
def initialize(params = {})
  set_clean_opts(params)
end

Public Instance Methods

cache_id_from_opts() click to toggle source
# File lib/soulheart/matcher.rb, line 50
def cache_id_from_opts
  "#{cache_id(categories_string)}#{@opts['q'].join(':')}"
end
cache_it_because() click to toggle source
# File lib/soulheart/matcher.rb, line 59
def cache_it_because
  redis.zinterstore(@cachekey, interkeys_from_opts)
  redis.expire(@cachekey, cache_duration) # cache_duration is set in base.rb
end
categories_string() click to toggle source
# File lib/soulheart/matcher.rb, line 42
def categories_string
  @opts['categories'].empty? ? 'all' : @opts['categories'].join('')
end
category_id_from_opts() click to toggle source
# File lib/soulheart/matcher.rb, line 46
def category_id_from_opts
  category_id(categories_string)
end
clean_opts() click to toggle source
# 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)
  # .reject{ |i| i && i.length > 0 } .split(' ').reject{  Soulmate.stop_words.include?(w) }
  @opts
end
interkeys_from_opts() click to toggle source
# File lib/soulheart/matcher.rb, line 54
def interkeys_from_opts
  # If there isn't a query, we use a special key in redis
  @opts['q'].empty? ? [no_query_id(@cid)] : @opts['q'].map { |w| "#{@cid}#{w}" }
end
matches() click to toggle source
# 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
  terms = redis.zrange(@cachekey, offset, limit)
  matching_hashes(terms)      
end
matching_hashes(terms) click to toggle source
# File lib/soulheart/matcher.rb, line 64
def matching_hashes(terms)
  return [] unless terms.size > 0
  results = redis.hmget(results_hashes_id, *terms)
  results = results.reject(&:nil?) # handle cached results for terms which have since been deleted
  results.map { |r| JSON.parse(r) }
end
set_clean_opts(params) click to toggle source
# 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) click to toggle source
# 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