picky 1.2.4 → 1.3.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.
- data/lib/picky/adapters/rack/base.rb +23 -0
- data/lib/picky/adapters/rack/live_parameters.rb +33 -0
- data/lib/picky/adapters/rack/query.rb +59 -0
- data/lib/picky/adapters/rack.rb +28 -0
- data/lib/picky/alias_instances.rb +2 -0
- data/lib/picky/application.rb +9 -8
- data/lib/picky/cli.rb +25 -3
- data/lib/picky/frontend_adapters/rack.rb +150 -0
- data/lib/picky/helpers/measuring.rb +0 -2
- data/lib/picky/index_api.rb +1 -1
- data/lib/picky/indexed/categories.rb +51 -14
- data/lib/picky/indexers/solr.rb +1 -5
- data/lib/picky/indexing/indexes.rb +6 -0
- data/lib/picky/interfaces/live_parameters.rb +165 -0
- data/lib/picky/loader.rb +13 -2
- data/lib/picky/query/base.rb +15 -18
- data/lib/picky/query/combination.rb +2 -2
- data/lib/picky/query/solr.rb +0 -17
- data/lib/picky/query/token.rb +14 -27
- data/lib/picky/query/weights.rb +13 -1
- data/lib/picky/results/base.rb +9 -2
- data/spec/lib/adapters/rack/base_spec.rb +24 -0
- data/spec/lib/adapters/rack/live_parameters_spec.rb +21 -0
- data/spec/lib/adapters/rack/query_spec.rb +33 -0
- data/spec/lib/application_spec.rb +27 -8
- data/spec/lib/cli_spec.rb +9 -0
- data/spec/lib/extensions/symbol_spec.rb +1 -3
- data/spec/lib/{routing_spec.rb → frontend_adapters/rack_spec.rb} +69 -66
- data/spec/lib/indexed/categories_spec.rb +24 -0
- data/spec/lib/interfaces/live_parameters_spec.rb +138 -0
- data/spec/lib/query/base_spec.rb +10 -14
- data/spec/lib/query/live_spec.rb +1 -30
- data/spec/lib/query/token_spec.rb +72 -5
- data/spec/lib/query/weights_spec.rb +59 -36
- data/spec/lib/results/base_spec.rb +13 -1
- metadata +20 -7
- data/lib/picky/routing.rb +0 -171
@@ -2,46 +2,69 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Query::Weights do
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
5
|
+
context 'with weights' do
|
6
|
+
before(:each) do
|
7
|
+
@weights = Query::Weights.new [:test1, :test2] => 6,
|
8
|
+
[:test1] => 5,
|
9
|
+
[:test3] => 3,
|
10
|
+
[:test3, :test2] => 4,
|
11
|
+
[:test1, :test4] => 5,
|
12
|
+
[:test4, :test1] => 5,
|
13
|
+
[:test4, :test1, :test2] => 4,
|
14
|
+
[:test1, :test4, :test2] => 4,
|
15
|
+
[:test4, :test5] => 3,
|
16
|
+
[:test5, :test1] => 2,
|
17
|
+
[:test1, :test5] => 2,
|
18
|
+
[:test3, :test1] => 2,
|
19
|
+
[:test1, :test3] => 2
|
20
|
+
end
|
20
21
|
|
21
|
-
|
22
|
-
|
23
|
-
|
22
|
+
describe "weight_for" do
|
23
|
+
it "should return zero if there is no specific weight" do
|
24
|
+
@weights.weight_for([:not_a_specific_allocation]).should be_zero
|
25
|
+
end
|
24
26
|
end
|
25
|
-
end
|
26
27
|
|
27
|
-
|
28
|
-
|
29
|
-
|
28
|
+
def self.it_should_return_a_specific_weight_for(allocation, weight)
|
29
|
+
it "should return weight #{weight} for #{allocation.inspect}" do
|
30
|
+
@weights.weight_for(allocation).should == weight
|
31
|
+
end
|
30
32
|
end
|
31
|
-
end
|
32
33
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
34
|
+
it_should_return_a_specific_weight_for [:test1, :test2], 6
|
35
|
+
it_should_return_a_specific_weight_for [:test1], 5
|
36
|
+
it_should_return_a_specific_weight_for [:test1, :test3], 2
|
37
|
+
it_should_return_a_specific_weight_for [:test3], 3
|
38
|
+
it_should_return_a_specific_weight_for [:test3, :test2], 4
|
39
|
+
it_should_return_a_specific_weight_for [:test1, :test4], 5
|
40
|
+
it_should_return_a_specific_weight_for [:test4, :test1], 5
|
41
|
+
it_should_return_a_specific_weight_for [:test4, :test1, :test2], 4
|
42
|
+
it_should_return_a_specific_weight_for [:test1, :test4, :test2], 4
|
43
|
+
it_should_return_a_specific_weight_for [:test4, :test5], 3
|
44
|
+
it_should_return_a_specific_weight_for [:test5, :test1], 2
|
45
|
+
it_should_return_a_specific_weight_for [:test1, :test5], 2
|
46
|
+
it_should_return_a_specific_weight_for [:test3, :test1], 2
|
47
|
+
|
48
|
+
describe 'to_s' do
|
49
|
+
it 'is correct' do
|
50
|
+
@weights.to_s.should == "{[:test1, :test2]=>6, [:test1]=>5, [:test3]=>3, [:test3, :test2]=>4, [:test1, :test4]=>5, [:test4, :test1]=>5, [:test4, :test1, :test2]=>4, [:test1, :test4, :test2]=>4, [:test4, :test5]=>3, [:test5, :test1]=>2, [:test1, :test5]=>2, [:test3, :test1]=>2, [:test1, :test3]=>2}"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
describe 'empty?' do
|
54
|
+
it 'is correct' do
|
55
|
+
@weights.empty?.should == false
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
context 'without weights' do
|
60
|
+
before(:each) do
|
61
|
+
@weights = Query::Weights.new
|
62
|
+
end
|
63
|
+
describe 'empty?' do
|
64
|
+
it 'is correct' do
|
65
|
+
@weights.empty?.should == true
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
46
69
|
|
47
70
|
end
|
@@ -1,6 +1,18 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe Results do
|
3
|
+
describe Results::Base do
|
4
|
+
|
5
|
+
describe "from" do
|
6
|
+
before(:each) do
|
7
|
+
@results = stub :results
|
8
|
+
Results::Base.stub! :new => @results
|
9
|
+
|
10
|
+
@results.stub! :prepare!
|
11
|
+
end
|
12
|
+
it "should generate a result" do
|
13
|
+
Results::Base.from(0, @allocations).should == @results
|
14
|
+
end
|
15
|
+
end
|
4
16
|
|
5
17
|
describe "ids" do
|
6
18
|
before(:each) do
|
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
|
+
- 3
|
8
|
+
- 0
|
9
|
+
version: 1.3.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:
|
17
|
+
date: 2011-01-27 00:00:00 +01:00
|
18
18
|
default_executable: picky
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -42,6 +42,10 @@ files:
|
|
42
42
|
- lib/bundling.rb
|
43
43
|
- lib/constants.rb
|
44
44
|
- lib/deployment.rb
|
45
|
+
- lib/picky/adapters/rack/base.rb
|
46
|
+
- lib/picky/adapters/rack/live_parameters.rb
|
47
|
+
- lib/picky/adapters/rack/query.rb
|
48
|
+
- lib/picky/adapters/rack.rb
|
45
49
|
- lib/picky/alias_instances.rb
|
46
50
|
- lib/picky/application.rb
|
47
51
|
- lib/picky/cacher/convenience.rb
|
@@ -73,6 +77,7 @@ files:
|
|
73
77
|
- lib/picky/extensions/module.rb
|
74
78
|
- lib/picky/extensions/object.rb
|
75
79
|
- lib/picky/extensions/symbol.rb
|
80
|
+
- lib/picky/frontend_adapters/rack.rb
|
76
81
|
- lib/picky/helpers/measuring.rb
|
77
82
|
- lib/picky/index/bundle.rb
|
78
83
|
- lib/picky/index/file/basic.rb
|
@@ -99,6 +104,7 @@ files:
|
|
99
104
|
- lib/picky/indexing/category.rb
|
100
105
|
- lib/picky/indexing/index.rb
|
101
106
|
- lib/picky/indexing/indexes.rb
|
107
|
+
- lib/picky/interfaces/live_parameters.rb
|
102
108
|
- lib/picky/loader.rb
|
103
109
|
- lib/picky/loggers/search.rb
|
104
110
|
- lib/picky/performant.rb
|
@@ -119,7 +125,6 @@ files:
|
|
119
125
|
- lib/picky/results/base.rb
|
120
126
|
- lib/picky/results/full.rb
|
121
127
|
- lib/picky/results/live.rb
|
122
|
-
- lib/picky/routing.rb
|
123
128
|
- lib/picky/signals.rb
|
124
129
|
- lib/picky/solr/schema_generator.rb
|
125
130
|
- lib/picky/sources/base.rb
|
@@ -146,6 +151,9 @@ files:
|
|
146
151
|
- lib/tasks/try.rake
|
147
152
|
- lib/picky/ext/ruby19/performant.c
|
148
153
|
- spec/ext/performant_spec.rb
|
154
|
+
- spec/lib/adapters/rack/base_spec.rb
|
155
|
+
- spec/lib/adapters/rack/live_parameters_spec.rb
|
156
|
+
- spec/lib/adapters/rack/query_spec.rb
|
149
157
|
- spec/lib/application_spec.rb
|
150
158
|
- spec/lib/cacher/cacher_strategy_spec.rb
|
151
159
|
- spec/lib/cacher/partial/default_spec.rb
|
@@ -167,6 +175,7 @@ files:
|
|
167
175
|
- spec/lib/extensions/module_spec.rb
|
168
176
|
- spec/lib/extensions/object_spec.rb
|
169
177
|
- spec/lib/extensions/symbol_spec.rb
|
178
|
+
- spec/lib/frontend_adapters/rack_spec.rb
|
170
179
|
- spec/lib/helpers/measuring_spec.rb
|
171
180
|
- spec/lib/index/bundle_spec.rb
|
172
181
|
- spec/lib/index/file/basic_spec.rb
|
@@ -188,6 +197,7 @@ files:
|
|
188
197
|
- spec/lib/indexing/category_spec.rb
|
189
198
|
- spec/lib/indexing/index_spec.rb
|
190
199
|
- spec/lib/indexing/indexes_spec.rb
|
200
|
+
- spec/lib/interfaces/live_parameters_spec.rb
|
191
201
|
- spec/lib/loader_spec.rb
|
192
202
|
- spec/lib/loggers/search_spec.rb
|
193
203
|
- spec/lib/query/allocation_spec.rb
|
@@ -205,7 +215,6 @@ files:
|
|
205
215
|
- spec/lib/rack/harakiri_spec.rb
|
206
216
|
- spec/lib/results/base_spec.rb
|
207
217
|
- spec/lib/results/live_spec.rb
|
208
|
-
- spec/lib/routing_spec.rb
|
209
218
|
- spec/lib/solr/schema_generator_spec.rb
|
210
219
|
- spec/lib/sources/couch_spec.rb
|
211
220
|
- spec/lib/sources/csv_spec.rb
|
@@ -252,6 +261,9 @@ specification_version: 3
|
|
252
261
|
summary: "Picky: Helps you find your data."
|
253
262
|
test_files:
|
254
263
|
- spec/ext/performant_spec.rb
|
264
|
+
- spec/lib/adapters/rack/base_spec.rb
|
265
|
+
- spec/lib/adapters/rack/live_parameters_spec.rb
|
266
|
+
- spec/lib/adapters/rack/query_spec.rb
|
255
267
|
- spec/lib/application_spec.rb
|
256
268
|
- spec/lib/cacher/cacher_strategy_spec.rb
|
257
269
|
- spec/lib/cacher/partial/default_spec.rb
|
@@ -273,6 +285,7 @@ test_files:
|
|
273
285
|
- spec/lib/extensions/module_spec.rb
|
274
286
|
- spec/lib/extensions/object_spec.rb
|
275
287
|
- spec/lib/extensions/symbol_spec.rb
|
288
|
+
- spec/lib/frontend_adapters/rack_spec.rb
|
276
289
|
- spec/lib/helpers/measuring_spec.rb
|
277
290
|
- spec/lib/index/bundle_spec.rb
|
278
291
|
- spec/lib/index/file/basic_spec.rb
|
@@ -294,6 +307,7 @@ test_files:
|
|
294
307
|
- spec/lib/indexing/category_spec.rb
|
295
308
|
- spec/lib/indexing/index_spec.rb
|
296
309
|
- spec/lib/indexing/indexes_spec.rb
|
310
|
+
- spec/lib/interfaces/live_parameters_spec.rb
|
297
311
|
- spec/lib/loader_spec.rb
|
298
312
|
- spec/lib/loggers/search_spec.rb
|
299
313
|
- spec/lib/query/allocation_spec.rb
|
@@ -311,7 +325,6 @@ test_files:
|
|
311
325
|
- spec/lib/rack/harakiri_spec.rb
|
312
326
|
- spec/lib/results/base_spec.rb
|
313
327
|
- spec/lib/results/live_spec.rb
|
314
|
-
- spec/lib/routing_spec.rb
|
315
328
|
- spec/lib/solr/schema_generator_spec.rb
|
316
329
|
- spec/lib/sources/couch_spec.rb
|
317
330
|
- spec/lib/sources/csv_spec.rb
|
data/lib/picky/routing.rb
DELETED
@@ -1,171 +0,0 @@
|
|
1
|
-
require 'rack/mount'
|
2
|
-
|
3
|
-
#
|
4
|
-
#
|
5
|
-
class Routing # :nodoc:all
|
6
|
-
|
7
|
-
@@defaults = {
|
8
|
-
query_key: 'query'.freeze,
|
9
|
-
offset_key: 'offset'.freeze,
|
10
|
-
content_type: 'application/octet-stream'.freeze
|
11
|
-
}
|
12
|
-
|
13
|
-
def initialize
|
14
|
-
@defaults = @@defaults.dup
|
15
|
-
end
|
16
|
-
|
17
|
-
#
|
18
|
-
#
|
19
|
-
def reset_routes
|
20
|
-
@routes = Rack::Mount::RouteSet.new
|
21
|
-
end
|
22
|
-
def routes
|
23
|
-
@routes || reset_routes
|
24
|
-
end
|
25
|
-
def freeze
|
26
|
-
routes.freeze
|
27
|
-
end
|
28
|
-
|
29
|
-
# Routing simply delegates to the route set to handle a request.
|
30
|
-
#
|
31
|
-
def call env
|
32
|
-
routes.call env
|
33
|
-
end
|
34
|
-
|
35
|
-
#
|
36
|
-
#
|
37
|
-
def route options = {}
|
38
|
-
mappings, route_options = split options
|
39
|
-
mappings.each do |url, query|
|
40
|
-
route_one url, query, route_options
|
41
|
-
end
|
42
|
-
end
|
43
|
-
def split options
|
44
|
-
mappings = {}
|
45
|
-
route_options = {}
|
46
|
-
options.each_pair do |key, value|
|
47
|
-
if Regexp === key or String === key
|
48
|
-
mappings[key] = value
|
49
|
-
else
|
50
|
-
route_options[key] = value
|
51
|
-
end
|
52
|
-
end
|
53
|
-
[mappings, route_options]
|
54
|
-
end
|
55
|
-
def route_one url, query, route_options = {}
|
56
|
-
raise TargetQueryNilError.new(url) unless query
|
57
|
-
query.tokenizer = @defaults[:tokenizer] if @defaults[:tokenizer]
|
58
|
-
routes.add_route generate_app(query, route_options), default_options(url, route_options)
|
59
|
-
end
|
60
|
-
class TargetQueryNilError < StandardError
|
61
|
-
def initialize url
|
62
|
-
@url = url
|
63
|
-
end
|
64
|
-
def to_s
|
65
|
-
"Routing for #{@url.inspect} was defined with a nil query object."
|
66
|
-
end
|
67
|
-
end
|
68
|
-
#
|
69
|
-
#
|
70
|
-
def root status
|
71
|
-
answer %r{^/$}, STATUSES[status]
|
72
|
-
end
|
73
|
-
#
|
74
|
-
#
|
75
|
-
def default status
|
76
|
-
answer nil, STATUSES[status]
|
77
|
-
end
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
# TODO Can Rack handle this for me?
|
82
|
-
#
|
83
|
-
# Note: Rack-mount already handles the 404.
|
84
|
-
#
|
85
|
-
STATUSES = {
|
86
|
-
200 => lambda { |_| [200, { 'Content-Type' => 'text/html', 'Content-Length' => '0' }, ['']] },
|
87
|
-
404 => lambda { |_| [404, { 'Content-Type' => 'text/html', 'Content-Length' => '0' }, ['']] }
|
88
|
-
}
|
89
|
-
|
90
|
-
#
|
91
|
-
#
|
92
|
-
def default_options url, route_options = {}
|
93
|
-
url = normalized url
|
94
|
-
|
95
|
-
options = { request_method: 'GET' }.merge route_options
|
96
|
-
|
97
|
-
options[:path_info] = url if url
|
98
|
-
|
99
|
-
options.delete :content_type # TODO
|
100
|
-
|
101
|
-
query_params = options.delete :query
|
102
|
-
options[:query_string] = %r{#{generate_query_string(query_params)}} if query_params
|
103
|
-
|
104
|
-
options
|
105
|
-
end
|
106
|
-
|
107
|
-
#
|
108
|
-
#
|
109
|
-
def generate_query_string query_params
|
110
|
-
raise "At least one query string condition is needed." if query_params.size.zero?
|
111
|
-
raise "Too many query param conditions (only 1 allowed): #{query_params}" if query_params.size > 1
|
112
|
-
k, v = query_params.first
|
113
|
-
"#{k}=#{v}"
|
114
|
-
end
|
115
|
-
|
116
|
-
# Generates a rack app for the given query.
|
117
|
-
#
|
118
|
-
def generate_app query, options = {}
|
119
|
-
query_key = options[:query_key] || @defaults[:query_key]
|
120
|
-
content_type = options[:content_type] || @defaults[:content_type]
|
121
|
-
lambda do |env|
|
122
|
-
params = Rack::Request.new(env).params
|
123
|
-
|
124
|
-
results = query.search_with_text *extracted(params)
|
125
|
-
|
126
|
-
PickyLog.log results.to_log(params[query_key]) # TODO Save the original query in the results object.
|
127
|
-
|
128
|
-
respond_with results.to_response, content_type
|
129
|
-
end
|
130
|
-
end
|
131
|
-
UTF8_STRING = 'UTF-8'.freeze
|
132
|
-
def extracted params
|
133
|
-
[
|
134
|
-
# query is encoded in ASCII
|
135
|
-
#
|
136
|
-
params[@defaults[:query_key]] && params[@defaults[:query_key]].force_encoding(UTF8_STRING),
|
137
|
-
params[@defaults[:offset_key]] && params[@defaults[:offset_key]].to_i || 0
|
138
|
-
]
|
139
|
-
end
|
140
|
-
def respond_with response, content_type
|
141
|
-
[200, { 'Content-Type' => content_type, 'Content-Length' => response.size.to_s, }, [response]]
|
142
|
-
end
|
143
|
-
|
144
|
-
# Setup a route that answers using the given app.
|
145
|
-
#
|
146
|
-
def answer url = nil, app = nil
|
147
|
-
routes.add_route (app || STATUSES[200]), default_options(url)
|
148
|
-
end
|
149
|
-
|
150
|
-
def normalized url
|
151
|
-
String === url ? %r{#{url}} : url
|
152
|
-
end
|
153
|
-
|
154
|
-
# Returns true if there are no routes defined.
|
155
|
-
#
|
156
|
-
def empty?
|
157
|
-
routes.length.zero?
|
158
|
-
end
|
159
|
-
|
160
|
-
# TODO Beautify.
|
161
|
-
#
|
162
|
-
def to_s
|
163
|
-
routes.instance_variable_get(:@routes).map do |route|
|
164
|
-
path_info = route.conditions[:path_info]
|
165
|
-
anchored = Rack::Mount::Utils.regexp_anchored?(path_info)
|
166
|
-
anchored_ok = anchored ? "\u2713" : " "
|
167
|
-
"#{anchored_ok} #{path_info.source}"
|
168
|
-
end.join "\n"
|
169
|
-
end
|
170
|
-
|
171
|
-
end
|