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

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"}}


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/elasticsearch/api/utils.rb', line 73

def __bulkify(payload)
  case
  # Hashes with `:data`
  when payload.any? { |d| d.is_a?(Hash) && d.values.first.is_a?(Hash) && (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| MultiJson.dump(item) }
    payload << "" unless payload.empty?
    return payload.join("\n")

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

  # Header/Data pairs
  else
    payload = payload.map { |item| MultiJson.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

#__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'


32
33
34
35
36
37
38
39
# File 'lib/elasticsearch/api/utils.rb', line 32

def __listify(*list)
  Array(list).flatten.
    map { |e| e.respond_to?(:split) ? e.split(',') : e }.
    flatten.
    compact.
    map { |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'


53
54
55
56
57
58
59
# File 'lib/elasticsearch/api/utils.rb', line 53

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

#__validate_and_extract_params(arguments, valid_params = []) ⇒ 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"

Parameters:

  • arguments (Hash)

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

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

    An array of symbols with valid keys

Returns:

  • (Hash)

    Return whitelisted Hash

Raises:

  • (ArgumentError)

    If the arguments Hash contains invalid keys



122
123
124
125
126
127
128
129
130
131
# File 'lib/elasticsearch/api/utils.rb', line 122

def __validate_and_extract_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

  params = arguments.select { |k,v| COMMON_QUERY_PARAMS.include?(k) || valid_params.include?(k) }
  params = Hash[params] unless params.is_a?(Hash) # Normalize Ruby 1.8 and Ruby 1.9 Hash#select behaviour
  params
end