SearchString 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +10 -0
- data/Gemfile +4 -0
- data/README.md +21 -0
- data/Rakefile +2 -0
- data/SearchString.gemspec +21 -0
- data/lib/SearchString.rb +2 -0
- data/lib/SearchString/search_string.rb +51 -0
- data/lib/SearchString/string.rb +7 -0
- data/lib/SearchString/version.rb +3 -0
- data/spec/SearchString/search_string_spec.rb +37 -0
- data/spec/spec_helper.rb +4 -0
- metadata +79 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
SearchString
|
2
|
+
============
|
3
|
+
SearchString gives you the ability to use simple queries to search a string. Like
|
4
|
+
|
5
|
+
'Hello World'.search("hello -bye")
|
6
|
+
|
7
|
+
Searches and returns
|
8
|
+
|
9
|
+
require "rubygems"
|
10
|
+
require "SearchString"
|
11
|
+
|
12
|
+
string = 'Lorem ipsum dolor sit amet'
|
13
|
+
|
14
|
+
string.search('lorem sit') #=> true
|
15
|
+
string.search('lorem sit OR hello world') #=> true
|
16
|
+
string.search('lorem sit -ignore') #=> true
|
17
|
+
string.search('hello world') #=> false
|
18
|
+
string.search('lorem -ipsum') #=> false
|
19
|
+
string.search('lorem ipsum OR hello world -ipsum') #=> false
|
20
|
+
string.search('lorem world OR hello world -ipsum') #=> false
|
21
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "SearchString/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "SearchString"
|
7
|
+
s.version = Searchstring::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Thomas Krampl"]
|
10
|
+
s.email = ["post@d09.no"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{A simple query search for strings. Used to filter Tweets from a stream with Twitter-search like queries.}
|
13
|
+
s.description = %q{A simple query search for strings. Used to filter Tweets from a stream with Twitter-search like queries.}
|
14
|
+
|
15
|
+
#s.rubyforge_project = "SearchString"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
end
|
data/lib/SearchString.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# Makes it posible to use Twitter-like search syntax on a string
|
2
|
+
#
|
3
|
+
#
|
4
|
+
module SearchString
|
5
|
+
class Search
|
6
|
+
class << self
|
7
|
+
def match(string, pattern)
|
8
|
+
without = get_subtractions(pattern)
|
9
|
+
string.strip!
|
10
|
+
|
11
|
+
return true if (without.nil? or string.match(without).nil?) and scan_string(string, pattern)
|
12
|
+
false
|
13
|
+
end
|
14
|
+
private
|
15
|
+
def get_subtractions(pattern)
|
16
|
+
if pattern.scan(/\B-(\w+)\b/).size > 0
|
17
|
+
/(\b#{ pattern.scan(/\B-(\w+)\b/).join('\b|\b') }\b)/i
|
18
|
+
else
|
19
|
+
nil
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def scan_string(string, pattern)
|
24
|
+
patterns = pattern.gsub(/\B-(\w+)\b/, '').strip.split(' OR ')
|
25
|
+
|
26
|
+
response = false
|
27
|
+
|
28
|
+
patterns.each do |keywords|
|
29
|
+
result = string.scan(/#{keywords.gsub(' ', '|')}/i)
|
30
|
+
if check_result(keywords, result)
|
31
|
+
response = true
|
32
|
+
break
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
response
|
37
|
+
end
|
38
|
+
|
39
|
+
def check_result(keywords, result)
|
40
|
+
num_keywords = keywords.split(' ').uniq.length
|
41
|
+
|
42
|
+
num_keywords == result.uniq.length
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
|
48
|
+
def search(pattern)
|
49
|
+
Search.match(self, pattern)
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe SearchString do
|
4
|
+
context "when the query match the text it should return true" do
|
5
|
+
it "with a single word" do
|
6
|
+
"lorem ipsum dolor sit amet".search("lorem").should be_true
|
7
|
+
end
|
8
|
+
|
9
|
+
it "with two words" do
|
10
|
+
"lorem ipsum dolor sit amet".search("lorem dolor").should be_true
|
11
|
+
end
|
12
|
+
|
13
|
+
it "with OR words" do
|
14
|
+
"lorem ipsum dolor sit amet".search("lorem dolor OR super duper").should be_true
|
15
|
+
end
|
16
|
+
|
17
|
+
it "with -words" do
|
18
|
+
"lorem ipsum dolor sit amet".search("lorem dolor -super -duper").should be_true
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context "when the query doesnt match the text it should return true" do
|
23
|
+
it "with a single word" do
|
24
|
+
"lorem ipsum dolor sit amet".search("lorema").should be_false
|
25
|
+
end
|
26
|
+
|
27
|
+
it "with two words" do
|
28
|
+
"lorem ipsum dolor sit amet".search("lorema dolorb").should be_false
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context "when the query includes - it should return false if one of the keywords is present" do
|
33
|
+
it "with -words" do
|
34
|
+
"lorem ipsum dolor sit amet".search("-lorem -duper").should be_false
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: SearchString
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 17
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 3
|
9
|
+
- 1
|
10
|
+
version: 0.3.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Thomas Krampl
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-02-09 00:00:00 +01:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: A simple query search for strings. Used to filter Tweets from a stream with Twitter-search like queries.
|
23
|
+
email:
|
24
|
+
- post@d09.no
|
25
|
+
executables: []
|
26
|
+
|
27
|
+
extensions: []
|
28
|
+
|
29
|
+
extra_rdoc_files: []
|
30
|
+
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- README.md
|
35
|
+
- Rakefile
|
36
|
+
- SearchString.gemspec
|
37
|
+
- lib/SearchString.rb
|
38
|
+
- lib/SearchString/search_string.rb
|
39
|
+
- lib/SearchString/string.rb
|
40
|
+
- lib/SearchString/version.rb
|
41
|
+
- spec/SearchString/search_string_spec.rb
|
42
|
+
- spec/spec_helper.rb
|
43
|
+
has_rdoc: true
|
44
|
+
homepage: ""
|
45
|
+
licenses: []
|
46
|
+
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
hash: 3
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
version: "0"
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
hash: 3
|
67
|
+
segments:
|
68
|
+
- 0
|
69
|
+
version: "0"
|
70
|
+
requirements: []
|
71
|
+
|
72
|
+
rubyforge_project:
|
73
|
+
rubygems_version: 1.5.0
|
74
|
+
signing_key:
|
75
|
+
specification_version: 3
|
76
|
+
summary: A simple query search for strings. Used to filter Tweets from a stream with Twitter-search like queries.
|
77
|
+
test_files:
|
78
|
+
- spec/SearchString/search_string_spec.rb
|
79
|
+
- spec/spec_helper.rb
|