picky-client 1.1.7 → 1.2.0
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.
@@ -1,16 +1,52 @@
|
|
1
|
+
# Ruby Search Client Frontend for the Picky search engine.
|
2
|
+
#
|
3
|
+
# === Usage
|
4
|
+
#
|
5
|
+
# ==== 1. Set up search clients.
|
6
|
+
#
|
7
|
+
# # Create search client instances e.g. in your development.rb, production.rb etc.
|
8
|
+
# #
|
9
|
+
# # Use the right host, port where your Picky server runs. Then, choose a URL path as defined
|
10
|
+
# # in your <tt>app/application.rb</tt> in the server.
|
11
|
+
# #
|
12
|
+
# FullBooks = Picky::Client::Full.new :host => 'localhost', :port => 8080, :path => '/books/full'
|
13
|
+
# LiveBooks = Picky::Client::Live.new :host => 'localhost', :port => 8080, :path => '/books/live'
|
14
|
+
#
|
15
|
+
# ==== 2. Get results.
|
16
|
+
#
|
17
|
+
# # Then, in your search methods, call #search.
|
18
|
+
# #
|
19
|
+
# # You will get back a Hash with categorized results.
|
20
|
+
# #
|
21
|
+
# results = FullBooks.search 'my query', :offset => 10
|
22
|
+
#
|
23
|
+
# ==== 3. Work with the results.
|
24
|
+
#
|
25
|
+
# # To make the Hash more useful, extend it with a few convenience methods.
|
26
|
+
# # See Picky::Convenience.
|
27
|
+
# #
|
28
|
+
# results.extend Picky::Convenience
|
29
|
+
#
|
30
|
+
# # One of the added Methods is:
|
31
|
+
# # populate_with(ModelThatSupportsFind, &optional_block_where_you_get_model_instance_and_you_can_render)
|
32
|
+
# # This adds the rendered models to the results Hash.
|
33
|
+
# #
|
34
|
+
# results.populate_with Book do |book|
|
35
|
+
# book.to_s
|
36
|
+
# end
|
37
|
+
#
|
38
|
+
# ==== 4. Last step is encoding it back into JSON.
|
39
|
+
#
|
40
|
+
# # Encode the results in JSON and return it to the Javascript Client (or your frontend).
|
41
|
+
# #
|
42
|
+
# ActiveSupport::JSON.encode results
|
43
|
+
#
|
44
|
+
# Note: The client might be rewritten such that instead of an http request it connects through tcp/0mq.
|
45
|
+
#
|
1
46
|
require 'net/http'
|
2
47
|
|
3
48
|
module Picky
|
4
|
-
|
5
|
-
#
|
6
|
-
# Configure a search by passing the options in the initializer:
|
7
|
-
# * host
|
8
|
-
# * port
|
9
|
-
# * path
|
10
|
-
#
|
11
|
-
# TODO Rewrite such that instead of an http request we connect through tcp.
|
12
|
-
# Or use EventMachine.
|
13
|
-
#
|
49
|
+
|
14
50
|
module Client
|
15
51
|
|
16
52
|
class Base
|
@@ -52,10 +88,10 @@ module Picky
|
|
52
88
|
#
|
53
89
|
# Returns a hash. Extend with Convenience.
|
54
90
|
#
|
55
|
-
def search params = {}
|
56
|
-
return {} unless
|
91
|
+
def search query, params = {}
|
92
|
+
return {} unless query && !query.empty?
|
57
93
|
|
58
|
-
send_search params
|
94
|
+
send_search params.merge :query => query
|
59
95
|
end
|
60
96
|
|
61
97
|
# Sends a search to the configured address.
|
@@ -1,5 +1,6 @@
|
|
1
1
|
module Picky
|
2
|
-
|
2
|
+
|
3
|
+
# Use this class to extend the hash that the client returns.
|
3
4
|
#
|
4
5
|
module Convenience
|
5
6
|
|
@@ -8,42 +9,59 @@ module Picky
|
|
8
9
|
def empty?
|
9
10
|
allocations.empty?
|
10
11
|
end
|
11
|
-
|
12
|
+
|
13
|
+
# Returns the topmost n results.
|
14
|
+
# (Note that not all ids are returned with the results. By default only maximally 20.)
|
15
|
+
#
|
16
|
+
# === Parameters
|
17
|
+
# * limit: The amount of ids to return. Default is 20.
|
12
18
|
#
|
13
19
|
def ids limit = 20
|
14
20
|
ids = []
|
15
21
|
allocations.each { |allocation| allocation[4].each { |id| break if ids.size > limit; ids << id } }
|
16
22
|
ids
|
17
23
|
end
|
18
|
-
|
24
|
+
|
25
|
+
# Removes all ids of each allocation.
|
19
26
|
#
|
20
27
|
def clear_ids
|
21
28
|
allocations.each { |allocation| allocation[4].clear }
|
22
29
|
end
|
23
30
|
|
24
|
-
#
|
31
|
+
# Returns the allocations.
|
25
32
|
#
|
26
33
|
def allocations
|
27
34
|
@allocations || @allocations = self[:allocations]
|
28
35
|
end
|
36
|
+
# Returns the number of allocations.
|
37
|
+
#
|
29
38
|
def allocations_size
|
30
39
|
@allocations_size || @allocations_size = allocations.size
|
31
40
|
end
|
41
|
+
# Returns the total of results.
|
42
|
+
#
|
32
43
|
def total
|
33
44
|
@total || @total = self[:total]
|
34
45
|
end
|
35
46
|
|
36
|
-
#
|
47
|
+
# Populates the ids with (rendered) model instances.
|
37
48
|
#
|
38
49
|
# Give it an AR class and options for the find and it
|
39
50
|
# will yield each found result for you to render.
|
40
51
|
#
|
41
52
|
# If you don't pass it a block, it will just use the AR results.
|
42
53
|
#
|
43
|
-
|
54
|
+
# === Parameters
|
55
|
+
# * model_class: The model to use for the results. Will call #find on the given class.
|
56
|
+
# * amount: Amount of results to populate. Default 20.
|
57
|
+
#
|
58
|
+
# === Options
|
59
|
+
# * options are directly passed through to the ModelClass.find(ids, options) method. Default is {}.
|
60
|
+
#
|
61
|
+
def populate_with model_class, amount = 20, options = {}, &block
|
44
62
|
the_ids = ids amount
|
45
63
|
|
46
|
-
objects =
|
64
|
+
objects = model_class.find the_ids, options
|
47
65
|
|
48
66
|
# Put together a mapping.
|
49
67
|
#
|
@@ -58,13 +76,18 @@ module Picky
|
|
58
76
|
|
59
77
|
objects.collect! &block if block_given?
|
60
78
|
|
61
|
-
|
79
|
+
amend_ids_with objects
|
62
80
|
clear_ids
|
63
81
|
|
64
82
|
objects
|
65
83
|
end
|
66
|
-
|
67
|
-
#
|
84
|
+
|
85
|
+
# Returns either
|
86
|
+
# * the rendered entries, if you have used #populate_with _with_ a block
|
87
|
+
# OR
|
88
|
+
# * the model instances, if you have used #populate_with _without_ a block
|
89
|
+
#
|
90
|
+
# Or, if you haven't called #populate_with yet, you will get an empty array.
|
68
91
|
#
|
69
92
|
def entries limit = 20
|
70
93
|
if block_given?
|
@@ -79,7 +102,7 @@ module Picky
|
|
79
102
|
|
80
103
|
# The ids need to come in the order which the ids were returned by the ids method.
|
81
104
|
#
|
82
|
-
def
|
105
|
+
def amend_ids_with entries # :nodoc:
|
83
106
|
i = 0
|
84
107
|
self.allocations.each do |allocation|
|
85
108
|
allocation[5] = allocation[4].map do |_|
|
data/lib/picky-client.rb
CHANGED
@@ -4,6 +4,6 @@ require 'rubygems'
|
|
4
4
|
require 'yajl'
|
5
5
|
|
6
6
|
dir = File.dirname __FILE__
|
7
|
-
require File.expand_path('picky-client/
|
7
|
+
require File.expand_path('picky-client/client', dir)
|
8
8
|
require File.expand_path('picky-client/convenience', dir)
|
9
9
|
require File.expand_path('picky-client/helper', dir)
|
@@ -104,15 +104,32 @@ describe Picky::Client do
|
|
104
104
|
end
|
105
105
|
|
106
106
|
describe "search" do
|
107
|
+
describe 'ok search term given' do
|
108
|
+
it 'calls send_search correctly' do
|
109
|
+
@full.should_receive(:send_search).once.with :query => 'hello'
|
110
|
+
|
111
|
+
@full.search 'hello'
|
112
|
+
end
|
113
|
+
end
|
114
|
+
describe 'no search term given' do
|
115
|
+
it "should raise an ArgumentError" do
|
116
|
+
lambda { @full.search }.should raise_error(ArgumentError)
|
117
|
+
end
|
118
|
+
end
|
107
119
|
describe "with nil as search term" do
|
108
120
|
before(:each) do
|
109
121
|
@query = nil
|
110
122
|
end
|
111
|
-
it "should return a Search::Results
|
112
|
-
@full.search(
|
123
|
+
it "should return a Search::Results" do
|
124
|
+
@full.search(@query).should be_kind_of(Hash)
|
113
125
|
end
|
114
126
|
it "should return an empty Search::Results" do
|
115
|
-
@full.search(
|
127
|
+
@full.search(@query).should be_empty
|
128
|
+
end
|
129
|
+
it 'calls send_search correctly' do
|
130
|
+
@full.should_receive(:send_search).never
|
131
|
+
|
132
|
+
@full.search @query
|
116
133
|
end
|
117
134
|
end
|
118
135
|
describe "with '' as search term" do
|
@@ -120,10 +137,15 @@ describe Picky::Client do
|
|
120
137
|
@query = ''
|
121
138
|
end
|
122
139
|
it "should return a Search::Results" do
|
123
|
-
@full.search(
|
140
|
+
@full.search(@query).should be_kind_of(Hash)
|
124
141
|
end
|
125
142
|
it "should return an empty Search::Results" do
|
126
|
-
@full.search(
|
143
|
+
@full.search(@query).should be_empty
|
144
|
+
end
|
145
|
+
it 'calls send_search correctly' do
|
146
|
+
@full.should_receive(:send_search).never
|
147
|
+
|
148
|
+
@full.search @query
|
127
149
|
end
|
128
150
|
end
|
129
151
|
end
|
@@ -170,15 +192,32 @@ describe Picky::Client do
|
|
170
192
|
end
|
171
193
|
|
172
194
|
describe "search" do
|
195
|
+
describe 'ok search term given' do
|
196
|
+
it 'calls send_search correctly' do
|
197
|
+
@live.should_receive(:send_search).once.with :query => 'hello'
|
198
|
+
|
199
|
+
@live.search 'hello'
|
200
|
+
end
|
201
|
+
end
|
202
|
+
describe 'no search term given' do
|
203
|
+
it "should raise an ArgumentError" do
|
204
|
+
lambda { @live.search }.should raise_error(ArgumentError)
|
205
|
+
end
|
206
|
+
end
|
173
207
|
describe "with nil as search term" do
|
174
208
|
before(:each) do
|
175
209
|
@query = nil
|
176
210
|
end
|
177
211
|
it "should return a Search::Results" do
|
178
|
-
@live.search(
|
212
|
+
@live.search(@query).should be_kind_of(Hash)
|
179
213
|
end
|
180
214
|
it "should return an empty Search::Results" do
|
181
|
-
@live.search(
|
215
|
+
@live.search(@query).should be_empty
|
216
|
+
end
|
217
|
+
it 'calls send_search correctly' do
|
218
|
+
@live.should_receive(:send_search).never
|
219
|
+
|
220
|
+
@live.search @query
|
182
221
|
end
|
183
222
|
end
|
184
223
|
describe "with '' as search term" do
|
@@ -186,10 +225,15 @@ describe Picky::Client do
|
|
186
225
|
@query = ''
|
187
226
|
end
|
188
227
|
it "should return a Search::Results" do
|
189
|
-
@live.search(
|
228
|
+
@live.search(@query).should be_kind_of(Hash)
|
190
229
|
end
|
191
230
|
it "should return an empty Search::Results" do
|
192
|
-
@live.search(
|
231
|
+
@live.search(@query).should be_empty
|
232
|
+
end
|
233
|
+
it 'calls send_search correctly' do
|
234
|
+
@live.should_receive(:send_search).never
|
235
|
+
|
236
|
+
@live.search @query
|
193
237
|
end
|
194
238
|
end
|
195
239
|
end
|
@@ -125,7 +125,7 @@ describe Picky::Convenience do
|
|
125
125
|
end
|
126
126
|
it 'should populate with the entries' do
|
127
127
|
new_ids = (11..31).to_a # +10
|
128
|
-
@results.
|
128
|
+
@results.amend_ids_with new_ids
|
129
129
|
@results.entries.should == (11..30).to_a
|
130
130
|
end
|
131
131
|
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 1
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 1.
|
7
|
+
- 2
|
8
|
+
- 0
|
9
|
+
version: 1.2.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Florian Hanke
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-12-
|
17
|
+
date: 2010-12-17 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -41,13 +41,13 @@ extensions: []
|
|
41
41
|
extra_rdoc_files:
|
42
42
|
- README.rdoc
|
43
43
|
files:
|
44
|
+
- lib/picky-client/client.rb
|
44
45
|
- lib/picky-client/convenience.rb
|
45
|
-
- lib/picky-client/engine.rb
|
46
46
|
- lib/picky-client/helper.rb
|
47
47
|
- lib/picky-client.rb
|
48
48
|
- README.rdoc
|
49
|
+
- spec/picky-client/client_spec.rb
|
49
50
|
- spec/picky-client/convenience_spec.rb
|
50
|
-
- spec/picky-client/engine_spec.rb
|
51
51
|
- spec/picky-client/helper_spec.rb
|
52
52
|
has_rdoc: false
|
53
53
|
homepage: http://floere.github.com/picky
|
@@ -82,6 +82,6 @@ signing_key:
|
|
82
82
|
specification_version: 3
|
83
83
|
summary: picky Ruby Search Engine Client
|
84
84
|
test_files:
|
85
|
+
- spec/picky-client/client_spec.rb
|
85
86
|
- spec/picky-client/convenience_spec.rb
|
86
|
-
- spec/picky-client/engine_spec.rb
|
87
87
|
- spec/picky-client/helper_spec.rb
|