lib/desi/index_manager.rb in desi-0.1.0 vs lib/desi/index_manager.rb in desi-0.2.0
- old
+ new
@@ -8,34 +8,39 @@
# Elastic Search cluster
class IndexManager
# Initializes a Desi::IndexManager instance
#
- # @param [#to_hash] opts Hash of extra opts
- # @option opts [#to_s] :host Host to manage indices for
- # (default: 'http://127.0.0.1:9200')
- # @option opts [Boolean] :verbose Whether to output the actions' result
- # on STDOUT
+ # @param [#to_hash] opts Hash of extra opts
#
+ # @option opts [#to_s] :host ('http://127.0.0.1:9200') Host to manage indices for
+ # @option opts [Boolean] :verbose (nil) Whether to output the actions' result
+ # on STDOUT
+ # @option opts [#new] :http_client_factory (Desi::HttpClient) HTTP transport class
+ # to use
+ #
+ # @note The +:http_client_factory+ should return an instance that responds
+ # to #get and #delete
# @return [undefined]
#
# @api public
def initialize(opts = {})
@host = opts.fetch(:host, 'http://127.0.0.1:9200')
@verbose = opts[:verbose]
- @client = Desi::HttpClient.new(@host)
+ @outputter = opts.fetch(:outputter, Kernel)
+ @client = opts.fetch(:http_client_factory, Desi::HttpClient).new(@host)
end
# List index names for the specified cluster
#
# You can restrict the list using a regular expression pattern. (The default
# pattern being +/.*/+, all releases will be returned if you do not
# specify anything.)
#
- # @param [#to_s] pattern Regexp pattern used to restrict the selection
- # @return [Array<String>] List of index names of the ES cluster
+ # @param [#to_s] pattern ('.*') Regexp pattern used to restrict the selection
+ # @return [Array<String>] List of index names of the ES cluster
#
# @note This method will also output its result on STDOUT if +@verbose+ is
# true
#
# @example List all indices whose name begins with "foo"
@@ -43,14 +48,14 @@
#
# @api public
def list(pattern = '.*')
pattern = Regexp.new(pattern || '.*')
- puts "Indices from host #{@client.uri} matching the pattern #{pattern.inspect}\n\n" if @verbose
+ @outputter.puts "Indices from host #{@host} matching the pattern #{pattern.inspect}\n\n" if @verbose
list = indices(pattern).sort
- list.each {|i| puts i } if @verbose
+ list.each {|i| @outputter.puts i } if @verbose
list
end
# Delete all indices matching the specified pattern
#
@@ -67,15 +72,15 @@
#
# @api public
def delete!(pattern)
warn "You must provide a pattern" and exit if pattern.nil?
- puts "The following indices from host #{@client.uri} are now deleted" if @verbose
+ @outputter.puts "The following indices from host #{@host} are now deleted" if @verbose
indices(Regexp.new(pattern)).each do |index|
@client.delete(index)
- puts " * #{index}" if @verbose
+ @outputter.puts " * #{index}" if @verbose
end
end
# Empty (remove all records) from indices matching the specified pattern
#
@@ -92,14 +97,14 @@
#
# @api public
def empty!(pattern)
warn "You must provide a pattern" and exit if pattern.nil?
- puts "The following indices from host #{@client.uri} are now emptied" if @verbose
+ @outputter.puts "The following indices from host #{@host} are now emptied" if @verbose
indices(Regexp.new(pattern)).each do |index|
@client.delete("#{index}/_query?q=*")
- puts " * #{index}" if @verbose
+ @outputter.puts " * #{index}" if @verbose
end
end
private