Module: Elasticsearch::API::Utils
Overview
Generic utility methods
Instance Method Summary collapse
-
#__bulkify(payload) ⇒ Object
Convert an array of payloads into Elasticsearch ‘headerndata` format.
-
#__escape(string) ⇒ Object
private
URL-escape a string.
- #__extract_params(arguments, params = []) ⇒ Object
-
#__extract_parts(arguments, valid_parts = []) ⇒ Array<String>
private
Extracts the valid parts of the URL from the arguments.
-
#__listify(*list) ⇒ Object
private
Create a “list” of values from arguments, ignoring nil values and encoding special characters.
-
#__pathify(*segments) ⇒ Object
private
Create a path (URL part) from arguments, ignoring nil values and empty strings.
-
#__rescue_from_not_found {|block| ... } ⇒ Object
private
Calls given block, rescuing from any exceptions.
-
#__validate_and_extract_params(arguments, params = [], options = {}) ⇒ Hash
private
Validates the argument Hash against common and valid API parameters.
- #__validate_params(arguments, valid_params = []) ⇒ Object
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, = item.to_a.first = .clone data = .delete(:data) || .delete('data') sum << { operation => } 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
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 = []) ⇒ Object
147 148 149 150 151 152 |
# File 'lib/elasticsearch/api/utils.rb', line 147 def __extract_params(arguments, params=[]) 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)] : [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.
Mutates the ‘arguments` argument, to prevent failures in `__validate_and_extract_params`.
Extracts the valid parts of the URL from the arguments
170 171 172 173 174 175 176 177 178 179 |
# File 'lib/elasticsearch/api/utils.rb', line 170 def __extract_parts(arguments, valid_parts=[]) # require 'pry'; binding.pry; # parts = arguments.keys.select { |a| valid_parts.include?(a) }.map { |a| a.to_s }.sort 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.
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'
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 |
#__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.
188 189 190 191 192 193 194 195 196 |
# File 'lib/elasticsearch/api/utils.rb', line 188 def __rescue_from_not_found(&block) yield rescue Exception => e if e.class.to_s =~ /NotFound/ || e. =~ /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
131 132 133 134 135 136 137 138 |
# File 'lib/elasticsearch/api/utils.rb', line 131 def __validate_and_extract_params(arguments, params=[], ={}) if [:skip_parameter_validation] || Elasticsearch::API.settings[:skip_parameter_validation] arguments else __validate_params(arguments, params) __extract_params(arguments, params) end end |
#__validate_params(arguments, valid_params = []) ⇒ Object
140 141 142 143 144 145 |
# File 'lib/elasticsearch/api/utils.rb', line 140 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 |