module GetUrl::Searcher

The Searcher deals with collecting all urls and searching through them.

Public Instance Methods

add_score(keyword, item, factor = 1) click to toggle source

Adds a score for the given keyword on the given item. The given factor gives extra weight to the score.

@param [String] keyword The keyword. @param [Hash] item The item. @param [FixNum] factor The weighing factor.

# File lib/geturl/geturl-searcher.rb, line 105
def add_score(keyword, item, factor = 1)
  score = 0
  begin
    score += get_field_score(item['name'], keyword, 50, 25)
    score += get_field_score(item['url'], keyword, 50, 25)
    score += get_field_score(item['description'], keyword, 30, 2)
    item['tags'].to_a.each {|tag|
      score += get_field_score(tag, keyword, 30, 20)
    }
  rescue
  end
  item['score'] ||= 0
  item['scored_keywords'] ||= 0
  if score > 0
    item['score'] += score * factor
    item['scored_keywords'] += 1
  end
end
all(ids = nil) click to toggle source

Returns all urls of the given ids. If ids is empty, all urls of all sources will be loaded.

@param [Array] ids The array of source ids. @return [Array] The array of url items.

# File lib/geturl/geturl-searcher.rb, line 48
def all(ids = nil)
  load_sources(ids)
  return @items
end
get_field_score(value, keyword, equal_score, contains_score) click to toggle source

Returns the relevant score:

  • If the given value and keyword are equal, the given equal_score is returned.

  • Else if the given keyword is a substring of value, the contains_score is returned.

  • Otherwise, 0 is returned.

@param [String] value The value. @param [String] keyword The keyword. @param [Integer] equal_score The score when value is equal to the keyword. @param [Integer] contains_score The score when value contains the keyword. @return [Integer]

# File lib/geturl/geturl-searcher.rb, line 135
def get_field_score(value, keyword, equal_score, contains_score)
  v = value.downcase
  k = keyword.downcase
  return equal_score if (v == k)
  return contains_score if v.include?(k)
  return 0
end
load_source(id) click to toggle source

Loads the urls of the source with the given id. @param [String] id The id

# File lib/geturl/geturl-searcher.rb, line 16
def load_source(id)
  file = FileManager.get_local_source_file(id)
  source_items = FileManager.load_items_from_yaml_file(file)
  source_items.each {|item|
    item['source'] = id
    @items << item
  }
end
load_sources(ids = nil) click to toggle source

Loads all the given source ids. If ids is empty, all sources will be loaded. @param [Array] ids An array of source ids.

# File lib/geturl/geturl-searcher.rb, line 27
def load_sources(ids = nil)
  @items.clear

  all_sources = (ids.nil? || ids.empty?)
  if (all_sources) && (FileManager.all_items_cache_exist?)
    @items = FileManager.get_all_items_from_cache
    return
  end

  ids = Sources.get_sources.keys + ['local'] if ids.nil? || ids.empty?
  load_source(LocalUrls.private_source_id) if ids.include?('local')
  Sources.get_sources.keys.each {|id|
    load_source(id) if ids.include?(id)
  }
  FileManager.cache_all_items(@items) if all_sources
end