#!/usr/bin/env ruby $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib') begin require 'redis' rescue LoadError require 'rubygems' require 'redis' end require 'soulmate' require 'optparse' parser = OptionParser.new do |opts| opts.banner = "Usage: soulmate [options] COMMAND" opts.separator "" opts.separator "Options:" opts.on("-r", "--redis [HOST:PORT]", "Redis connection string") do |host| Soulmate.redis = host end opts.on("-s", "--stop-words [FILE]", "Path to file containing a list of stop words") do |fn| File.open(fn) do |file| Soulmate.stop_words = file.readlines.map{ |l| l.strip } end end opts.on("-h", "--help", "Show this message") do puts opts exit end opts.separator "" opts.separator "Commands:" opts.separator " load TYPE Replaces collection specified by TYPE with items read from stdin in the JSON lines format." opts.separator " add TYPE Adds items to collection specified by TYPE read from stdin in the JSON lines format." opts.separator " remove TYPE Removes items from collection specified by TYPE read from stdin in the JSON lines format. Items only require an 'id', all other fields are ignored." opts.separator " query TYPE QUERY Queries for items from collection specified by TYPE." end def load(type) puts "Loading items of type #{type}..." items = $stdin.read.split("\n").map { |l| MultiJson.decode(l) } loaded = Soulmate::Loader.new(type).load(items) puts "Loaded a total of #{loaded.size} items" end def add(type) puts "Adding items of type #{type}..." loader = Soulmate::Loader.new(type) items = $stdin.read.split("\n").map { |l| MultiJson.decode(l) } items.each do |item| loader.add(item) end puts "Loaded a total of #{items.size} items" end def remove(type) puts "Removing items of type #{type}..." loader = Soulmate::Loader.new(type) items = $stdin.read.split("\n").map { |l| MultiJson.decode(l) } items.each do |item| loader.remove(item) end puts "Removed a total of #{items.size} items" end def query(type, query) puts "> Querying '#{type}' for '#{query}'" matcher = Soulmate::Matcher.new(type) results = matcher.matches_for_term(query, :limit => 0) results.each do |item| puts MultiJson.encode(item) end puts "> Found #{results.size} matches" end parser.parse! case ARGV[0] when 'load' load ARGV[1] when 'add' add ARGV[1] when 'remove' remove ARGV[1] when 'query' query ARGV[1], ARGV[2] else puts parser.help end