freelancing-god-riddle 0.9.8.1371.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENCE +20 -0
- data/README +85 -0
- data/lib/riddle/client/filter.rb +44 -0
- data/lib/riddle/client/message.rb +65 -0
- data/lib/riddle/client/response.rb +84 -0
- data/lib/riddle/client.rb +639 -0
- data/lib/riddle.rb +30 -0
- data/spec/functional/excerpt_spec.rb +102 -0
- data/spec/functional/keywords_spec.rb +40 -0
- data/spec/functional/search_spec.rb +69 -0
- data/spec/functional/update_spec.rb +41 -0
- data/spec/unit/client_spec.rb +194 -0
- data/spec/unit/filter_spec.rb +33 -0
- data/spec/unit/message_spec.rb +63 -0
- data/spec/unit/response_spec.rb +64 -0
- metadata +70 -0
@@ -0,0 +1,102 @@
|
|
1
|
+
require 'spec/spec_helper'
|
2
|
+
|
3
|
+
describe "Sphinx Excepts" do
|
4
|
+
before :each do
|
5
|
+
@client = Riddle::Client.new("localhost", 3313)
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should highlight a single word multiple times in a document" do
|
9
|
+
@client.excerpts(
|
10
|
+
:index => "people",
|
11
|
+
:words => "Mary",
|
12
|
+
:docs => ["Mary, Mary, quite contrary."]
|
13
|
+
).should == [
|
14
|
+
'<span class="match">Mary</span>, <span class="match">Mary</span>, quite contrary.'
|
15
|
+
]
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should use specified word markers" do
|
19
|
+
@client.excerpts(
|
20
|
+
:index => "people",
|
21
|
+
:words => "Mary",
|
22
|
+
:docs => ["Mary, Mary, quite contrary."],
|
23
|
+
:before_match => "<em>",
|
24
|
+
:after_match => "</em>"
|
25
|
+
).should == [
|
26
|
+
"<em>Mary</em>, <em>Mary</em>, quite contrary."
|
27
|
+
]
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should separate matches that are far apart by an ellipsis by default" do
|
31
|
+
@client.excerpts(
|
32
|
+
:index => "people",
|
33
|
+
:words => "Pat",
|
34
|
+
:docs => [
|
35
|
+
<<-SENTENCE
|
36
|
+
This is a really long sentence written by Pat. It has to be over 256
|
37
|
+
characters long, between keywords. But what is the keyword? Well, I
|
38
|
+
can't tell you just yet... wait patiently until we've hit the 256 mark.
|
39
|
+
It'll take a bit longer than you think. We're probably just hitting the
|
40
|
+
200 mark at this point. But I think we've now arrived - so I can tell
|
41
|
+
you what the keyword is. I bet you're really interested in finding out,
|
42
|
+
yeah? Excerpts are particularly riveting. This keyword, however, is
|
43
|
+
not. It's just my name: Pat.
|
44
|
+
SENTENCE
|
45
|
+
],
|
46
|
+
:before_match => "<em>",
|
47
|
+
:after_match => "</em>"
|
48
|
+
).should == [
|
49
|
+
<<-SENTENCE
|
50
|
+
This is a really long sentence written by <em>Pat</em>. It has to be over 256
|
51
|
+
characters long, between keywords. But what is the keyword? … interested in finding out,
|
52
|
+
yeah? Excerpts are particularly riveting. This keyword, however, is
|
53
|
+
not. It's just my name: <em>Pat</em>.
|
54
|
+
SENTENCE
|
55
|
+
]
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should use the provided separator" do
|
59
|
+
@client.excerpts(
|
60
|
+
:index => "people",
|
61
|
+
:words => "Pat",
|
62
|
+
:docs => [
|
63
|
+
<<-SENTENCE
|
64
|
+
This is a really long sentence written by Pat. It has to be over 256
|
65
|
+
characters long, between keywords. But what is the keyword? Well, I
|
66
|
+
can't tell you just yet... wait patiently until we've hit the 256 mark.
|
67
|
+
It'll take a bit longer than you think. We're probably just hitting the
|
68
|
+
200 mark at this point. But I think we've now arrived - so I can tell
|
69
|
+
you what the keyword is. I bet you're really interested in finding out,
|
70
|
+
yeah? Excerpts are particularly riveting. This keyword, however, is
|
71
|
+
not. It's just my name: Pat.
|
72
|
+
SENTENCE
|
73
|
+
],
|
74
|
+
:before_match => "<em>",
|
75
|
+
:after_match => "</em>",
|
76
|
+
:chunk_separator => " --- "
|
77
|
+
).should == [
|
78
|
+
<<-SENTENCE
|
79
|
+
This is a really long sentence written by <em>Pat</em>. It has to be over 256
|
80
|
+
characters long, between keywords. But what is the keyword? --- interested in finding out,
|
81
|
+
yeah? Excerpts are particularly riveting. This keyword, however, is
|
82
|
+
not. It's just my name: <em>Pat</em>.
|
83
|
+
SENTENCE
|
84
|
+
]
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should return multiple results for multiple documents" do
|
88
|
+
@client.excerpts(
|
89
|
+
:index => "people",
|
90
|
+
:words => "Mary",
|
91
|
+
:docs => [
|
92
|
+
"Mary, Mary, quite contrary.",
|
93
|
+
"The epithet \"Bloody Mary\" is associated with a number of historical and fictional women, most notably Queen Mary I of England"
|
94
|
+
],
|
95
|
+
:before_match => "<em>",
|
96
|
+
:after_match => "</em>"
|
97
|
+
).should == [
|
98
|
+
"<em>Mary</em>, <em>Mary</em>, quite contrary.",
|
99
|
+
"The epithet \"Bloody <em>Mary</em>\" is associated with a number of historical and fictional women, most notably Queen <em>Mary</em> I of England"
|
100
|
+
]
|
101
|
+
end
|
102
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'spec/spec_helper'
|
2
|
+
|
3
|
+
describe "Sphinx Keywords" do
|
4
|
+
before :each do
|
5
|
+
@client = Riddle::Client.new("localhost", 3313)
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should return an array of hashes" do
|
9
|
+
results = @client.keywords("pat", "people")
|
10
|
+
results.should be_kind_of(Array)
|
11
|
+
|
12
|
+
results.each do |result|
|
13
|
+
result.should be_kind_of(Hash)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should have keys for normalised and tokenised versions of the keywords" do
|
18
|
+
results = @client.keywords("pat", "people")
|
19
|
+
results.each do |result|
|
20
|
+
result.keys.should include(:normalised)
|
21
|
+
result.keys.should include(:tokenised)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
it "shouldn't have docs or hits keys if not requested" do
|
26
|
+
results = @client.keywords("pat", "people")
|
27
|
+
results.each do |result|
|
28
|
+
result.keys.should_not include(:docs)
|
29
|
+
result.keys.should_not include(:hits)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should have docs and hits keys if requested" do
|
34
|
+
results = @client.keywords("pat", "people", true)
|
35
|
+
results.each do |result|
|
36
|
+
result.keys.should include(:docs)
|
37
|
+
result.keys.should include(:hits)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'spec/spec_helper'
|
2
|
+
|
3
|
+
describe "Sphinx Searches" do
|
4
|
+
before :each do
|
5
|
+
@client = Riddle::Client.new("localhost", 3313)
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should return a single hash if a single query" do
|
9
|
+
@client.query("smith").should be_kind_of(Hash)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should return an array of hashs if multiple queries are run" do
|
13
|
+
@client.append_query "smith"
|
14
|
+
@client.append_query "jones"
|
15
|
+
results = @client.run
|
16
|
+
results.should be_kind_of(Array)
|
17
|
+
results.each { |result| result.should be_kind_of(Hash) }
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should return an array of matches" do
|
21
|
+
matches = @client.query("smith")[:matches]
|
22
|
+
matches.should be_kind_of(Array)
|
23
|
+
matches.each { |match| match.should be_kind_of(Hash) }
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should return an array of string fields" do
|
27
|
+
fields = @client.query("smith")[:fields]
|
28
|
+
fields.should be_kind_of(Array)
|
29
|
+
fields.each { |field| field.should be_kind_of(String) }
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should return an array of attribute names" do
|
33
|
+
attributes = @client.query("smith")[:attribute_names]
|
34
|
+
attributes.should be_kind_of(Array)
|
35
|
+
attributes.each { |a| a.should be_kind_of(String) }
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should return a hash of attributes" do
|
39
|
+
attributes = @client.query("smith")[:attributes]
|
40
|
+
attributes.should be_kind_of(Hash)
|
41
|
+
attributes.each do |key,value|
|
42
|
+
key.should be_kind_of(String)
|
43
|
+
value.should be_kind_of(Integer)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should return the total number of results returned" do
|
48
|
+
@client.query("smith")[:total].should be_kind_of(Integer)
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should return the total number of results available" do
|
52
|
+
@client.query("smith")[:total_found].should be_kind_of(Integer)
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should return the time taken for the query as a float" do
|
56
|
+
@client.query("smith")[:time].should be_kind_of(Float)
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should return a hash of the words from the query, with the number of documents and the number of hits" do
|
60
|
+
words = @client.query("smith victoria")[:words]
|
61
|
+
words.should be_kind_of(Hash)
|
62
|
+
words.each do |word,hash|
|
63
|
+
word.should be_kind_of(String)
|
64
|
+
hash.should be_kind_of(Hash)
|
65
|
+
hash[:docs].should be_kind_of(Integer)
|
66
|
+
hash[:hits].should be_kind_of(Integer)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'spec/spec_helper'
|
2
|
+
|
3
|
+
describe "Sphinx Updates" do
|
4
|
+
before :each do
|
5
|
+
@client = Riddle::Client.new("localhost", 3313)
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should update a single record appropriately" do
|
9
|
+
# check existing birthday
|
10
|
+
result = @client.query("Ellie K Ford")
|
11
|
+
result[:matches].should_not be_empty
|
12
|
+
result[:matches].length.should == 1
|
13
|
+
ellie = result[:matches].first
|
14
|
+
ellie[:attributes]["birthday"].should == Time.local(1970, 1, 23).to_i
|
15
|
+
|
16
|
+
# make Ellie younger by 6 years
|
17
|
+
@client.update("people", ["birthday"], {ellie[:doc] => [Time.local(1976, 1, 23).to_i]})
|
18
|
+
|
19
|
+
# check attribute's value
|
20
|
+
result = @client.query("Ellie K Ford")
|
21
|
+
result[:matches].should_not be_empty
|
22
|
+
result[:matches].length.should == 1
|
23
|
+
ellie = result[:matches].first
|
24
|
+
ellie[:attributes]["birthday"].should == Time.local(1976, 1, 23).to_i
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should update multiple records appropriately" do
|
28
|
+
result = @client.query("Steele")
|
29
|
+
pairs = {}
|
30
|
+
result[:matches].each do |match|
|
31
|
+
pairs[match[:doc]] = [match[:attributes]["birthday"] + (365*24*60*60)]
|
32
|
+
end
|
33
|
+
|
34
|
+
@client.update "people", ["birthday"], pairs
|
35
|
+
|
36
|
+
result = @client.query("Steele")
|
37
|
+
result[:matches].each do |match|
|
38
|
+
match[:attributes]["birthday"].should == pairs[match[:doc]].first
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,194 @@
|
|
1
|
+
require 'spec/spec_helper'
|
2
|
+
|
3
|
+
describe Riddle::Client do
|
4
|
+
it "should have the same keys for both commands and versions" do
|
5
|
+
Riddle::Client::Commands.keys.should == Riddle::Client::Versions.keys
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should default to localhost as the server" do
|
9
|
+
Riddle::Client.new.server.should == "localhost"
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should default to port 3312" do
|
13
|
+
Riddle::Client.new.port.should == 3312
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should translate anchor arguments correctly" do
|
17
|
+
client = Riddle::Client.new
|
18
|
+
client.set_anchor "latitude", 10.0, "longitude", 95.0
|
19
|
+
client.anchor.should == {
|
20
|
+
:latitude_attribute => "latitude",
|
21
|
+
:latitude => 10.0,
|
22
|
+
:longitude_attribute => "longitude",
|
23
|
+
:longitude => 95.0
|
24
|
+
}
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should add queries to the queue" do
|
28
|
+
client = Riddle::Client.new
|
29
|
+
client.queue.should be_empty
|
30
|
+
client.append_query "spec"
|
31
|
+
client.queue.should_not be_empty
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should build a basic search message correctly" do
|
35
|
+
client = Riddle::Client.new
|
36
|
+
client.append_query "test "
|
37
|
+
client.queue.first.should == query_contents(:simple)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should build a message with a specified index correctly" do
|
41
|
+
client = Riddle::Client.new
|
42
|
+
client.append_query "test ", "edition"
|
43
|
+
client.queue.first.should == query_contents(:index)
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should build a message using match mode :any correctly" do
|
47
|
+
client = Riddle::Client.new
|
48
|
+
client.match_mode = :any
|
49
|
+
client.append_query "test this "
|
50
|
+
client.queue.first.should == query_contents(:any)
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should build a message using sort by correctly" do
|
54
|
+
client = Riddle::Client.new
|
55
|
+
client.sort_by = 'id'
|
56
|
+
client.sort_mode = :extended
|
57
|
+
client.append_query "testing "
|
58
|
+
client.queue.first.should == query_contents(:sort)
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should build a message using match mode :boolean correctly" do
|
62
|
+
client = Riddle::Client.new
|
63
|
+
client.match_mode = :boolean
|
64
|
+
client.append_query "test "
|
65
|
+
client.queue.first.should == query_contents(:boolean)
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should build a message using match mode :phrase correctly" do
|
69
|
+
client = Riddle::Client.new
|
70
|
+
client.match_mode = :phrase
|
71
|
+
client.append_query "testing this "
|
72
|
+
client.queue.first.should == query_contents(:phrase)
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should build a message with a filter correctly" do
|
76
|
+
client = Riddle::Client.new
|
77
|
+
client.filters << Riddle::Client::Filter.new("id", [10, 100, 1000])
|
78
|
+
client.append_query "test "
|
79
|
+
client.queue.first.should == query_contents(:filter)
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should build a message with group values correctly" do
|
83
|
+
client = Riddle::Client.new
|
84
|
+
client.group_by = "id"
|
85
|
+
client.group_function = :attr
|
86
|
+
client.group_clause = "id"
|
87
|
+
client.append_query "test "
|
88
|
+
client.queue.first.should == query_contents(:group)
|
89
|
+
end
|
90
|
+
|
91
|
+
it "should build a message with group distinct value correctly" do
|
92
|
+
client = Riddle::Client.new
|
93
|
+
client.group_distinct = "id"
|
94
|
+
client.append_query "test "
|
95
|
+
client.queue.first.should == query_contents(:distinct)
|
96
|
+
end
|
97
|
+
|
98
|
+
it "should build a message with weights correctly" do
|
99
|
+
client = Riddle::Client.new
|
100
|
+
client.weights = [100, 1]
|
101
|
+
client.append_query "test "
|
102
|
+
client.queue.first.should == query_contents(:weights)
|
103
|
+
end
|
104
|
+
|
105
|
+
it "should build a message with an anchor correctly" do
|
106
|
+
client = Riddle::Client.new
|
107
|
+
client.set_anchor "latitude", 10.0, "longitude", 95.0
|
108
|
+
client.append_query "test "
|
109
|
+
client.queue.first.should == query_contents(:anchor)
|
110
|
+
end
|
111
|
+
|
112
|
+
it "should build a message with index weights correctly" do
|
113
|
+
client = Riddle::Client.new
|
114
|
+
client.index_weights = {"people" => 101}
|
115
|
+
client.append_query "test "
|
116
|
+
client.queue.first.should == query_contents(:index_weights)
|
117
|
+
end
|
118
|
+
|
119
|
+
it "should build a message with field weights correctly" do
|
120
|
+
client = Riddle::Client.new
|
121
|
+
client.field_weights = {"city" => 101}
|
122
|
+
client.append_query "test "
|
123
|
+
client.queue.first.should == query_contents(:field_weights)
|
124
|
+
end
|
125
|
+
|
126
|
+
it "should build a message with acomment correctly" do
|
127
|
+
client = Riddle::Client.new
|
128
|
+
client.append_query "test ", "*", "commenting"
|
129
|
+
client.queue.first.should == query_contents(:comment)
|
130
|
+
end
|
131
|
+
|
132
|
+
it "should keep multiple messages in the queue" do
|
133
|
+
client = Riddle::Client.new
|
134
|
+
client.weights = [100, 1]
|
135
|
+
client.append_query "test "
|
136
|
+
client.append_query "test "
|
137
|
+
client.queue.length.should == 2
|
138
|
+
client.queue.each { |item| item.should == query_contents(:weights) }
|
139
|
+
end
|
140
|
+
|
141
|
+
it "should keep multiple messages in the queue with different params" do
|
142
|
+
client = Riddle::Client.new
|
143
|
+
client.weights = [100, 1]
|
144
|
+
client.append_query "test "
|
145
|
+
client.weights = []
|
146
|
+
client.append_query "test ", "edition"
|
147
|
+
client.queue.first.should == query_contents(:weights)
|
148
|
+
client.queue.last.should == query_contents(:index)
|
149
|
+
end
|
150
|
+
|
151
|
+
it "should build a basic update message correctly" do
|
152
|
+
client = Riddle::Client.new
|
153
|
+
client.send(
|
154
|
+
:update_message,
|
155
|
+
"people",
|
156
|
+
["birthday"],
|
157
|
+
{1 => [191163600]}
|
158
|
+
).should == query_contents(:update_simple)
|
159
|
+
end
|
160
|
+
|
161
|
+
it "should build a keywords request without hits correctly" do
|
162
|
+
client = Riddle::Client.new
|
163
|
+
client.send(
|
164
|
+
:keywords_message,
|
165
|
+
"pat",
|
166
|
+
"people",
|
167
|
+
false
|
168
|
+
).should == query_contents(:keywords_without_hits)
|
169
|
+
end
|
170
|
+
|
171
|
+
it "should build a keywords request with hits correctly" do
|
172
|
+
client = Riddle::Client.new
|
173
|
+
client.send(
|
174
|
+
:keywords_message,
|
175
|
+
"pat",
|
176
|
+
"people",
|
177
|
+
true
|
178
|
+
).should == query_contents(:keywords_with_hits)
|
179
|
+
end
|
180
|
+
|
181
|
+
it "should timeout after a specified time" do
|
182
|
+
client = Riddle::Client.new
|
183
|
+
client.port = 3314
|
184
|
+
client.timeout = 1
|
185
|
+
|
186
|
+
server = TCPServer.new "localhost", 3314
|
187
|
+
|
188
|
+
lambda {
|
189
|
+
client.send(:connect) { |socket| }
|
190
|
+
}.should raise_error(Riddle::ConnectionError)
|
191
|
+
|
192
|
+
server.close
|
193
|
+
end
|
194
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'spec/spec_helper'
|
2
|
+
|
3
|
+
describe Riddle::Client::Filter do
|
4
|
+
it "should render a filter that uses an array of ints correctly" do
|
5
|
+
filter = Riddle::Client::Filter.new("field", [1, 2, 3])
|
6
|
+
filter.query_message.should == query_contents(:filter_array)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should render a filter that has exclude set correctly" do
|
10
|
+
filter = Riddle::Client::Filter.new("field", [1, 2, 3], true)
|
11
|
+
filter.query_message.should == query_contents(:filter_array_exclude)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should render a filter that is a range of ints correctly" do
|
15
|
+
filter = Riddle::Client::Filter.new("field", 1..3)
|
16
|
+
filter.query_message.should == query_contents(:filter_range)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should render a filter that is a range of ints as exclude correctly" do
|
20
|
+
filter = Riddle::Client::Filter.new("field", 1..3, true)
|
21
|
+
filter.query_message.should == query_contents(:filter_range_exclude)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should render a filter that is a range of floats correctly" do
|
25
|
+
filter = Riddle::Client::Filter.new("field", 5.4..13.5)
|
26
|
+
filter.query_message.should == query_contents(:filter_floats)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should render a filter that is a range of floats as exclude correctly" do
|
30
|
+
filter = Riddle::Client::Filter.new("field", 5.4..13.5, true)
|
31
|
+
filter.query_message.should == query_contents(:filter_floats_exclude)
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'spec/spec_helper'
|
2
|
+
|
3
|
+
describe Riddle::Client::Message do
|
4
|
+
it "should start with an empty string" do
|
5
|
+
Riddle::Client::Message.new.to_s.should == ""
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should append raw data correctly" do
|
9
|
+
data = [1, 2, 3].pack('NNN')
|
10
|
+
message = Riddle::Client::Message.new
|
11
|
+
message.append data
|
12
|
+
message.to_s.should == data
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should append strings correctly - with length first" do
|
16
|
+
str = "something to test with"
|
17
|
+
message = Riddle::Client::Message.new
|
18
|
+
message.append_string str
|
19
|
+
message.to_s.should == [str.length].pack('N') + str
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should append integers correctly - packed with N" do
|
23
|
+
message = Riddle::Client::Message.new
|
24
|
+
message.append_int 234
|
25
|
+
message.to_s.should == [234].pack('N')
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should append floats correctly - packed with f" do
|
29
|
+
message = Riddle::Client::Message.new
|
30
|
+
message.append_float 1.4
|
31
|
+
message.to_s.should == [1.4].pack('f').unpack('L*').pack('N')
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should append a collection of integers correctly" do
|
35
|
+
message = Riddle::Client::Message.new
|
36
|
+
message.append_ints 1, 2, 3, 4
|
37
|
+
message.to_s.should == [1, 2, 3, 4].pack('NNNN')
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should append a collection of floats correctly" do
|
41
|
+
message = Riddle::Client::Message.new
|
42
|
+
message.append_floats 1.0, 1.1, 1.2, 1.3
|
43
|
+
message.to_s.should == [1.0, 1.1, 1.2, 1.3].pack('ffff').unpack('L*L*L*L*').pack('NNNN')
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should append an array of strings correctly" do
|
47
|
+
arr = ["a", "bb", "ccc"]
|
48
|
+
message = Riddle::Client::Message.new
|
49
|
+
message.append_array arr
|
50
|
+
message.to_s.should == [3, 1].pack('NN') + "a" + [2].pack('N') + "bb" +
|
51
|
+
[3].pack('N') + "ccc"
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should append a variety of objects correctly" do
|
55
|
+
message = Riddle::Client::Message.new
|
56
|
+
message.append_int 4
|
57
|
+
message.append_string "test"
|
58
|
+
message.append_array ["one", "two"]
|
59
|
+
message.append_floats 1.5, 1.7
|
60
|
+
message.to_s.should == [4, 4].pack('NN') + "test" + [2, 3].pack('NN') +
|
61
|
+
"one" + [3].pack('N') + "two" + [1.5, 1.7].pack('ff').unpack('L*L*').pack('NN')
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'spec/spec_helper'
|
2
|
+
|
3
|
+
describe Riddle::Client::Response do
|
4
|
+
it "should interpret an integer correctly" do
|
5
|
+
Riddle::Client::Response.new([42].pack('N')).next_int.should == 42
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should interpret a string correctly" do
|
9
|
+
str = "this is a string"
|
10
|
+
Riddle::Client::Response.new(
|
11
|
+
[str.length].pack('N') + str
|
12
|
+
).next.should == str
|
13
|
+
end
|
14
|
+
|
15
|
+
# Comparing floats with decimal places doesn't seem to be exact
|
16
|
+
it "should interpret a float correctly" do
|
17
|
+
Riddle::Client::Response.new([1.0].pack('f').unpack('L*').pack('N')).next_float.should == 1.0
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should interpret an array of strings correctly" do
|
21
|
+
arr = ["a", "b", "c", "d"]
|
22
|
+
Riddle::Client::Response.new(
|
23
|
+
[arr.length].pack('N') + arr.collect { |str|
|
24
|
+
[str.length].pack('N') + str
|
25
|
+
}.join("")
|
26
|
+
).next_array.should == arr
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should interpret an array of ints correctly" do
|
30
|
+
arr = [1, 2, 3, 4]
|
31
|
+
Riddle::Client::Response.new(
|
32
|
+
[arr.length].pack('N') + arr.collect { |int|
|
33
|
+
[int].pack('N')
|
34
|
+
}.join("")
|
35
|
+
).next_int_array.should == arr
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should reflect the length of the incoming data correctly" do
|
39
|
+
data = [1, 2, 3, 4].pack('NNNN')
|
40
|
+
Riddle::Client::Response.new(data).length.should == data.length
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should handle a combination of strings and ints correctly" do
|
44
|
+
data = [1, 3, 5, 1].pack('NNNN') + 'a' + [2, 4].pack('NN') + 'test'
|
45
|
+
response = Riddle::Client::Response.new(data)
|
46
|
+
response.next_int.should == 1
|
47
|
+
response.next_int.should == 3
|
48
|
+
response.next_int.should == 5
|
49
|
+
response.next.should == 'a'
|
50
|
+
response.next_int.should == 2
|
51
|
+
response.next.should == 'test'
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should handle a combination of strings, ints, floats and string arrays correctly" do
|
55
|
+
data = [1, 2, 2].pack('NNN') + 'aa' + [2].pack('N') + 'bb' + [4].pack('N') +
|
56
|
+
"word" + [7].pack('f').unpack('L*').pack('N') + [3, 2, 2, 2].pack('NNNN')
|
57
|
+
response = Riddle::Client::Response.new(data)
|
58
|
+
response.next_int.should == 1
|
59
|
+
response.next_array.should == ['aa', 'bb']
|
60
|
+
response.next.should == "word"
|
61
|
+
response.next_float.should == 7
|
62
|
+
response.next_int_array.should == [2, 2, 2]
|
63
|
+
end
|
64
|
+
end
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: freelancing-god-riddle
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.8.1371.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Pat Allan
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-08-13 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: API for Sphinx, written in and for Ruby.
|
17
|
+
email: pat@freelancing-gods.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- lib/riddle/client/filter.rb
|
26
|
+
- lib/riddle/client/message.rb
|
27
|
+
- lib/riddle/client/response.rb
|
28
|
+
- lib/riddle/client.rb
|
29
|
+
- lib/riddle.rb
|
30
|
+
- MIT-LICENCE
|
31
|
+
- README
|
32
|
+
has_rdoc: true
|
33
|
+
homepage: http://riddle.freelancing-gods.com
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options:
|
36
|
+
- --title
|
37
|
+
- Riddle -- Ruby Sphinx Client
|
38
|
+
- --main
|
39
|
+
- Riddle::Client
|
40
|
+
- --line-numbers
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
version:
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
requirements: []
|
56
|
+
|
57
|
+
rubyforge_project: riddle
|
58
|
+
rubygems_version: 1.2.0
|
59
|
+
signing_key:
|
60
|
+
specification_version: 2
|
61
|
+
summary: API for Sphinx, written in and for Ruby.
|
62
|
+
test_files:
|
63
|
+
- spec/functional/excerpt_spec.rb
|
64
|
+
- spec/functional/keywords_spec.rb
|
65
|
+
- spec/functional/search_spec.rb
|
66
|
+
- spec/functional/update_spec.rb
|
67
|
+
- spec/unit/client_spec.rb
|
68
|
+
- spec/unit/filter_spec.rb
|
69
|
+
- spec/unit/message_spec.rb
|
70
|
+
- spec/unit/response_spec.rb
|