Module: Elasticsearch::API::Utils

Extended by:
Utils
Included in:
Utils
Defined in:
lib/elasticsearch/api/utils.rb

Overview

Generic utility methods

Instance Method Summary collapse

Instance Method Details

#__bulkify(payload) ⇒ Object

Convert an array of payloads into Elasticsearch ‘headerndata` format

Supports various different formats of the payload: Array of Strings, Header/Data pairs, or the conveniency “combined” format where data is passed along with the header in a single item.

Elasticsearch::API::Utils.__bulkify [
  { :index =>  { :_index => 'myindexA', :_type => 'mytype', :_id => '1', :data => { :title => 'Test' } } },
  { :update => { :_index => 'myindexB', :_type => 'mytype', :_id => '2', :data => { :doc => { :title => 'Update' } } } }
]

# => {"index":{"_index":"myindexA","_type":"mytype","_id":"1"}}
# => {"title":"Test"}
# => {"update":{"_index":"myindexB","_type":"mytype","_id":"2"}}
# => {"doc":{"title":"Update"}}


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/elasticsearch/api/utils.rb', line 82

def __bulkify(payload)
  operations = %w[index create delete update]

  case

  # Hashes with `:data`
  when payload.any? { |d| d.is_a?(Hash) && d.values.first.is_a?(Hash) && operations.include?(d.keys.first.to_s) && (d.values.first[:data] || d.values.first['data']) }
    payload = payload.
      inject([]) do |sum, item|
        operation, meta = item.to_a.first
        meta            = meta.clone
        data            = meta.delete(:data) || meta.delete('data')

        sum << { operation => meta }
        sum << data if data
        sum
      end.
      map { |item| Elasticsearch::API.serializer.dump(item) }
    payload << '' unless payload.empty?

  # Array of strings
  when payload.all? { |d| d.is_a? String }
    payload << ''

  # Header/Data pairs
  else
    payload = payload.map { |item| Elasticsearch::API.serializer.dump(item) }
    payload << ''
  end

  payload = payload.join("\n")
end

#__escape(string) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

URL-escape a string

Examples:

__escape('foo/bar') # => 'foo%2Fbar'
__escape('bar^bam') # => 'bar%5Ebam'


15
16
17
18
# File 'lib/elasticsearch/api/utils.rb', line 15

def __escape(string)
  return string if string == '*'
  defined?(EscapeUtils) ? EscapeUtils.escape_url(string.to_s) : CGI.escape(string.to_s)
end

#__extract_params(arguments, params = [], options = {}) ⇒ Object



158
159
160
161
162
163
# File 'lib/elasticsearch/api/utils.rb', line 158

def __extract_params(arguments, params=[], options={})
  result = arguments.select { |k,v| COMMON_QUERY_PARAMS.include?(k) || params.include?(k) }
  result = Hash[result] unless result.is_a?(Hash) # Normalize Ruby 1.8 and Ruby 1.9 Hash#select behaviour
  result = Hash[result.map { |k,v| v.is_a?(Array) ? [k, __listify(v, options)] : [k,v]  }] # Listify Arrays
  result
end

#__extract_parts(arguments, valid_parts = []) ⇒ Array<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

Mutates the ‘arguments` argument, to prevent failures in `__validate_and_extract_params`.

Extracts the valid parts of the URL from the arguments

Examples:

Extract parts

__extract_parts { :foo => true }, [:foo, :bar]
# => [:foo]

Parameters:

  • arguments (Hash)

    Hash of arguments to verify and extract, **with symbolized keys**

  • valid_parts (Array<Symbol>) (defaults to: [])

    An array of symbol with valid keys

Returns:

  • (Array<String>)

    Valid parts of the URL as an array of strings



181
182
183
184
185
186
187
# File 'lib/elasticsearch/api/utils.rb', line 181

def __extract_parts(arguments, valid_parts=[])
  parts = Hash[arguments.select { |k,v| valid_parts.include?(k) }]
  parts = parts.reduce([]) { |sum, item| k, v = item; v.is_a?(TrueClass) ? sum << k.to_s : sum << v  }

  arguments.delete_if { |k,v| valid_parts.include? k }
  return parts
end

#__listify(*list) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a “list” of values from arguments, ignoring nil values and encoding special characters.

Examples:

Create a list from array

__listify(['A','B']) # => 'A,B'

Create a list from arguments

__listify('A','B') # => 'A,B'

Escape values

__listify('foo','bar^bam') # => 'foo,bar%5Ebam'

Do not escape the values

__listify('foo','bar^bam', escape: false) # => 'foo,bar^bam'


35
36
37
38
39
40
41
42
43
44
# File 'lib/elasticsearch/api/utils.rb', line 35

def __listify(*list)
  options = list.last.is_a?(Hash) ? list.pop : {}

  Array(list).flatten.
    map { |e| e.respond_to?(:split) ? e.split(',') : e }.
    flatten.
    compact.
    map { |e| options[:escape] == false ? e : __escape(e) }.
    join(',')
end

#__pathify(*segments) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a path (URL part) from arguments, ignoring nil values and empty strings.

# @example Encode special characters

__pathify(['foo', 'bar^bam']) # => 'foo/bar%5Ebam'

Examples:

Create a path from array

__pathify(['foo', '', nil, 'bar']) # => 'foo/bar'

Create a path from arguments

__pathify('foo', '', nil, 'bar') # => 'foo/bar'


58
59
60
61
62
63
64
# File 'lib/elasticsearch/api/utils.rb', line 58

def __pathify(*segments)
  Array(segments).flatten.
    compact.
    reject { |s| s.to_s =~ /^\s*$/ }.
    join('/').
    squeeze('/')
end

#__report_unsupported_method(name) ⇒ Object



246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/elasticsearch/api/utils.rb', line 246

def __report_unsupported_method(name)
  message = "[!] You are using unsupported method [#{name}]"
  if source = caller && caller.last
    message += " in `#{source}`"
  end

  message += ". This method is not supported in the version you're using: #{Elasticsearch::API::VERSION}, and will be removed in the next release."

  if STDERR.tty?
    STDERR.puts "\e[31;1m#{message}\e[0m"
  else
    STDERR.puts message
  end
end

#__report_unsupported_parameters(arguments, params = []) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/elasticsearch/api/utils.rb', line 206

def __report_unsupported_parameters(arguments, params=[])
  messages = []
  unsupported_params = params.select {|d| d.is_a?(Hash) ? arguments.include?(d.keys.first) : arguments.include?(d) }

  unsupported_params.each do |param|
    name = case param
    when Symbol
      param
    when Hash
      param.keys.first
    else
      raise ArgumentError, "The param must be a Symbol or a Hash"
    end

    explanation = if param.is_a?(Hash)
      ". #{param.values.first[:explanation]}."
    else
      ". This parameter is not supported in the version you're using: #{Elasticsearch::API::VERSION}, and will be removed in the next release."
    end

    message = "[!] You are using unsupported parameter [:#{name}]"

    if source = caller && caller.last
      message += " in `#{source}`"
    end

    message += explanation

    messages << message
  end

  unless messages.empty?
    if STDERR.tty?
      STDERR.puts messages.map { |m| "\e[31;1m#{m}\e[0m" }.join("\n")
    else
      STDERR.puts messages.join("\n")
    end
  end
end

#__rescue_from_not_found {|block| ... } ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Calls given block, rescuing from any exceptions. Returns ‘false` if exception contains NotFound/404 in its class name or message, else re-raises exception.

Yields:

  • (block)

    A block of code to be executed with exception handling.



196
197
198
199
200
201
202
203
204
# File 'lib/elasticsearch/api/utils.rb', line 196

def __rescue_from_not_found(&block)
  yield
rescue Exception => e
  if e.class.to_s =~ /NotFound/ || e.message =~ /Not\s*Found|404/i
    false
  else
    raise e
  end
end

#__validate_and_extract_params(arguments, params = [], options = {}) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Validates the argument Hash against common and valid API parameters

Examples:

Extract parameters

__validate_and_extract_params( { :foo => 'qux' }, [:foo, :bar] )
# => { :foo => 'qux' }

Raise an exception for invalid parameters

__validate_and_extract_params( { :foo => 'qux', :bam => 'mux' }, [:foo, :bar] )
# ArgumentError: "URL parameter 'bam' is not supported"

Skip validating parameters

__validate_and_extract_params( { :foo => 'q', :bam => 'm' }, [:foo, :bar], { skip_parameter_validation: true } )
# => { :foo => "q", :bam => "m" }

Skip validating parameters when the module setting is set

Elasticsearch::API.settings[:skip_parameter_validation] = true
__validate_and_extract_params( { :foo => 'q', :bam => 'm' }, [:foo, :bar] )
# => { :foo => "q", :bam => "m" }

Parameters:

  • arguments (Hash)

    Hash of arguments to verify and extract, **with symbolized keys**

  • valid_params (Array<Symbol>)

    An array of symbols with valid keys

Returns:

  • (Hash)

    Return whitelisted Hash

Raises:

  • (ArgumentError)

    If the arguments Hash contains invalid keys



142
143
144
145
146
147
148
149
# File 'lib/elasticsearch/api/utils.rb', line 142

def __validate_and_extract_params(arguments, params=[], options={})
  if options[:skip_parameter_validation] || Elasticsearch::API.settings[:skip_parameter_validation]
    arguments
  else
    __validate_params(arguments, params)
    __extract_params(arguments, params, options.merge(:escape => false))
  end
end

#__validate_params(arguments, valid_params = []) ⇒ Object



151
152
153
154
155
156
# File 'lib/elasticsearch/api/utils.rb', line 151

def __validate_params(arguments, valid_params=[])
  arguments.each do |k,v|
    raise ArgumentError, "URL parameter '#{k}' is not supported" \
      unless COMMON_PARAMS.include?(k) || COMMON_QUERY_PARAMS.include?(k) || valid_params.include?(k)
  end
end