soulmate 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -6,7 +6,7 @@ group :development do
6
6
  gem 'jeweler', '~> 1.5'
7
7
  end
8
8
 
9
- gem 'redis', '>= 2.0'
9
+ gem 'redis', '>= 3.0'
10
10
  gem 'vegas', '>= 0.1.0'
11
11
  gem 'sinatra', '>= 1.0'
12
12
  gem 'multi_json', '>= 1.0'
@@ -110,6 +110,14 @@ Add this to gemfile:
110
110
 
111
111
  Then you can query soulmate at the /sm url, for example: http://localhost:3000/sm/search?types[]=venues&limit=6&term=kitten
112
112
 
113
+ You can also config your redis instance:
114
+
115
+ # config/initializers/soulmate.rb
116
+
117
+ Soulmate.redis = 'redis://127.0.0.1:6379/0'
118
+ # or you can asign an existing instance of Redis, Redis::Namespace, etc.
119
+ # Soulmate.redis = $redis
120
+
113
121
  ### Rendering an autocompleter
114
122
 
115
123
  Soulmate doesn't include any client-side code necessary to render an autocompleter, but Mitch Crowe put together a pretty cool looking jquery plugin designed for exactly that: <a href="https://github.com/mcrowe/soulmate.js">soulmate.js</a>.
@@ -1,45 +1,12 @@
1
- require 'uri'
2
1
  require 'multi_json'
3
- require 'redis'
4
2
 
5
3
  require 'soulmate/version'
6
4
  require 'soulmate/helpers'
7
5
  require 'soulmate/base'
8
6
  require 'soulmate/matcher'
9
7
  require 'soulmate/loader'
8
+ require 'soulmate/config'
10
9
 
11
10
  module Soulmate
12
-
13
- extend self
14
-
15
- MIN_COMPLETE = 2
16
- DEFAULT_STOP_WORDS = ["vs", "at", "the"]
17
-
18
- def redis=(url)
19
- @redis = nil
20
- @redis_url = url
21
- redis
22
- end
23
-
24
- def redis
25
- @redis ||= (
26
- url = URI(@redis_url || ENV["REDIS_URL"] || "redis://127.0.0.1:6379/0")
27
-
28
- ::Redis.new({
29
- :host => url.host,
30
- :port => url.port,
31
- :db => url.path[1..-1],
32
- :password => url.password
33
- })
34
- )
35
- end
36
-
37
- def stop_words
38
- @stop_words ||= DEFAULT_STOP_WORDS
39
- end
40
-
41
- def stop_words=(arr)
42
- @stop_words = Array(arr).flatten
43
- end
44
-
11
+ extend Config
45
12
  end
@@ -0,0 +1,52 @@
1
+ require 'uri'
2
+ require 'redis'
3
+
4
+ module Soulmate
5
+ module Config
6
+ DEFAULT_MIN_COMPLETE = 2
7
+ DEFAULT_STOP_WORDS = ["vs", "at", "the"]
8
+
9
+ attr_writer :min_complete
10
+
11
+ def min_complete
12
+ @min_complete ||= DEFAULT_MIN_COMPLETE
13
+ end
14
+
15
+ # Accepts:
16
+ # 1. A Redis URL String 'redis://host:port/db'
17
+ # 2. An existing instance of Redis, Redis::Namespace, etc.
18
+ def redis=(server)
19
+ if server.is_a?(String)
20
+ @redis = nil
21
+ @redis_url = server
22
+ else
23
+ @redis = server
24
+ end
25
+
26
+ redis
27
+ end
28
+
29
+ # Returns the current Redis connection. If none has been created, will
30
+ # create a new one.
31
+ def redis
32
+ @redis ||= (
33
+ url = URI(@redis_url || ENV["REDIS_URL"] || "redis://127.0.0.1:6379/0")
34
+
35
+ ::Redis.new({
36
+ :host => url.host,
37
+ :port => url.port,
38
+ :db => url.path[1..-1],
39
+ :password => url.password
40
+ })
41
+ )
42
+ end
43
+
44
+ def stop_words
45
+ @stop_words ||= DEFAULT_STOP_WORDS
46
+ end
47
+
48
+ def stop_words=(arr)
49
+ @stop_words = Array(arr).flatten
50
+ end
51
+ end
52
+ end
@@ -8,7 +8,7 @@ module Soulmate
8
8
  Soulmate.stop_words.include?(w)
9
9
  end
10
10
  words.map do |w|
11
- (MIN_COMPLETE-1..(w.length-1)).map{ |l| w[0..l] }
11
+ (Soulmate.min_complete-1..(w.length-1)).map{ |l| w[0..l] }
12
12
  end.flatten.uniq
13
13
  end
14
14
 
@@ -61,4 +61,4 @@ module Soulmate
61
61
  end
62
62
  end
63
63
  end
64
- end
64
+ end
@@ -6,14 +6,14 @@ module Soulmate
6
6
  options = { :limit => 5, :cache => true }.merge(options)
7
7
 
8
8
  words = normalize(term).split(' ').reject do |w|
9
- w.size < MIN_COMPLETE or Soulmate.stop_words.include?(w)
9
+ w.size < Soulmate.min_complete or Soulmate.stop_words.include?(w)
10
10
  end.sort
11
11
 
12
12
  return [] if words.empty?
13
13
 
14
14
  cachekey = "#{cachebase}:" + words.join('|')
15
15
 
16
- if !options[:cache] || !Soulmate.redis.exists(cachekey)
16
+ if !options[:cache] || !Soulmate.redis.exists(cachekey) || Soulmate.redis.exists(cachekey) == 0
17
17
  interkeys = words.map { |w| "#{base}:#{w}" }
18
18
  Soulmate.redis.zinterstore(cachekey, interkeys)
19
19
  Soulmate.redis.expire(cachekey, 10 * 60) # expire after 10 minutes
@@ -29,4 +29,4 @@ module Soulmate
29
29
  end
30
30
  end
31
31
  end
32
- end
32
+ end
@@ -1,7 +1,7 @@
1
1
  module Soulmate
2
2
  module Version
3
3
  MAJOR = 1
4
- MINOR = 0
4
+ MINOR = 1
5
5
  PATCH = 0
6
6
 
7
7
  STRING = [MAJOR, MINOR, PATCH].compact.join('.')
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "soulmate"
8
- s.version = "1.0.0"
8
+ s.version = "1.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Eric Waller"]
12
- s.date = "2013-02-04"
12
+ s.date = "2014-05-15"
13
13
  s.description = "Soulmate is a tool to help solve the common problem of developing a fast autocomplete feature. It uses Redis's sorted sets to build an index of partial words and corresponding top matches, and provides a simple sinatra app to query them. Soulmate finishes your sentences."
14
14
  s.email = "eric@seatgeek.com"
15
15
  s.executables = ["soulmate", "soulmate-web"]
@@ -27,6 +27,7 @@ Gem::Specification.new do |s|
27
27
  "bin/soulmate-web",
28
28
  "lib/soulmate.rb",
29
29
  "lib/soulmate/base.rb",
30
+ "lib/soulmate/config.rb",
30
31
  "lib/soulmate/helpers.rb",
31
32
  "lib/soulmate/loader.rb",
32
33
  "lib/soulmate/matcher.rb",
@@ -43,14 +44,14 @@ Gem::Specification.new do |s|
43
44
  s.homepage = "http://github.com/seatgeek/soulmate"
44
45
  s.licenses = ["MIT"]
45
46
  s.require_paths = ["lib"]
46
- s.rubygems_version = "1.8.24"
47
+ s.rubygems_version = "1.8.23.2"
47
48
  s.summary = "Redis-backed service for fast autocompleting - extracted from SeatGeek"
48
49
 
49
50
  if s.respond_to? :specification_version then
50
51
  s.specification_version = 3
51
52
 
52
53
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
53
- s.add_runtime_dependency(%q<redis>, [">= 2.0"])
54
+ s.add_runtime_dependency(%q<redis>, [">= 3.0"])
54
55
  s.add_runtime_dependency(%q<vegas>, [">= 0.1.0"])
55
56
  s.add_runtime_dependency(%q<sinatra>, [">= 1.0"])
56
57
  s.add_runtime_dependency(%q<multi_json>, [">= 1.0"])
@@ -58,7 +59,7 @@ Gem::Specification.new do |s|
58
59
  s.add_development_dependency(%q<bundler>, ["~> 1.0"])
59
60
  s.add_development_dependency(%q<jeweler>, ["~> 1.5"])
60
61
  else
61
- s.add_dependency(%q<redis>, [">= 2.0"])
62
+ s.add_dependency(%q<redis>, [">= 3.0"])
62
63
  s.add_dependency(%q<vegas>, [">= 0.1.0"])
63
64
  s.add_dependency(%q<sinatra>, [">= 1.0"])
64
65
  s.add_dependency(%q<multi_json>, [">= 1.0"])
@@ -67,7 +68,7 @@ Gem::Specification.new do |s|
67
68
  s.add_dependency(%q<jeweler>, ["~> 1.5"])
68
69
  end
69
70
  else
70
- s.add_dependency(%q<redis>, [">= 2.0"])
71
+ s.add_dependency(%q<redis>, [">= 3.0"])
71
72
  s.add_dependency(%q<vegas>, [">= 0.1.0"])
72
73
  s.add_dependency(%q<sinatra>, [">= 1.0"])
73
74
  s.add_dependency(%q<multi_json>, [">= 1.0"])
@@ -108,7 +108,9 @@ class TestSoulmate < Test::Unit::TestCase
108
108
  assert_equal ["te", "tes", "test", "testi", "testin", "th", "thi", "this"], loader.prefixes_for_phrase("testin' this")
109
109
  assert_equal ["te", "tes", "test"], loader.prefixes_for_phrase("test test")
110
110
  assert_equal ["so", "sou", "soul", "soulm", "soulma", "soulmat", "soulmate"], loader.prefixes_for_phrase("SoUlmATE")
111
-
112
111
  assert_equal ['测试', '测试中', '测试中文', 'te', 'tes', 'test'], loader.prefixes_for_phrase('测试中文 test')
112
+
113
+ Soulmate.min_complete = 4
114
+ assert_equal ['同华东生', '同华东生产', '同华东生产队', 'abcd', 'abcde'], loader.prefixes_for_phrase('同华东生产队 abcde')
113
115
  end
114
116
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: soulmate
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-04 00:00:00.000000000 Z
12
+ date: 2014-05-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: redis
@@ -18,7 +18,7 @@ dependencies:
18
18
  requirements:
19
19
  - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
- version: '2.0'
21
+ version: '3.0'
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
- version: '2.0'
29
+ version: '3.0'
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: vegas
32
32
  requirement: !ruby/object:Gem::Requirement
@@ -145,6 +145,7 @@ files:
145
145
  - bin/soulmate-web
146
146
  - lib/soulmate.rb
147
147
  - lib/soulmate/base.rb
148
+ - lib/soulmate/config.rb
148
149
  - lib/soulmate/helpers.rb
149
150
  - lib/soulmate/loader.rb
150
151
  - lib/soulmate/matcher.rb
@@ -172,7 +173,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
172
173
  version: '0'
173
174
  segments:
174
175
  - 0
175
- hash: 2119288237740455138
176
+ hash: 3570037129997399612
176
177
  required_rubygems_version: !ruby/object:Gem::Requirement
177
178
  none: false
178
179
  requirements:
@@ -181,7 +182,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
181
182
  version: '0'
182
183
  requirements: []
183
184
  rubyforge_project:
184
- rubygems_version: 1.8.24
185
+ rubygems_version: 1.8.23.2
185
186
  signing_key:
186
187
  specification_version: 3
187
188
  summary: Redis-backed service for fast autocompleting - extracted from SeatGeek