solr_wrapper 0.0.7 → 0.0.8

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/.rubocop_todo.yml ADDED
File without changes
data/.travis.yml CHANGED
@@ -5,4 +5,4 @@ rvm:
5
5
  - jruby
6
6
  cache:
7
7
  directories:
8
- - /tmp/solr-5.0.0
8
+ - /tmp/solr-5.1.0
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in solr_wrapper.gemspec
4
- gemspec
4
+ gemspec
data/Rakefile CHANGED
@@ -1,8 +1,7 @@
1
1
  require "bundler/gem_tasks"
2
2
 
3
-
4
3
  require 'rspec/core/rake_task'
5
4
 
6
5
  RSpec::Core::RakeTask.new(:spec)
7
6
 
8
- task default: ['spec']
7
+ task default: ['spec']
data/lib/solr_wrapper.rb CHANGED
@@ -3,16 +3,16 @@ require 'solr_wrapper/instance'
3
3
 
4
4
  module SolrWrapper
5
5
  def self.default_solr_version
6
- "5.0.0"
6
+ "5.1.0"
7
7
  end
8
8
 
9
- def self.default_instance options = {}
9
+ def self.default_instance(options = {})
10
10
  @default_instance ||= SolrWrapper::Instance.new options
11
11
  end
12
12
 
13
13
  ##
14
14
  # Ensures a Solr service is running before executing the block
15
- def self.wrap options = {}, &block
15
+ def self.wrap(options = {}, &block)
16
16
  default_instance(options).wrap &block
17
17
  end
18
- end
18
+ end
@@ -21,14 +21,17 @@ module SolrWrapper
21
21
  # @option options [String] :instance_dir
22
22
  # @option options [String] :download_path
23
23
  # @option options [String] :md5sum
24
+ # @option options [String] :solr_xml
24
25
  # @option options [Boolean] :verbose
25
26
  # @option options [Boolean] :managed
26
27
  # @option options [Boolean] :ignore_md5sum
27
- def initialize options = {}
28
+ # @option options [Hash] :solr_options
29
+ # @option options [Hash] :env
30
+ def initialize(options = {})
28
31
  @options = options
29
32
  end
30
33
 
31
- def wrap &block
34
+ def wrap(&_block)
32
35
  start
33
36
  yield self
34
37
  ensure
@@ -52,7 +55,7 @@ module SolrWrapper
52
55
  ##
53
56
  # Stop Solr and wait for it to finish exiting
54
57
  def stop
55
- if managed? and started?
58
+ if managed? && started?
56
59
 
57
60
  exec('stop', p: port)
58
61
  # Wait for solr to stop
@@ -82,18 +85,18 @@ module SolrWrapper
82
85
  # @param [Hash] options
83
86
  # @option options [String] :name
84
87
  # @option options [String] :dir
85
- def create options = {}
88
+ def create(options = {})
86
89
  options[:name] ||= SecureRandom.hex
87
90
 
88
91
  exec("create", c: options[:name], d: options[:dir], p: port)
89
92
 
90
93
  options[:name]
91
94
  end
92
-
95
+
93
96
  ##
94
97
  # Create a new collection in solr
95
98
  # @param [String] name collection name
96
- def delete name, options = {}
99
+ def delete(name, _options = {})
97
100
  exec("delete", c: name, p: port)
98
101
  end
99
102
 
@@ -102,7 +105,7 @@ module SolrWrapper
102
105
  # @param [Hash] options
103
106
  # @option options [String] :name
104
107
  # @option options [String] :dir
105
- def with_collection options = {}
108
+ def with_collection(options = {})
106
109
  name = create(options)
107
110
  begin
108
111
  yield name
@@ -112,7 +115,7 @@ module SolrWrapper
112
115
  end
113
116
 
114
117
  ##
115
- # Get the port this Solr instance is running at
118
+ # Get the port this Solr instance is running at
116
119
  def port
117
120
  options.fetch(:port, "8983").to_s
118
121
  end
@@ -128,9 +131,17 @@ module SolrWrapper
128
131
  FileUtils.remove_entry(version_file) if File.exists? version_file
129
132
  end
130
133
 
134
+ ##
135
+ # Get a (likely) URL to the solr instance
136
+ def url
137
+ "http://127.0.0.1:#{port}/solr/"
138
+ end
139
+
131
140
  protected
141
+
142
+ # rubocop:disable Lint/RescueException
132
143
  def extract
133
- return solr_dir if File.exists?(solr_binary) and extracted_version == version
144
+ return solr_dir if File.exists?(solr_binary) && extracted_version == version
134
145
 
135
146
  zip_path = download
136
147
 
@@ -138,8 +149,8 @@ module SolrWrapper
138
149
  Zip::File.open(zip_path) do |zip_file|
139
150
  # Handle entries one by one
140
151
  zip_file.each do |entry|
141
- dest_file = File.join(tmp_save_dir,entry.name)
142
- FileUtils.remove_entry(dest_file,true)
152
+ dest_file = File.join(tmp_save_dir, entry.name)
153
+ FileUtils.remove_entry(dest_file, true)
143
154
  entry.extract(dest_file)
144
155
  end
145
156
  end
@@ -148,34 +159,37 @@ module SolrWrapper
148
159
  abort "Unable to unzip #{zip_path} into #{tmp_save_dir}: #{e.message}"
149
160
  end
150
161
 
151
- begin
152
- FileUtils.remove_dir(solr_dir,true)
153
- FileUtils.cp_r File.join(tmp_save_dir, File.basename(default_url, ".zip")), solr_dir
162
+ begin
163
+ FileUtils.remove_dir(solr_dir, true)
164
+ FileUtils.cp_r File.join(tmp_save_dir, File.basename(download_url, ".zip")), solr_dir
154
165
  self.extracted_version = version
155
166
  FileUtils.chmod 0755, solr_binary
156
167
  rescue Exception => e
157
168
  abort "Unable to copy #{tmp_save_dir} to #{solr_dir}: #{e.message}"
158
169
  end
159
170
 
171
+ configure
172
+
160
173
  solr_dir
161
174
  ensure
162
175
  FileUtils.remove_entry tmp_save_dir if File.exists? tmp_save_dir
163
176
  end
177
+ # rubocop:enable Lint/RescueException
164
178
 
165
179
  def download
166
- unless File.exists? download_path and validate? download_path
167
- fetch_with_progressbar url, download_path
180
+ unless File.exists?(download_path) && validate?(download_path)
181
+ fetch_with_progressbar download_url, download_path
168
182
  validate! download_path
169
183
  end
170
184
 
171
185
  download_path
172
186
  end
173
187
 
174
- def validate? file
188
+ def validate?(file)
175
189
  Digest::MD5.file(file).hexdigest == expected_md5sum
176
190
  end
177
191
 
178
- def validate! file
192
+ def validate!(file)
179
193
  unless validate? file
180
194
  raise "MD5 mismatch" unless options[:ignore_md5sum]
181
195
  end
@@ -184,18 +198,19 @@ module SolrWrapper
184
198
  ##
185
199
  # Run a bin/solr command
186
200
  # @see https://github.com/apache/lucene-solr/blob/trunk/solr/bin/solr
187
- def exec cmd, options = {}
201
+ def exec(cmd, options = {})
188
202
  output = !!options.delete(:output)
189
- args = [solr_binary, cmd] + options.map { |k,v| ["-#{k}", "#{v}"] }.flatten
203
+ args = [solr_binary, cmd] + solr_options.merge(options).map { |k, v| ["-#{k}", "#{v}"] }.flatten
190
204
 
191
205
  if IO.respond_to? :popen4
192
206
  # JRuby
193
- pid, input, output, error = IO.popen4(args.join(" "))
194
-
207
+ env_str = env.map { |k, v| "#{Shellwords.escape(k)}=#{Shellwords.escape(v)}" }.join(" ")
208
+ _, input, output, error = IO.popen4(env_str + " " + args.join(" "))
209
+
195
210
  stringio = StringIO.new
196
- if verbose? and !output
197
- IO.copy_stream(output,$stderr)
198
- IO.copy_stream(error,$stderr)
211
+ if verbose? && !output
212
+ IO.copy_stream(output, $stderr)
213
+ IO.copy_stream(error, $stderr)
199
214
  else
200
215
  IO.copy_stream(output, stringio)
201
216
  IO.copy_stream(error, stringio)
@@ -203,7 +218,7 @@ module SolrWrapper
203
218
  input.close
204
219
  output.close
205
220
  error.close
206
-
221
+
207
222
  stringio.rewind
208
223
 
209
224
  if $? != 0
@@ -212,10 +227,10 @@ module SolrWrapper
212
227
 
213
228
  stringio
214
229
  else
215
- IO.popen(args + [err: [:child, :out]]) do |io|
230
+ IO.popen(env, args + [err: [:child, :out]]) do |io|
216
231
  stringio = StringIO.new
217
- if verbose? and !output
218
- IO.copy_stream(io,$stderr)
232
+ if verbose? && !output
233
+ IO.copy_stream(io, $stderr)
219
234
  else
220
235
  IO.copy_stream(io, stringio)
221
236
  end
@@ -232,12 +247,13 @@ module SolrWrapper
232
247
  end
233
248
  end
234
249
 
235
- private
236
- def url
237
- @download_url ||= options.fetch(:url, default_url)
250
+ private
251
+
252
+ def download_url
253
+ @download_url ||= options.fetch(:url, default_download_url)
238
254
  end
239
255
 
240
- def default_url
256
+ def default_download_url
241
257
  @default_url ||= begin
242
258
  mirror_url = "http://www.apache.org/dyn/closer.cgi/lucene/solr/#{version}/solr-#{version}.zip?asjson=true"
243
259
  json = open(mirror_url).read
@@ -254,20 +270,28 @@ module SolrWrapper
254
270
  @version ||= options.fetch(:version, default_solr_version)
255
271
  end
256
272
 
273
+ def solr_options
274
+ options.fetch(:solr_options, {})
275
+ end
276
+
277
+ def env
278
+ options.fetch(:env, {})
279
+ end
280
+
257
281
  def default_solr_version
258
282
  SolrWrapper.default_solr_version
259
283
  end
260
284
 
261
285
  def download_path
262
- @download_path ||= options.fetch(:download_path, default_download_path)
286
+ @download_path ||= options.fetch(:download_path, default_download_path)
263
287
  end
264
288
 
265
289
  def default_download_path
266
- File.join(Dir.tmpdir, File.basename(default_url))
290
+ File.join(Dir.tmpdir, File.basename(download_url))
267
291
  end
268
292
 
269
293
  def solr_dir
270
- @solr_dir ||= options.fetch(:instance_dir, File.join(Dir.tmpdir, File.basename(default_url, ".zip")))
294
+ @solr_dir ||= options.fetch(:instance_dir, File.join(Dir.tmpdir, File.basename(download_url, ".zip")))
271
295
  end
272
296
 
273
297
  def verbose?
@@ -285,7 +309,7 @@ module SolrWrapper
285
309
  def expected_md5sum
286
310
  @md5sum ||= options.fetch(:md5sum, open(md5file).read.split(" ").first)
287
311
  end
288
-
312
+
289
313
  def solr_binary
290
314
  File.join(solr_dir, "bin", "solr")
291
315
  end
@@ -298,17 +322,17 @@ module SolrWrapper
298
322
  @tmp_save_dir ||= Dir.mktmpdir
299
323
  end
300
324
 
301
- def fetch_with_progressbar url, output
325
+ def fetch_with_progressbar(url, output)
302
326
  pbar = ProgressBar.create(title: File.basename(url), total: nil, format: "%t: |%B| %p%% (%e )")
303
- open(url, content_length_proc: lambda {|t|
327
+ open(url, content_length_proc: lambda do|t|
304
328
  if t && 0 < t
305
329
  pbar.total = t
306
330
  end
307
- },
308
- progress_proc: lambda {|s|
309
- pbar.progress = s
310
- }) do |io|
311
- IO.copy_stream(io,output)
331
+ end,
332
+ progress_proc: lambda do|s|
333
+ pbar.progress = s
334
+ end) do |io|
335
+ IO.copy_stream(io, output)
312
336
  end
313
337
  end
314
338
 
@@ -324,10 +348,14 @@ module SolrWrapper
324
348
  File.read(version_file).strip if File.exists? version_file
325
349
  end
326
350
 
327
- def extracted_version= version
351
+ def extracted_version=(version)
328
352
  File.open(version_file, "w") do |f|
329
353
  f.puts version
330
354
  end
331
355
  end
356
+
357
+ def configure
358
+ FileUtils.cp options[:solr_xml], File.join(solr_dir, "server", "solr", "solr.xml") if options[:solr_xml]
359
+ end
332
360
  end
333
- end
361
+ end
@@ -1,3 +1,3 @@
1
1
  module SolrWrapper
2
- VERSION = "0.0.7"
3
- end
2
+ VERSION = "0.0.8"
3
+ end
@@ -16,4 +16,4 @@ describe SolrWrapper::Instance do
16
16
  end
17
17
  end
18
18
  end
19
- end
19
+ end
@@ -4,12 +4,12 @@ describe SolrWrapper do
4
4
  describe ".wrap" do
5
5
  it "should launch solr" do
6
6
  SolrWrapper.wrap do |solr|
7
- expect {
7
+ expect do
8
8
  Timeout::timeout(15) do
9
9
  TCPSocket.new('127.0.0.1', solr.port).close
10
10
  end
11
- }.not_to raise_exception
11
+ end.not_to raise_exception
12
12
  end
13
13
  end
14
14
  end
15
- end
15
+ end
data/spec/spec_helper.rb CHANGED
@@ -8,6 +8,5 @@ require 'rspec'
8
8
 
9
9
  FIXTURES_DIR = File.expand_path("fixtures", File.dirname(__FILE__))
10
10
 
11
- RSpec.configure do |config|
12
-
13
- end
11
+ RSpec.configure do |_config|
12
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: solr_wrapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Beer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-25 00:00:00.000000000 Z
11
+ date: 2015-04-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubyzip
@@ -116,6 +116,9 @@ extensions: []
116
116
  extra_rdoc_files: []
117
117
  files:
118
118
  - ".gitignore"
119
+ - ".rubocop.yml"
120
+ - ".rubocop_hound.yml"
121
+ - ".rubocop_todo.yml"
119
122
  - ".travis.yml"
120
123
  - Gemfile
121
124
  - LICENSE