Permalink
Browse files

Supporting an optional geo_rank param

  • Loading branch information...
1 parent 716d9c9 commit 5726a5c43525e5d2c3cfcae0eef37a2519342792 @ravibhim committed Oct 13, 2011
Showing with 36 additions and 3 deletions.
  1. +3 −2 lib/soulmate/matcher.rb
  2. +1 −1 lib/soulmate/server.rb
  3. +32 −0 test/test_soulmate.rb
View
5 lib/soulmate/matcher.rb
@@ -5,7 +5,8 @@ module Soulmate
class Matcher < Base
def matches_for_term(term, options = {})
- options = { :limit => 5, :cache => true }.merge(options)
+ # Default limit of 0 means no limit.
+ options = { :limit => 0, :cache => true }.merge(options)
words = normalize(term).split(' ').reject do |w|
w.size < MIN_COMPLETE or STOP_WORDS.include?(w)
@@ -30,7 +31,7 @@ def matches_for_term(term, options = {})
search_point = ::Geokit::LatLng.new(options[:lat], options[:long])
matches.sort! {|a,b| ::Geokit::LatLng.new(a["lat"], a["long"]).distance_to(search_point) <=> ::Geokit::LatLng.new(b["lat"], b["long"]).distance_to(search_point) }
end
- matches
+ options[:geo_rank] ? matches.first(options[:geo_rank].to_i) : matches
else
[]
end
View
2 lib/soulmate/server.rb
@@ -28,7 +28,7 @@ class Server < Sinatra::Base
matcher = Matcher.new(type)
options = {
}
- results[type] = matcher.matches_for_term(term, :limit => params[:limit].to_i, :lat => params[:lat], :long => params[:long])
+ results[type] = matcher.matches_for_term(term, :limit => params[:limit].to_i, :lat => params[:lat], :long => params[:long], :geo_rank => params[:geo_rank].to_i)
end
MultiJson.encode({
View
32 test/test_soulmate.rb
@@ -60,6 +60,38 @@ def test_integration_ranks_values_via_geo_proximity
assert_equal 2.0, first_result['lat']
assert_equal 2.0, first_result['long']
end
+
+ def test_integration_geo_rank_option
+ items = []
+ venues = File.open(File.expand_path(File.dirname(__FILE__)) + '/samples/venues.json', "r")
+ venues.each_line do |venue|
+ items << MultiJson.decode(venue)
+ end
+
+ items_loaded = Soulmate::Loader.new('venues').load(items)
+ assert_equal 6, items_loaded.size
+
+ matcher = Soulmate::Matcher.new('venues')
+ results = matcher.matches_for_term('stad', :lat => 2.0, :long => 2.0, :geo_rank => '2')
+
+ assert_equal 2, results.size
+ end
+
+ def test_integration_geo_rank_unused
+ items = []
+ venues = File.open(File.expand_path(File.dirname(__FILE__)) + '/samples/venues.json', "r")
+ venues.each_line do |venue|
+ items << MultiJson.decode(venue)
+ end
+
+ items_loaded = Soulmate::Loader.new('venues').load(items)
+ assert_equal 6, items_loaded.size
+
+ matcher = Soulmate::Matcher.new('venues')
+ results = matcher.matches_for_term('stad', :lat => 2.0, :long => 2.0, :geo_rank => nil)
+
+ assert_equal 5, results.size # Because there are only 5 entries with stadium
+ end
def test_can_remove_items

0 comments on commit 5726a5c

Please sign in to comment.